Arduino real time clock 7 SEGment display

Hello world! Today i made a Arduino real time clock on a 4 digit display. The display i used is not really for clocks but it works, only the 2 dots are not in the right place, but you do not have to change anything in the code. You need a Library for this project. Follow the next step for the Schematic, Code & Parts list.

You need the following library SevSeg.h <-download it here check if you are using a common anode or cathode display You can change this in the code at line 26 int displayType = COMMON_CATHODE; //change that to COMMON_ANODE
Other instructions on how to set time are in the code for the Arduino real time clock.

Schematic:
Arduino real time clock

This is the rtc i used for the Arduino real time clock:
Arduino real time clock
Wire everything like the schematic and upload the code! Make sure the library is installed.

Parts list:
Arduino uno
AT24C32 / DS1307 real time clock ( I used the one on the picture above. There is also room for a temperature sensor like the LM35 on it)
4 Digit 7 Segment Display
4x 1 k Resistors. (Optional)
Jumper wires set MM/FM/MF

Code:

/*
Author: Danny van den Brande, arduinosensors.nl. BlueCore Tech.
This code is written for the AT24C32/DS1307 RTC Module and 
4 digit Seven Segment displays. 
Be sure to check if you have a common anode or cathode display.(change @ line 26)
 */
#include "Wire.h"
#include "SevSeg.h" //https://github.com/sparkfun/SevSeg/tree/master/src
 
#define DS1307_ADDRESS 0x68
 
//Create an instance
SevSeg display7seg;
 
int valor = 0;
 
byte zero = 0x00; 
unsigned long timer;
void setup()
{
  Wire.begin();
  //Comment this line after you have set the date @ void TimeSelection() below
//   TimeSelection(); Uncomment this line when your setting the time. Set time @ (line 66)
  
  //Change to COMMON_ANODE if you have a Common anode display.
  int displayType = COMMON_CATHODE; 
     
  int digit1 = 10; 
  int digit2 = 11; 
  int digit3 = 12; 
  int digit4 = 13; 
 
  int segA = 2;  
  int segB = 3; 
  int segC = 4; 
  int segD = 5; 
  int segE = 6; 
  int segF = 7; 
  int segG = 8; 
  int segDP= 9; 
   
  int numberOfDigits = 4;
 
  //Initialize display
  display7seg.Begin(displayType, numberOfDigits, digit1, digit2, digit3, digit4, segA, segB, segC, segD, segE, segF, segG, segDP);
   
  //Set brightness
  display7seg.SetBrightness(50);
  timer = millis();
}
 
void loop()
{
  char tempString[10]; //Used for sprintf
  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero);
  Wire.endTransmission();
  Wire.requestFrom(DS1307_ADDRESS, 7);
  int seconds = ConvertToDecimal(Wire.read());
  int minutes = ConvertToDecimal(Wire.read());
  int hours = ConvertToDecimal(Wire.read() & 0b111111); 
  sprintf(tempString, "%02d%02d", hours, minutes);
  display7seg.DisplayString(tempString, 3);
}
 
void TimeSelection()   //Select date and time here below!!(line 69,70,71)
//after setting time also comment time TimeSelection() @ void setup
{
  byte seconds = 10; //Valores de 0 a 59
  byte minutes = 38; //Valores de 0 a 59
  byte hours = 3; //Valores de 0 a 23
  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero); //Stop no CI para que o mesmo possa receber os dados
 
  //As linhas abaixo escrevem no CI os valores de 
  //data e hora que foram colocados nas variaveis acima
  Wire.write(ConvertToPCD(seconds));
  Wire.write(ConvertToPCD(minutes));
  Wire.write(ConvertToPCD(hours));
  Wire.write(zero);
  Wire.endTransmission(); 
}
 
byte ConvertToPCD(byte val)
{ 
  //Convert Decimal to binary
  return ( (val/10*16) + (val%10) );
}
 
byte ConvertToDecimal(byte val)  
{ 
  //Convert binary to decimal
  return ( (val/16*10) + (val%16) );
}
Danny van den Brande

http://www.arduinosensors.nl

Programmer, Game Designer, 3D Designer, Web Designer, Graphic Designer. Jup this website looks crappy.. :) But enjoy the projects! Everything you need is here.

Leave a Reply

Your email address will not be published. Required fields are marked *