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

HARDWARE VIDEO

SIMULATION VIDEO


  • Here i am interfacing Real time clock with Arduino Uno 
  • sends time to pc via serial port and 7 segment
  • connect SDA---> A4, SCL--->A5
  • 4 digit common cathode seven segment
  • uln2003 as a driver of 4  control pins of common cathode
  • RTC IC DS1307
  • 330 ohm resistor for LED
CIRCUIT



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
}