← Back to Academy

Lesson 137: Keeping Time - Using a DS3231 Real-Time Clock (RTC)

If you’ve ever unplugged your Arduino, you know it has the memory of a goldfish. When it boots back up, its internal timer starts right back at zero. It has no concept of what day it is. If you want to build a real digital clock or a daily plant waterer, you need a Real-Time Clock (RTC). We’ll use the DS3231 RTC module, which has its own little battery so it can keep ticking even while your Arduino is passed out cold. A DS3231 Real Time Clock module connected to an Arduino Uno The DS3231 module keeps time independently of your Arduino, powered by its own coin cell battery.

The Magic of the Coin Cell

The DS3231 has a slot for a small CR2032 coin cell battery. This battery doesn’t power your Arduino; it simply keeps the tiny clock chip ticking inside the module. Even if your Arduino is sitting in a drawer unpowered for months, the DS3231 is quietly counting seconds, minutes, hours, and days.

Need the parts? Grab a DS3231 Real Time Clock module on Amazon. They usually come with the necessary battery included.

Wiring the DS3231

The DS3231 uses the I2C communication protocol, which means it only requires four wires to connect to the Arduino:

Installing the RTClib Library

To make reading the time incredibly easy, we’ll use a library created by Adafruit.

  1. Open the Arduino IDE.
  2. Go to Sketch > Include Library > Manage Libraries...
  3. Search for RTClib (by Adafruit) and install it.

Writing the Code

This code will check the RTC and print the current date and time to the Serial Monitor.

#include <Wire.h>
#include "RTClib.h"

// Create an RTC instance
RTC_DS3231 rtc;

void setup() {
  Serial.begin(9600);

  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1); // Halt if not found
  }

  // If the RTC lost power (battery was removed), set the time!
  if (rtc.lostPower()) {
    Serial.println("RTC lost power, setting the time...");
    // This line sets the RTC to the date & time this sketch was compiled:
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
}

void loop() {
  // Get the current time from the RTC
  DateTime now = rtc.now();

  // Print the date
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(" ");
  
  // Print the time
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();

  delay(1000); // Wait 1 second
}

Upload the sketch and open the Serial Monitor. You should see the exact time printed every second! Try unplugging your Arduino from your computer, waiting a minute, and plugging it back in. The time will still be perfectly correct.

Next Steps

Now that your Arduino knows the time, the possibilities are endless. You can trigger relays at specific hours to automate your home, or log sensor data to an SD card with precise timestamps!