Vous êtes sur la page 1sur 7

real time clock DS1307 interfacing

with Arduino
Bilal Malik 2 Years Ago 5 Comments
real time clock DS1307 interfacing with Arduino, In this article you will learn
how to interface real time clock DS1307 with Arduino. What is real time? why
real time clock is used? what is dedicated integrated circuit for real time
clock? how to make digital clock using Arduino and integrated circuit
DS1307? What are application of real time clock DS1307? Hardware
connections of liquid crystal display 16 X 2 LCD and real time clock DS1303
with Arduino. How to use library of real time clock to display time and date
on LCD? You will get answers of all your questions in this article.

Before reading this article you should have a basic knowledge about Arduino
and its use. You should know how to interface LCD with Arduino and how to
use input/ output ports of Arduino. If you dont know how to do these things. I
recommend you to go throgh following articles to get better understanding of
this article. Because I have already posted articles on such things. For more
information check following articles.

Page Contents [hide]


o 0.1 Getting started with Arduino UNO R3.
o 0.2 LED blinking using Arduino UNO R3
o 0.3 How to use push button with Arduino UNO R3
o 0.4 LCD interfacing with Arduino UNO R3
1 What is real time clock?
2 DS1307 IC for real time clock:
3 Real time clock DS1307 with Arduino :
4 Programming:
o 4.1 RTC library

Getting started with Arduino UNO R3.


LED blinking using Arduino UNO R3
How to use push button with Arduino UNO R3
LCD interfacing with Arduino UNO R3
What is real time clock?
As it name suggests, real time clock is used to keep record off time and to
display time. It is used in many digital electronics devices like computers,
electronics watches , date loggers and situation where you need to keep
track of time. one of the great benefits of real time clock is that it also keep
record of time even if power supply is not available. Now the question is how
can a electronics device like real time clock work without use of power
supply. Because it have small power cell of about 3-5 volt inside which can
work for years. Because real time clock consume minimum amount of power.
There is many dedicated integrated circuits are available in market which is
used to make real time clock by adding necessary electronic components.
But in this article I will discuss DS1307 real time clock IC.

DS1307 IC for real time clock:


DS1302 is IC for real time clock which is used to count seconds, minutes ,
hours, days, months any years. It use I2C communication protocol for
communicating with other devices like in our case we are using Arduino.
Arduino read values of time and date from DS1307 using I2C communication
protocol. It also have feature to keep record of exact time in case of power
failure. It is a 8 bit IC. It is used to make real time clock using some other
electronic components. Pin configuration of DS1307 is given below:

pin configuration of DS1307


Functionality of each pin is given below:
pin number one and two is used for crystal oscillator . Crystal oscillator
value usually used with DS1307 is 32.768k Hz.
Pin three is used for back up battery. Its value should be between 3-5
volt. voltage more than 5 volt may burn DS1307 permanently. Back
battery is used to keep track of time in case of power failure to DS1307.
After getting power DS1307 shows correct time due to back up battery.
pin 4 is ground pin of power supply.
pin 5 and 6 is used to communicate with other devices with the help of
I2C communication protocol.
pin 5 is serial data pin and pin 6 is serial clock. If you dont know about
I2C communication, i recommend you to learn about it.
Pin 8 is used for 5 volt power supply.
you need external components to connect with DS1307 to use it as a real
time clock. Circuit diagram with necessary components is given below:
real time clock DS1307 circuit diagram
In above circuit diagram there is no connection for power supply and ground
at pin number four and eight. Dont forget to to provide power supply of 5
volt to this circuit while making this circuit.

Real time clock DS1307 with Arduino :


real time clock DS1307 with Arudino circuit is given below. pin number 5 and
6 of DS1307 is connected with SCL and SDA pins of Arduino. If you have
already gone through above mentioned article on LCD interfacing with
Arduino and other basic articles to get know how of Arduino, you can easily
understand following circuit.
circuit diagram of DIgital clock using real time clock and Arduino
All connections in above circuit diagram is self explanatory, but if you still
have any issue. your comments are welcome.

Programming:
The code given below is write to display date and time on LCD.

#include <Wire.h>
#include RTClib.h
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
RTC_DS1307 RTC;
void setup () {
Serial.begin(9600);
Wire.begin();
RTC.begin();
lcd.begin(20, 4);
pinMode(8,OUTPUT);
if (! RTC.isrunning()) {
Serial.println(RTC is NOT running!);
// following line sets the RTC to the date & time this sketch was
compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
}
void loop () {
DateTime now = RTC.now();
lcd.setCursor(0, 0);
lcd.print(now.day(), DEC);
lcd.print(/);
lcd.print(now.month(), DEC);
lcd.print(/);
lcd.print(now.year(), DEC);
lcd.print( );
lcd.setCursor(0, 1);
if (now.hour()<10)
lcd.print(0);
lcd.print(now.hour(), DEC);
lcd.print(:);
if (now.minute()<10)
lcd.print(0);
lcd.print(now.minute(), DEC);
lcd.print(:);
if (now.second()<10)
lcd.print(0);
lcd.print(now.second(), DEC);
delay(1000);
}
The result of above code is given below:
circuit diagram of DIgital clock using real time clock and Arduino result
In above code #include RTClib.h is used to include library of real time
clock. If you dont have already library installed in your Arduino IDE compiler.
you can add it very easily. Download library from given below link. After
downloading code, abstract the code and paste the file into libraries folder of
Arduino UNO folder.

Vous aimerez peut-être aussi