Skip to Content

Arduino and RTC Module DS3231

Arduino and RTC Module DS3231

Do you want to know how to use an Arduino with a real-time clock (RTC) module? 

Well, you’re in the right place! In this article, we’ll go over all the basics of setting up and using an RTC module DS3231 with an Arduino.

But first, what is an RTC module and why might you want to use one? 

An RTC module is a device that keeps track of the current time and date using an onboard crystal oscillator and a battery backup.

The RTC Module DS3231 can be really useful if you want to build a project that needs to keep track of time, even when it’s not connected to a power source. 

Some examples of projects that might use an RTC module include alarms, timers, and data logging systems.

In this article, we’ll provide you with a connection diagram and code examples to help you get started using an RTC module built using DS3231 with your Arduino.

You will display the time on an LCD. 

So grab your breadboard, and let’s get started!

Components Needed To Build Arduino And RTC Module Sensor Project

Hardware Components

Software

Makerguides.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to products on Amazon.com.

Basics of The RTC Module DS3231

In this section, we will understand the basics of the DS3231 RTC module. It’s a low-cost RTC with many features and super-accurate timekeeping.

So, what makes the DS3231 so great? For starters, it’s got an integrated temperature-compensated crystal oscillator (TCXO) that helps it keep super accurate time – we’re talking a precision of +/- 2 minutes per year.

That’s a lot better than a lot of other RTCs out there.

But the DS3231 isn’t just accurate, it’s also super handy.

It’s got a built-in battery backup that lets it keep track of the time even when it’s not powered, and it’s got programmable alarms and timers that can trigger events at specific times or intervals.

The DS3231 communicates with your Arduino UNO using the I2C protocol, which only requires two wires. This makes it super easy to connect and get up and running. 

In this article, we’ll see the libraries available that make it a breeze to use the DS3231 with your microcontroller, with functions for 

  • reading and writing to the RTC,
  • setting alarms and timers, and 
  • converting the time and date data into a format that’s easy to use in your code.

So there you have it – the DS3231 is a low-cost, highly accurate RTC with a bunch of useful features. 

It’s great for various electronic projects that need timekeeping capabilities, and it’s easy to use with a microcontroller.

Give it a try!

Features of the RTC DS3231 timekeeping module

Here are the features of Arduino-compatible RTC module DS3231:

  • Highly accurate RTC completely manages all timekeeping functions
    • Real-Time Clock Counts Seconds, Minutes, Hours,
    • Date of the Month, Month, Day of the Week, and
    • Year, with Leap-Year Compensation Valid Up to 2100
    • Accuracy ±2 ppm from 0°C to +40°C
    • Accuracy ±3.5ppm from -40°C to +85°C
    • Digital Temp Sensor Output: ±3°C Accuracy
    • Register for Aging Trim
    • RST Output/Push Button Reset Debounce Input
    • Two Time-of-Day Alarms
    • Programmable Square-Wave Output Signal
  • Simple serial interface connects to most microcontrollers
    • Fast (400kHz) I2C Interface
  • Battery-Backup Input for Continuous Timekeeping
    • Low Power Operation Extends Battery-Backup
  • Operating voltage – 3.3 V

Pin Details Of The RTC Module DS3231

Let’s have a closer look at the RTC DS3231 module.

RTC DS3231 module

Here is the pin description of the pins of the RTC module

  1. 32 kHz Output – This pin provides you a very accurate 32 kHz clock from the crystal.
  2. Square wave output – This is square wave output. You can configure the desired frequency of the output.
  3. I2C Clock – I2C communication protocol Clock line.
  4. I2C Data – I2C data line.
  5. Power Supply – Power input – You can connect 5 V to this pin.
  6. Ground – Ground connections.

The RTC DS3231 IC supports address configuration. It helps you to resolve I2C address conflicts. An I2C address conflict occurs when two or more devices on the same I2C bus have the same address. This can cause issues with communication on the bus, as the devices may not be able to distinguish each other’s messages.

To resolve an I2C address conflict, you can change the address of one of the conflicting devices. RTC DS3231 has a way to change its I2C address. There are three pins: A0, A1, and A2 address pins. Here is how you can set the I2C address of the module.

Here is an example application Example. Notice there is a VBAT connection.

It helps the DS3231 chip to keep the time running when the external supply is not available.

DS3231 chip

I have summarised the pin description of the DS3231 chip in the table below

