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

VID_20210331_070916.00_00_05_06.Still001.jpg
How to use Arduino Real Time Clock module with DS1302 chip

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

What Is a Real Time Clock?

VID_20210331_070916.00_00_54_08.Still002.jpg
VID_20210331_070916.00_03_59_09.Still004.jpg
VID_20210331_070916.00_01_34_20.Still003.jpg

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?

VID_20210331_070916.00_04_36_12.Still005.jpg

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)

VID_20210331_070916.00_09_26_06.Still009.jpg

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

VID_20210331_070916.00_05_03_00.Still006.jpg
VID_20210331_070916.00_05_36_19.Still007.jpg
VID_20210331_070916.00_07_02_18.Still008.jpg

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?

VID_20210331_070916.00_10_10_12.Still010.jpg

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!