CLOCK USING RTC IC DS1307,ARDUINO AND SEVEN SEGMENT DISPLAY (DISPLAYS DATE AND TIME)

Photo of author

By Jackson Taylor

Home » Electronics Topics » Arduino » CLOCK USING RTC IC DS1307,ARDUINO AND SEVEN SEGMENT DISPLAY (DISPLAYS DATE AND TIME)

As an Amazon Associate we earn from qualifying purchases.

HARDWARE VIDEO

YouTube video

SIMULATION VIDEO

YouTube video

CIRCUIT

Arduino Uno connected to DS1307 RTC and 4-digit seven segment display circuit diagram

PROGRAM

#include <Wire.h>
const int DS1307 = 0x68; // Address of DS1307 see data sheets
byte second = 0;
byte minute = 0;
byte hour = 0;
byte weekday = 0;
byte monthday = 0;
byte month = 0;
byte year = 0;
int led;
int x;
//7 SEGMENT connections
#define a 2
#define b 3
#define c 4
#define d 5
#define e 6
#define f 7
#define g 8
#define c1 13
#define c2 12
#define c3 11
#define c4 10
void setup()
{
sevensegmentinit();
  Wire.begin();
  Serial.begin(9600);
  delay(100);
//setTime();
Serial.print("HHH");
}
void loop()
{
readTime();
x=x+1;
if(x ==3)
{x=0;
 for(int i=0;i<62;i++){
led=monthday/10;ledposition1(); displaydata(led);   delay1();
led=monthday%10;ledposition2(); displaydata(led); delay1();
 if(month<10)
 {
 led=month%10;ledposition4(); displaydata(led);  delay1();
 ledposition3();cross();  delay1();
 }
 else
 {
led=month/10;ledposition3(); displaydata(led);   delay1();
led=month%10;ledposition4(); displaydata(led);  delay1();
 }
}}
if(hour>12)hour%=12;
for(int i=0;i<62;i++)
{
 if(hour<10)
 {led=hour%10;ledposition1(); displaydata(led);  delay1();
 ledposition2();cross();  delay1();
 }
 else
 {
led=hour/10;ledposition1(); displaydata(led);   delay1();
led=hour%10;ledposition2(); displaydata(led);  delay1();
 }
led=minute/10;ledposition3(); displaydata(led);   delay1();
led=minute%10;ledposition4(); displaydata(led); delay1();
}
}
void delay1(void)
{
  delay(4);
}
void ledposition1(void)
{
digitalWrite(c1,HIGH);
digitalWrite(c2,LOW);
digitalWrite(c3,LOW);
digitalWrite(c4,LOW);  
}
void ledposition2(void)
{
digitalWrite(c1,LOW);
digitalWrite(c2,HIGH);
digitalWrite(c3,LOW);
digitalWrite(c4,LOW);  
}
void ledposition3(void)
{
digitalWrite(c1,LOW);
digitalWrite(c2,LOW);
digitalWrite(c3,HIGH);
digitalWrite(c4,LOW);  
}
void ledposition4(void)
{
digitalWrite(c1,LOW);
digitalWrite(c2,LOW);
digitalWrite(c3,LOW);
digitalWrite(c4,HIGH);  
}
void sevensegmentinit(void)
{
pinMode(a,OUTPUT);
pinMode(b,OUTPUT);
pinMode(c,OUTPUT);
pinMode(d,OUTPUT);
pinMode(e,OUTPUT);
pinMode(f,OUTPUT);
pinMode(g,OUTPUT);
pinMode(c2,OUTPUT);digitalWrite(c2,HIGH);
pinMode(c3,OUTPUT);digitalWrite(c3,HIGH);
pinMode(c4,OUTPUT);digitalWrite(c4,HIGH);
pinMode(c1,OUTPUT);digitalWrite(c1,HIGH);
offallled();
}
void test (void)
{
  nine();delaystart();eight();delaystart(); seven();delaystart();
 six();  delaystart(); five();delaystart(); four(); delaystart();
 three(); delaystart(); two();delaystart();one();  delaystart(); zero(); delaystart();
}
void delaystart(void)
{
  delay(200);
}
void offallled(void)
{
digitalWrite(c1,LOW);  delay(300);
digitalWrite(c2,LOW);   delay(300);
digitalWrite(c3,LOW);   delay(300);
digitalWrite(c4,LOW);   delay(300);
}
void cross(void)
{
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(g,HIGH);
}
void nine(void)
{
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,LOW);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
}
void eight (void)
{
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
}
void seven(void)
{
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
}
void six(void)
{
digitalWrite(a,HIGH);
digitalWrite(b,LOW);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
}
void five(void)
{
digitalWrite(a,HIGH);
digitalWrite(b,LOW);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,LOW);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
}
void four(void)
{
digitalWrite(a,LOW);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
}
void three(void)
{
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(g,HIGH);
}
void two(void)
{
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,LOW);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,LOW);
digitalWrite(g,HIGH);
}
void one(void)
{
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,LOW);
}
void zero(void)
{
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,LOW);
}
byte bcdToDec(byte val)
{
  int msbdec=val/16*10; //gets decimel msb of BCD value(4 bits)
  int lsbdec = val%16;//gets decimel lsb of BCD value(4 bits)
  int total=msbdec+lsbdec;
  return (total);
  //return ((val/16*10) + (val%16));
}
void readTime()
{
  Wire.beginTransmission(DS1307);
  Wire.write(byte(0));
  Wire.endTransmission();
  Wire.requestFrom(DS1307, 7);
  second = bcdToDec(Wire.read());
  minute = bcdToDec(Wire.read());
  hour = bcdToDec(Wire.read());
  weekday = bcdToDec(Wire.read());
  monthday = bcdToDec(Wire.read());
  month = bcdToDec(Wire.read());
  year = bcdToDec(Wire.read());
   Serial.print(monthday);
   Serial.write('-');
   Serial.print(month );
   Serial.write('-');
   Serial.print(year);
   Serial.write('-');
   Serial.write('-');
   Serial.print(hour);
   Serial.write(':');
   Serial.print(minute);
   Serial.write(':');
   Serial.print(second);
   Serial.write(0xd);
}
void displaydata(int led)
{
  switch(led)
  {
   case 0:    zero(); break;
   case 1:    one(); break;
   case 2:    two(); break;
   case 3:    three(); break;
   case 4:    four(); break;
   case 5:    five(); break;
   case 6:    six(); break;
   case 7:    seven(); break;  
   case 8:    eight(); break;
   case 9:    nine(); break;
 }
}
void setTime() {
  // The following codes transmits the data to the RTC
  Wire.beginTransmission(DS1307);
  Wire.write(byte(0)); //starts sending data writing
  Wire.write(0x12);//second
  Wire.write(0x08); //minute
  Wire.write(0x18); //hour
  Wire.write(0x5); //weekday
  Wire.write(0x29);//monthday
  Wire.write(0x10);//month
  Wire.write(0x15);//year
  Wire.write(byte(0));
  Wire.endTransmission();
  // Ends transmission of data
}