Pin numberPin namePin function
132 kHz32kHz Output. This open-drain pin requires an external pullup resistor. When enabled, the output operates on either power supply. It may be left open if not used.
2VCCDC Power Pin for Primary Power Supply. This pin should be decoupled using a 0.1µF to 1.0µF capacitor. If not used, connect to ground.
3#INT/SQWActive-Low Interrupt or Square-Wave Output. This open-drain pin requires an external pullup resistor connected to a supply at 5.5V or less.
4#RSTActive-Low Reset. This pin is an open-drain input/output. It indicates the status of VCC relative to the VPF specification. As VCC falls below VPF, the RST pin is driven low.
5-12N.C.No connection. Connect to ground
13GNDGround connection
14VBATBackup Power-Supply Input. When using the device with the VBAT input as the primary power source, this pin should be decoupled using a 0.1µF to 1.0µF low-leakage capacitor.
15SDASerial Data input/output
16SCLSerial clock input

Step-By-Step Instructions To Connect The RTC Module DS3231 with Arduino UNO

In this section, we will build a project using Arduino UNO, LCD and the famous RTC module DS3231.

The connections are easy to take significantly less time to complete. 

The RTC module and the LCD communicate with the Arduino via I2C serial interface. 

Let’s get started with the hardware connections!

How To Connect The RTC module DS3231 to the Arduino UNO?

Below is the step-by-step connection guide to complete the Arduino and the RTC DS3231 module. 

Step 1: Start with the GND connections

Start with the GND connections

Connect the GND pin of the module to the GND of the Arduino.

Choose any GND pins available on the Arduino for the connection.

It is an excellent practice to start with the GND connections. 

Step 2: Connect the I2C SCL line

Connect the I2C SCL line

Connect the SCL pin on the DS3231 to A5 on the Arduino. Analog pin A5 is the I2C clock line pin of the Arduino. 

Step 3: Connect the I2C SDA line

Connect the I2C SDA line

Connect the SDA pin on the DS3231 to A4 on the Arduino. Analog pin A4 is the I2C data line pin of the Arduino. 

Step 4: Connect the Power line (Red wire)

Connect the Power line

Connect the VCC pin on the DS3231 to a 3.3 V power pin on the Arduino.

Step 5: The basic connection

The basic connection

You can now set the time and date and read the RTC module to get the current time information.

In the next step, we will connect the LCD to display the time on the LCD.

Let’s keep going!

Step 6: Complete connection with the LCD

Complete connection with the LCD

Here are the steps to connect an I2C 16×2 LCD to an Arduino:

  1. Connect the VCC pin on the LCD to a 5V power pin on the Arduino.
  2. Connect the GND pin on the LCD to a GND pin on the Arduino.
  3. Connect the SDA pin on the LCD to the A4 on the Arduino.
  4. Connect the SCL pin on the LCD to the A5 on the Arduino.

Congratulations! This completes all the required connections.

Arduino Code Example For The RTC DS2321 Module Project

In this section, you can find the complete Arduino sketch and the information on installing the necessary libraries. 

The complete Arduino Code For The DS3231 Arduino Project Example

#include "Wire.h"                   // for I2C communication
#include "LiquidCrystal_I2C.h"      // for LCD
#include "RTClib.h"                 // for RTC

LiquidCrystal_I2C lcd(0x27, 16, 2); // create LCD with I2C address 0x27, 16 characters per line, 2 lines
RTC_DS3231 rtc;                     // create rtc for the DS3231 RTC module, address is fixed at 0x68

void setup()
{
  Serial.begin(9600); // initialize serial
  lcd.init();       // initialize lcd
  lcd.backlight();  // switch-on lcd backlight
  rtc.begin();       // initialize rtc
}

void loop()
{
  updateLCD();  // update LCD text

  if (Serial.available()) {
    char input = Serial.read();
    if (input == 'u') updateRTC();  // update RTC time
  }
}

/*
   function to update RTC time using user input
*/
void updateRTC()
{
  lcd.clear();  // clear LCD display
  lcd.setCursor(0, 0);
  lcd.print("Edit Mode...");

  // ask user to enter new date and time
  const char txt[6][15] = { "year [4-digit]", "month [1~12]", "day [1~31]",
                            "hours [0~23]", "minutes [0~59]", "seconds [0~59]"
                          };
  String str = "";
  long newDate[6];

  while (Serial.available()) {
    Serial.read();  // clear serial buffer
  }

  for (int i = 0; i < 6; i++) {
    Serial.print("Enter ");
    Serial.print(txt[i]);
    Serial.print(": ");

    while (!Serial.available()) {
      ; // wait for user input
    }

    str = Serial.readString();  // read user input
    newDate[i] = str.toInt();   // convert user input to number and save to array

    Serial.println(newDate[i]); // show user input
  }

  // update RTC
  rtc.adjust(DateTime(newDate[0], newDate[1], newDate[2], newDate[3], newDate[4], newDate[5]));
  Serial.println("RTC Updated!");
}

