Real Time Clock (RTC)

By Emir

Arduino doesn't know the answer to a simple question: What time is it?

To be able to know that, Arduino project needs a Real Time Clock.

This tutorial will show you how to set the date and time and how to read it.

Step 1

Bill of materials

  • Arduino board with USB cable (we use Arduino Uno)
  • Real time clock module - DS3231
  • 4 pcs male to male wires
Step 2

Connect wires to RTC

For real time clock module use we need four wires.

Connect female side wires with male pins on RTC module board.

show more
Step 3

Connect RTC module with Arduino

RTC module uses a special protocol for communication with Arduino, called I2C.

For I2C communication Arduino board uses special pins. For each Arduino board it is different.

Arduino UNO uses pins A4 for SDA and A5 for SCL.

Connect male wires on Arduino board:

  • A4 - yellow;
  • A5 - green;
  • 5V - blue;
  • GND - purple;
Step 4

Upload code

Open Arduino IDE and copy/paste code below.

show more
Step 5

Upload code to Arduino board

Connect Arduino with your computer using USB.

Upload Code to Arduino.

show more
Step 7

Set RTC date and time

Open Arduino IDE with code you uploaded in step 4.

Line 19 starts with "//". These characters are used for comments, meaning that compiler will ignore that line.

//setDS3231time(30,9,11,3,5,4,16);

This line is used to set date and time. It is inside setup function, which means that it will be executed just once when Arduino is started.

To set time, change values and delete "//" at line 19:

setDS3231time(30,9,11,3,5,4,16);

In this example 30 represents seconds, 9 - minutes, 11 - hours, 3 - day in week starting from Sunday, 5 - day of month, 4 - month and 16 - year.

Make changes to this line in order to set current date and time.

Upload code again like in step 5.

Step 8

Comment last change again

To avoid date and time being set to value we enter every time we turn Arduino on, we have to comment again the line 19 and upload code once again.

Just add two slashes at the start of line 19 and upload code again.

//setDS3231time(30,9,11,3,5,4,16);
Step 9

Battery for RTC

If we were to disconnect USB cable from Arduino and plug it in again, date and time that we have set would be lost.

The biggest value of real time clock is to keep track of time even when Arduino is powered off. To be able to do that, TRC has to be equipped with 3V battery CR2032.

We could have added the battery at the beginning, but you would not noticed it's importance.

Insert battery in a slot on RTC module.

Now, you can turn off Arduino and you will see that after turning it back on, RTC keeps date and time set in previous steps.

Tutorial Forum

There are no posts here yet. Add the first post