Intro: Arduino Real Time Clock Tutorial
How to use a real time clock with arduino might sound like a lot of work, but hey, it’s not that difficult and not only is it fun, but you’ll learn microcontroller programming along the way!
We’ll assume that your still new to the world of microcontrollers but at the same time you are energized and can see the endless projects that you would like to make and are eager to learn more.
Here we will not only look at how to setup a real time clock module, but also build an LCD digital clock where we output the data from the real time clock to an LCD screen.
Step 1: The DS1307 Real Time Clock (RTC)
We will be using the DS1307 real time clock which is a low power, binary coded decimal (BCD) clock calendar. It provides seconds, minutes, hours, day, date, month and year information.
It adjusts the date for leap years and it also adjusts the months which have less than 31 days in them. The time format can be selected to either 12 or 24 hours and a battery backup is used to keep time when power is removed from the circuit.
The DS1307 can be used in your projects such as data logging to provide a time stamp or for scheduling purposes like an irrigation controller.
Step 2: Building the Project - LCD RTC Using Arduino
- Arduino Uno
- LCD Shield (that uses a 4 bit parallel interface)
- Real Time Clock (RTC) DS1307 module
- Prototype Breadboard
- Dupont or solid core hook up wires
- Toggle button (Optional)
- Computer with Arduino IDE installed
- USB cable
We wire up the clock by connecting the ground and power pins to the microcontroller's ground and power pins. Then the I2C wiring is connected using the clock’s SDA and SCL pins to the microcontrollers SDA and SCL pins which are analogue pin 5 for SCL and analogue pin 4 for SDA. There is an extra yellow wire that connects the board’s pin 2 to ground which we will look at a bit further down the page.
Step 3: How to Setup the DS1307 – RTC Time and Date
The full code is available at the link below:
When you look at the code, initially it does seem like a lot but don’t let that deter you. We have not used a clock library but are instead controlling the clock directly using the wire.h library. This way you can understand how the I2C communication works when we set and read the information to and from the clock.
We first set the time and date variables to the time and date you would like the clock to start at. We entered the byte variables in decimal form as the code will convert them into binary coded decimal before the values are sent to the clock’s registers.
Once the clock is set we can then read the latest clock data, then output them to the LCD screen.
Earlier up the page we mentioned the extra yellow wire connecting the board’s pin 2 to ground. We have introduced an "If-Else" statement so the clock does not reset every time during power up. So, after we upload the program, we set pin 2 high and the clock will be set at power up. After that, we can connect pin 2 to ground and leave it that way until we want to reset the clock again.
To make it easier you could place a button that goes high when pressed (see the image above). In order to reset the time, the button is held down just before the board is powered up, and once the time starts to display on the LCD we can release the button.
Step 4: Conclusion
Using a real time clock in your projects can be very useful allowing you to advance your projects further to report real time and date information.
I hope this Tutorial has been useful to you. If you liked it please share it with a friend.
Also, I'd really appreciate it for you to join my email subscriber list where we'll keep you up to date with our latest videos, tutorials and articles relating to microcontrollers, Arduino and other electronics information. You'll also receive our free newsletter which is exclusive to our email subscribers only !
Thanks for spending your time to read this Instructable. I really appreciate it !