/*
   function to update LCD text
*/
void updateLCD()
{
  /*
     create array to convert digit days to words:
     0 = Sunday    |   4 = Thursday
     1 = Monday    |   5 = Friday
     2 = Tuesday   |   6 = Saturday
     3 = Wednesday |
  */
  const char dayInWords[7][4] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};


  /*
     create array to convert digit months to words:
     0 = [no use]  |
     1 = January   |   6 = June
     2 = February  |   7 = July
     3 = March     |   8 = August
     4 = April     |   9 = September
     5 = May       |   10 = October
     6 = June      |   11 = November
     7 = July      |   12 = December
  */
  const char monthInWords[13][4] = {" ", "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
                                    "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"
                                   };

  // get time and date from RTC and save in variables
  DateTime rtcTime = rtc.now();

  int ss = rtcTime.second();
  int mm = rtcTime.minute();
  int hh = rtcTime.twelveHour();
  int DD = rtcTime.dayOfTheWeek();
  int dd = rtcTime.day();
  int MM = rtcTime.month();
  int yyyy = rtcTime.year();

  // move LCD cursor to upper-left position
  lcd.setCursor(0, 0);

  // print date in dd-MMM-yyyy format and day of week
  if (dd < 10) lcd.print("0");  // add preceeding '0' if number is less than 10
  lcd.print(dd);
  lcd.print("-");
  lcd.print(monthInWords[MM]);
  lcd.print("-");
  lcd.print(yyyy);

  lcd.print("  ");
  lcd.print(dayInWords[DD]);

  // move LCD cursor to lower-left position
  lcd.setCursor(0, 1);

  // print time in 12H format
  if (hh < 10) lcd.print("0");
  lcd.print(hh);
  lcd.print(':');

  if (mm < 10) lcd.print("0");
  lcd.print(mm);
  lcd.print(':');

  if (ss < 10) lcd.print("0");
  lcd.print(ss);

  if (rtcTime.isPM()) lcd.print(" PM"); // print AM/PM indication
  else lcd.print(" AM");
}

FAQs About The RTC Module And The Arduino UNO Projects

I have included a list of the most frequently asked questions about projects built using Arduino and the RTC module – DS3231.

If you have more questions, please post them in the comments section.

I will be happy to answer them.

1. What is the DS3231 RTC module?

The DS3231 is a low-cost, highly accurate real-time clock (RTC) module with a number of useful features.

It is commonly used in electronic projects and devices that require accurate timekeeping, such as alarms, timers, and data logging systems.

2. What makes the DS3231 accurate?

The DS3231 has a precision of +/- 2 minutes per year, which is significantly better than many other RTCs on the market.

This accuracy is achieved through the use of an integrated temperature-compensated crystal oscillator (TCXO).

Higher precision means lesser maintenance and more accurate measurements. 

3. What other features does the DS3231 have?

In addition to its high accuracy, the DS3231 also has a built-in battery backup that allows you to keep track of the time even when the main power source is disconnected.

It also has programmable alarms and timers that can be used to trigger events at specific times or intervals.

With creative ideas, you can make a highly reliable time logging system.

4. How is the DS3231 connected to an Arduino?

The DS3231 communicates with a microcontroller using the I2C protocol, which only requires two wires for communication.

This makes it easy to connect the DS3231 to an Arduino or other microcontroller.

I2C protocol brings down the number of cables to a  minimum while not sacrificing the required performance.

5. Are there any libraries or resources available for using the DS3231 with an Arduino?

Yes, there are a number of libraries available that make it easy to use the DS3231 with an Arduino.

Example-1: DS3231

Example-2: GitHub – rodan/ds3231: arduino library for DS3231 RTC

These libraries provide functions for reading and writing to the DS3231, setting alarms and timers, and converting the time and date data into a format that is easy to use in your code.

Conclusion

In this article, we covered all the info you will need to get started using the RTC module DS3231 with Arduino, including its applications and features which makes it a go-to option for superior timekeeping projects. 

We also covered the step-by-step connection guide and working code examples to help you get started using the DS3231 RTC module.

This should help you get set up quickly with your next Arduino RTC project. 

We hope this article was useful in getting you up and running with the DS3231.

If you have any questions or feedback, we’d love to hear from you!

Let us know if there’s anything else you’d like us to cover in future articles, or if there’s anything we can do to improve this one.

Please remember to share the Arduino and the RTC DS3231 sensor article with your fellow Arduino enthusiasts.

Derek

Wednesday 29th of March 2023

found solution. line 19 should read LCD.BEGIN();

Derek

Wednesday 29th of March 2023

Line 19 LCD.INIT(); error message exit status 1

Compilation error: 'class LiquidCrystal_I2C' has no member named 'init'

Can you give me a fix for this please