Real Time Clock RTC Module Arduino - Random Nerd Tutorials
Maybe your like
This post is about how to use the DS1307 Real Time Clock (RTC) module with the Arduino. You can also follow this guide for other similar modules like the DS3231 RTC.
Introducing the Real Time Clock module
The real time clock module is the one in the figure below (front and back view).

When you first use this module, you need to solder some header pins.
As you can see in the picture above, the module has a backup battery installed. This allows the module to retain the time, even when it’s not being powered up by the Arduino. This way, every time you turn on and off your module, the time doesn’t reset.
This module uses I2C communication. This means that it communicates with the Arduino using just 2 pins.
Where to buy?
The Real Time Clock is an affordable module. You can check the DS1307 Real Time Clock module on Maker Advisor and find the best price.
Pin Wiring
Wiring the RTC module is pretty straightforward!
| Pin | Wiring to Arduino Uno |
| SCL | A5 |
| SDA | A4 |
| VCC | 5V |
| GND | GND |
If you’re using other Arduino board rather than the uno, chek out what are their SCL and SDA pins.
- Nano: SDA (A4); SCL(A5)
- MEGA: SDA (20); SCL(21)
- Leonardo: SDA (20); SCL(21)
Example: Displaying date and time on the serial monitor
This example displays date and time on the serial monitor.
Parts required
For this example you need the following parts (click the links below to find the best price at Maker Advisor):
- Arduino UNO – read Best Arduino Starter Kits
- DS1307 RTC module
- Jumper wires
You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Schematics
Connect your Real Time Clock module to your Arduino as in the schematics below.

Code
Working with the RTC requires two important steps:
- setting the current time, so that the RTC knows what time is it
- retaining the time, so that the RTC always gives the correct time, even when it is turned off
Set the current time in the Real Time Clock
For setting the current time you need to change the code provided.
- set your current time int the function setDS3231time()

The parameters for the function are highlighted in red: seconds, minutes, hours, day of the week, date, month and year (in this order). Sunday is the day 1 of the week and Saturday is 7. Don’t forget to uncomment that line of code.
After setting the current time, you can upload the provided code with the required modifications.
The code provided was written by John Boxall from tronixstuff. You can read his tutorial here.
// Written by John Boxall from http://tronixstuff.com #include "Wire.h" #define DS3231_I2C_ADDRESS 0x68 // Convert normal decimal numbers to binary coded decimal byte decToBcd(byte val){ return( (val/10*16) + (val%10) ); } // Convert binary coded decimal to normal decimal numbers byte bcdToDec(byte val){ return( (val/16*10) + (val%16) ); } void setup(){ Wire.begin(); Serial.begin(9600); // set the initial time here: // DS3231 seconds, minutes, hours, day, date, month, year setDS3231time(30,42,16,5,13,10,16); } void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte dayOfMonth, byte month, byte year){ // sets time and date data to DS3231 Wire.beginTransmission(DS3231_I2C_ADDRESS); Wire.write(0x0E); // select register Wire.write(0b00011100); // write register bitmap, bit 7 is /EOS Wire.write(decToBcd(second)); // set seconds Wire.write(decToBcd(minute)); // set minutes Wire.write(decToBcd(hour)); // set hours Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday) Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31) Wire.write(decToBcd(month)); // set month Wire.write(decToBcd(year)); // set year (0 to 99) Wire.endTransmission(); } void readDS3231time(byte *second, byte *minute, byte *hour, byte *dayOfWeek, byte *dayOfMonth, byte *month, byte *year){ Wire.beginTransmission(DS3231_I2C_ADDRESS); Wire.write(0); // set DS3231 register pointer to 00h Wire.endTransmission(); Wire.requestFrom(DS3231_I2C_ADDRESS, 7); // request seven bytes of data from DS3231 starting from register 00h *second = bcdToDec(Wire.read() & 0x7f); *minute = bcdToDec(Wire.read()); *hour = bcdToDec(Wire.read() & 0x3f); *dayOfWeek = bcdToDec(Wire.read()); *dayOfMonth = bcdToDec(Wire.read()); *month = bcdToDec(Wire.read()); *year = bcdToDec(Wire.read()); } void displayTime(){ byte second, minute, hour, dayOfWeek, dayOfMonth, month, year; // retrieve data from DS3231 readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year); // send it to the serial monitor Serial.print(hour, DEC); // convert the byte variable to a decimal number when displayed Serial.print(":"); if (minute<10){ Serial.print("0"); } Serial.print(minute, DEC); Serial.print(":"); if (second<10){ Serial.print("0"); } Serial.print(second, DEC); Serial.print(" "); Serial.print(dayOfMonth, DEC); Serial.print("/"); Serial.print(month, DEC); Serial.print("/"); Serial.print(year, DEC); Serial.print(" Day of week: "); switch(dayOfWeek){ case 1: Serial.println("Sunday"); break; case 2: Serial.println("Monday"); break; case 3: Serial.println("Tuesday"); break; case 4: Serial.println("Wednesday"); break; case 5: Serial.println("Thursday"); break; case 6: Serial.println("Friday"); break; case 7: Serial.println("Saturday"); break; } } void loop(){ displayTime(); // display the real-time clock data on the Serial Monitor, delay(1000); // every second }View raw code
Retain the time in the Real Time Clock
If you don’t want to reset the time everytime the RTC is turned off, you should do the following:
- after setting up the time, you should comment the function that sets the time and upload the code again.

This is a very important step to set up the time in your RTC. If you don’t do this, everytime your RTC resets, it will display the time that you’ve set up previously and not the current time.
Demonstration
Open the serial monitor at a baud rate of 9600 and you’ll see the results.
Here’s the Serial Monitor displaying the current date and time.

Wrapping up
I hope you’ve found this guide useful.
The RTC module is really useful and you can use it as a clock, timer, etc..
If you would like to display the date and time with the RTC module in the OLED display, check the following post:
- Guide for 0.96 inch OLED Display with Arduino
What projects have you done or are you expecting to build with this module?
Let me know by writing a comment down below.
Thanks for reading,
Rui and Sara
Tag » Arduino Zs-042 Example
-
How To Use A Real-Time Clock Module (DS3231) - Arduino Cloud
-
Arduino Real Time Clock Module (ZS-042) - Eli The Computer Guy
-
Arduino And DS3231 Real Time Clock Tutorial - How To Mechatronics
-
Configuring The ZS-042 Real Time Clock Module - DS3231
-
Arduino And DS3231 Real Time Clock Tutorial - YouTube
-
In-Depth: Interface DS3231 Precision RTC Module With Arduino
-
Arduino DS3231 RTC Module Tutorial - Real Time Clock
-
Real Time Clock Using DS3231 (EASY) : 4 Steps - Instructables
-
Sleemanj/DS3231_Simple - GitHub
-
Arduino & Horloge DS3231 (RTC = Real Time Clock) - /dev/tbo
-
Arduino Usage | Adafruit DS3231 Precision RTC Breakout
-
ZS-042 RTC (Real Time Clock) Module - Electronics In Touch Co.
-
Bộ 2 Mô đun Arduino DS3231 ZS042 AT24C32 IIC Làm Bộ Nhớ Thời ...