fbpx

Interface RTC with Arduino Uno

A Real Time Clock is like a watch – it runs on a battery and keeps time even when there is a power outage.

  • DS1307 is an I2c based serial real time clock (RTC).
  • RTC keep track of seconds, minutes, hours, date of month, month, day of month and year with leap year compensation which is valid upto year 2100.
  • The clock can be programmed to operate either in 12-hour or 24-hour format.
  • DS1307 requires very less number of components to operate.
  • RTC DS1307 operate in frequency of crystal oscillator 32.768kHz.
  • DS1307 uses I2C Protocol.
  • RTC act as a slave device and I2C Master can read/write register of RTC.
  • To communicate with the slave device, master need the slave address of device connected on bus.
  • DS1307 has fixed slave address.
  • Slave Address
    7-Bit format: 0b1101000 = 0x68
    Slave address for I2C Write: 0b11010000 = 0xD0
    Slave address for I2C Read: 0b11010001 = 0xD1.

Application

  • Digital Clock.

HardWare Required

  • DS1307.
  • 3.3v Cmos battery.
  • Arduino.

Pin Out

  • SCL and SDA to port pins.
  • Gnd: Gnd.

Circuit

Code

 #include <Time.h>
 #include <Wire.h>
 #include <DS1307RTC.h>
 void setup()
 {
 Serial.begin(9600);
 setSyncProvider(RTC.get);
 if(timeStatus()!= timeSet)
 {
 Serial.println("Unable to sync with the RTC");
 }
 else
 {
 Serial.println("RTC has set the system time");
 }
 }
 void loop()
 {
 digitalClockDisplay();
 delay(1000);
 }
 void digitalClockDisplay()
 {
 Serial.print(hour());
 printDigits(minute());
 printDigits(second());
 Serial.print(" ");
 Serial.print(day());
 Serial.print("/ ");
 Serial.print(month());
 Serial.print("/ ");
 Serial.print(year());
 Serial.println();
 }
 void printDigits(int digits)
 {
 Serial.print(":");
 if(digits < 10)
 {
 Serial.print('0');
 Serial.print(digits);
 }
 } 

Leave a Reply

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