Real Time Clock Module With Arduino
by taste_the_code in Circuits > Arduino
10811 Views, 5 Favorites, 0 Comments
Real Time Clock Module With Arduino
In this Instructable, I'll go over the basics of what a real-time clock module is and how we can use it in our projects to keep an accurate representation of the time, even when the project is disconnected from the main power source
Supplies
Tools and materials used in this video:
- NodeMCU v3 - https://s.click.aliexpress.com/e/_9zsjMa
- DS1302 RTC Module - https://s.click.aliexpress.com/e/_9vbjPg
- Mini Breadboards - https://s.click.aliexpress.com/e/_9vg9cK
- Jumper Wires - https://s.click.aliexpress.com/e/_AZwpjC
- CR2032 Battery - https://s.click.aliexpress.com/e/_9fZL2e
Alternate links on Banggood:
- NodeMCU v3 - https://www.banggood.com/custlink/vmmdoCDTQk
- DS1302 RTC Module - https://www.banggood.com/custlink/KKDyOcm7Uv
- Mini Breadboards - https://www.banggood.com/custlink/mG3yZasO60
- Jumper Wires - https://www.banggood.com/custlink/KG3YjJGTqh
- CR2032 Battery - https://s.click.aliexpress.com/e/_9fZL2e
What Is a Real Time Clock?
A Real-Time Clock, or RTC for short, is an integrated circuit that keeps track of time. It uses a backup battery to maintain time in the event that the main power source is removed.
These modules use a crystal with a frequency of 32768Hz (2^15) and an internal binary counter to create a frequency of exactly 1Hz and count seconds. The 32768Hz frequency is used as a nice balance between the cost and size of the actual crystal.
Once the battery is in place and the time is initially set, these ICs and modules will keep the correct date and time for up to 5 years on a single 2032 lithium cell, because of the extremely low power consumption.
When to Use a Real-time Clock?
All microcontrollers have some type of oscillator that we can use to keep track of the elapsed time and do specific actions based on that time.
However, in the event of a power loss, that time will be lost and we can no longer know when to execute the action again.
With the RTC in place, once power is restored we can get the current time and date and continue with the execution of those actions based on the initial schedule.
So an RTC is really essential in the following use cases:
- executing actions at a specific time of the day
- executing actions on a specific date
- following schedules for actions in home automation
- following schedules in garden automation
- clock projects
- data logging
- etc.
Connect the Module to Arduino (NodeMCU)
The DS1302 module can work with both 3.3V and 5V so it can basically be used with any microcontroller.
In my case, I'm using a NodeMCU board.
Besides the power lines, we need three wires to connect the module to the NodeMCU by following the rules below:
- CLK/SCLK --> D5
- DAT/IO --> D4
- RST/CE --> D2
- VCC --> 3.3v - 5v
- GND --> GND
Program the Arduino
To interface the chip I'm using the Rtc by Makuna library and one of its examples for the DS1302 IC. Since the example is originally intended for the original Arduino boards it is important that you update the pin numbers if you use NodeMCU as I do.
In this example, we first initialize the communication with the module and we then check to see the state of the date and time set on the chip. If we find a valid date and time, then we display that on the serial monitor every 10 seconds.
If not, then we set the date and time to the chip based on the compiled date and time of the uploaded sketch and we continue with displaying the time every 10 seconds.
Downloads
What Will You Build Next?
I hope that with this Instructable I managed to demystify the RTC modules and give you a basic understanding of how they operate and how they can be used.
If you have any specific ideas on how an RTC module can be used in a project then be sure to let me know down in the comments.
If you want to learn more about Arduino and electronics in general then be sure to check out my YouTube channel and also check out my other Instructables.
Cheers!