Arduino Nano ESP32 - RTC

In this guide, we are going to learn how to use Arduino Nano ESP32 with DS3231 Real-Time Clock Module. In detail, we'll cover the following topics:

Hardware Preparation

1×Arduino Nano ESP32
1×USB Cable Type-C
1×Real-Time Clock DS3231 Module
1×CR2032 battery
1×Jumper Wires
1×Breadboard
1×(Optional) DC Power Jack
1×(Recommended) Screw Terminal Expansion Board for Arduino Nano
1×(Recommended) Breakout Expansion Board for Arduino Nano
1×(Recommended) Power Splitter For Arduino Nano ESP32

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Real-Time Clock DS3231 Module

Arduino Nano ESP32 itself has some time-related functions such as millis(), micros(). However, they can not provide the date and time (seconds, minutes, hours, day, date, month, and year). To get date and time, we needs to use a Real-Time Clock (RTC) module such as DS3231, DS1370. DS3231 Module has higher precision than DS1370. See DS3231 vs DS1307

Pinout

Real-Time Clock DS3231 Module includes 10 pins:

  • 32K pin: outputs the stable(temperature compensated) and accurate reference clock.
  • SQW pin: outputs a nice square wave at either 1Hz, 4kHz, 8kHz or 32kHz and can be handled programmatically. This can further be used as an interrupt due to alarm condition in many time-based applications.
  • SCL pin: is a clock pin for I2C interface.
  • SDA pin: is a data pin for I2C interface.
  • VCC pin: supplies power for the module. It can be anywhere between 3.3V to 5.5V.
  • GND pin: is a ground pin.

For normal use, it needs to use 4 pins: VCC, GND, SDA, SCL

Real-Time Clock DS3231 Module Pinout

The DS3231 Module also has a battery holder.

  • If we insert a CR2032 battery, It keeps the time on module running when the main power is off.
  • If we do not insert the battery, the time information is lost if the main power is off and you need to set the time again.

Wiring Diagram

The wiring diagram between Arduino Nano ESP32 and Real-Time Clock DS3231

This image is created using Fritzing. Click to enlarge image

Arduino Nano ESP32 - DS3231 RTC Module

DS1307 RTC Module Arduino Nano ESP32
Vin 3.3V
GND GND
SDA A4
SCL A5

How To Program For DS3231 RTC Module

  • Include the library:
#include <RTClib.h>
  • Declare a RTC object:
RTC_DS3231 rtc;
  • Initialize RTC:
if (! rtc.begin()) { Serial.println("RTC module is NOT found"); while (1); }
  • For the first time, set the RTC to the date & time on PC the sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  • Reads date and time information from RTC module
DateTime now = rtc.now(); Serial.print("Arduino Nano ESP32 RTC Date Time: "); Serial.print(now.year(), DEC); Serial.print('/'); Serial.print(now.month(), DEC); Serial.print('/'); Serial.print(now.day(), DEC); Serial.print(" ("); Serial.print(now.dayOfTheWeek()); Serial.print(") "); Serial.print(now.hour(), DEC); Serial.print(':'); Serial.print(now.minute(), DEC); Serial.print(':'); Serial.println(now.second(), DEC);

Arduino Nano ESP32 Code – How to get data and time

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-rtc */ #include <RTClib.h> RTC_DS3231 rtc; char daysOfWeek[7][12] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; void setup () { Serial.begin(9600); // SETUP RTC MODULE if (! rtc.begin()) { Serial.println("RTC module is NOT found"); Serial.flush(); while (1); } // automatically sets the RTC to the date & time on PC this sketch was compiled rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // manually sets the RTC with an explicit date & time, for example to set // January 21, 2021 at 3am you would call: // rtc.adjust(DateTime(2021, 1, 21, 3, 0, 0)); } void loop () { DateTime now = rtc.now(); Serial.print("ESP32 RTC Date Time: "); Serial.print(now.year(), DEC); Serial.print('/'); Serial.print(now.month(), DEC); Serial.print('/'); Serial.print(now.day(), DEC); Serial.print(" ("); Serial.print(daysOfWeek[now.dayOfTheWeek()]); Serial.print(") "); Serial.print(now.hour(), DEC); Serial.print(':'); Serial.print(now.minute(), DEC); Serial.print(':'); Serial.println(now.second(), DEC); delay(1000); // delay 1 seconds }

Detailed Instructions

To get started with Arduino Nano ESP32, follow these steps:

  • If you are new to Arduino Nano ESP32, refer to the tutorial on how to set up the environment for Arduino Nano ESP32 in the Arduino IDE.
  • Wire the components according to the provided diagram.
  • Connect the Arduino Nano ESP32 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano ESP32 board and its corresponding COM port.
  • Open the Library Manager by clicking on the Library Manager icon on the left navigation bar of Arduino IDE.
  • Search “RTClib”, then find the RTC library by Adafruit
  • Click Install button to install RTC library.
Arduino Nano ESP32 RTC library
  • A windows may appear to ask you to install dependencies for the library
  • Install all dependencies for the library by clicking on Install All button.
Arduino Nano ESP32 Adafruit BusIO library
  • Copy the above code and paste it to Arduino IDE
  • Compile and upload code to Arduino Nano ESP32 board by clicking Upload button on Arduino IDE
  • Open Serial Monitor on Arduino IDE
  • See the output on Serial Monitor.
COM6
Send
Arduino Nano ESP32 RTC Date Time: 2022/08/26 (Friday) 15:21:35 Arduino Nano ESP32 RTC Date Time: 2022/08/26 (Friday) 15:21:36 Arduino Nano ESP32 RTC Date Time: 2022/08/26 (Friday) 15:21:37 Arduino Nano ESP32 RTC Date Time: 2022/08/26 (Friday) 15:21:38 Arduino Nano ESP32 RTC Date Time: 2022/08/26 (Friday) 15:21:39 Arduino Nano ESP32 RTC Date Time: 2022/08/26 (Friday) 15:21:40 Arduino Nano ESP32 RTC Date Time: 2022/08/26 (Friday) 15:21:41 Arduino Nano ESP32 RTC Date Time: 2022/08/26 (Friday) 15:21:42 Arduino Nano ESP32 RTC Date Time: 2022/08/26 (Friday) 15:21:43 Arduino Nano ESP32 RTC Date Time: 2022/08/26 (Friday) 15:21:44
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!