How the DS1307 Works with Arduino

The DS1307 is an I2C real time clock. It keeps time with an external 32.768 kHz crystal and has a backup battery input, so the clock keeps running when the Arduino is powered off. On the Arduino Uno it shares the two I2C lines (SDA on A4, SCL on A5), and the code above talks to it at address 0x68.

See also
CUK CONVERTER WITH MPPT FOR 12V BATTERY CHARGING FROM SOLAR PANEL USING ARDUINO

The chip stores each value in BCD (binary coded decimal) rather than as a plain number. For example, 18 hours is stored as 0x18, which is 24 in ordinary binary. That is why the program passes every value through bcdToDec() before showing it. Without that step, the digits on the display would look wrong for most times. The registers are read in order: seconds, minutes, hours, weekday, day of month, month and year. The year holds only two digits, so 0x15 means 15.

Setting the time for the first time

  1. Edit the values in setTime() to the current time and date. Remember they are written in BCD, so write 0x30 for 30.
  2. Uncomment the setTime(); line in setup() and upload the sketch once.
  3. Comment the line out again and upload a second time.

The second upload matters. If setTime() stays active, the clock goes back to the same time on every reset or power cycle.

Hardware Hack

Stop Buying New Batteries for Your Tech

Electronics are expensive, but their power sources don't have to be. Discover the simple engineering trick to recondition dead batteries and bring your gadgets back to 100% capacity.

Watch the Free Video

If the display shows the wrong time

  • Time never changes or reads zero: check the SDA and SCL wiring and the common ground. The Serial Monitor at 9600 baud prints the raw date and time, so you can tell whether the problem is the RTC or the display.
  • Time resets after power off: the backup battery is missing, flat or fitted the wrong way round.
  • Garbled readings: the I2C lines need pull-up resistors. Most DS1307 breakout boards already include them, but a bare chip on a breadboard does not.
  • Flickering digits: the four digits are multiplexed, so the short delay in delay1() sets how steady the display looks.

The code shows the hour in 12-hour format and does not show AM or PM. The date appears briefly once every few passes through the loop, then the clock goes back to the time.

Revive Your Dead Electronics

Got a drawer full of dead gadgets or a failing car battery? Instead of paying a premium for replacements, use this simple trick to easily recondition them right from home.

See How It Works