This content originally appeared on DEV Community and was authored by wangsheng
Hey, developer friends! Have you ever gotten fed up with timekeeping in your Arduino projects always being off? The built-in millis() function resets to zero after every restart, which is a total pain when building alarms, smart home setups, or data loggers. Luckily, the DS3231MZ+ low-power I2C real-time clock (RTC) chip makes it all super simple.
Today, I’ll walk you through building a project that displays the real-time date and time using Arduino and the DS3231MZ+ module. The whole process is beginner-friendly and straightforward. Ready? Let’s dive in!
Why Choose DS3231MZ+?
The DS3231MZ+ is a precision RTC chip from Maxim Integrated (now Analog Devices), featuring a temperature-compensated crystal oscillator with accuracy up to ±2ppm (that’s less than 1 minute of drift per year). It supports I2C interface, has ultra-low power consumption (just 3μA in standby), and even reads temperature. Compared to the DS1307, it’s way more stable, especially in varying temperatures.
In module form (like the common ZS-042 board), it includes a battery backup interface—pop in a CR2032 battery, and time keeps ticking even after a power loss!
What You’ll Need
Arduino Uno (or compatible board)
DS3231MZ+ RTC module (a few bucks on Taobao/AliExpress)
16×2 LCD display (optional; Serial print works too)
Breadboard and jumper wires
CR2032 coin cell battery (for backup)
Total cost? Under $3. Perfect!
Step 1: Hardware Connections
The DS3231MZ+ uses I2C communication, so it’s dead simple. Just four wires:
DS3231MZ+ VCC to Arduino 5V (power).
DS3231MZ+ GND to Arduino GND (ground).
DS3231MZ+ SDA to Arduino A4 (data line).
DS3231MZ+ SCL to Arduino A5 (clock line).
If using an LCD, connect it to LiquidCrystal library pins (e.g., RS=12, EN=11, D4=5, D5=4, D6=3, D7=2).
Insert the battery, and the module becomes “immortal”—time won’t lose even if the Arduino powers down.
Step 2: Install Libraries and Code
In the Arduino IDE, install the Adafruit RTClib library (Tools > Manage Libraries > search “RTClib”).
Here’s the full code: It sets the time, displays date/time, and reads temperature. Upload it, then open the Serial Monitor (9600 baud) to see the magic.
#include <Wire.h>
#include "RTClib.h"
RTC_DS3231 rtc;
void setup() {
Serial.begin(9600);
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
// If RTC lost power, set the current time (format: year,month,day,hour,minute,second,weekday=0)
if (rtc.lostPower()) {
Serial.println("RTC lost power, setting the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // Auto-sets to compile time
}
}
void loop() {
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
// Read temperature (Celsius)
Serial.print("Temperature: ");
Serial.print(rtc.getTemperature());
Serial.println("C");
delay(1000);
}
Quick breakdown of key parts:
rtc.begin() initializes I2C.
rtc.adjust() sets the time only on first run (using compile time).
rtc.now() grabs the current DateTime object.
Temperature via getTemperature() is a breeze, with ±3°C accuracy.
Run it, and you’ll see output like:
2025/11/03 14:30:45
Temperature: 23.5C
Step 3: Testing and Extensions
Upload the code, restart your Arduino—the time should be spot-on! Now, level it up with these cool extensions:
Add LCD Display: Swap Serial for LCD.print() with the LiquidCrystal library.
Alarm Feature: Use rtc.setAlarm1() to trigger interrupts on a buzzer pin for alarms.
Data Logging: Pair with an SD card module to timestamp sensor data.
ESP32 Upgrade: Switch to ESP32 and sync with NTP over WiFi for auto-correction.
Troubleshooting? Check the I2C address (default 0x68; use an I2C Scanner sketch to verify).
Wrapping Up
With the DS3231MZ+, your Arduino projects go pro in an instant. This tiny chip isn’t just accurate—it’s power-efficient and hassle-free. Next time you’re building a weather station or logger, don’t forget it!
What’s your wildest RTC project? Drop your code or time-sync horror stories in the comments. Like, share, follow—let’s geek out on embedded stuff together!
This content originally appeared on DEV Community and was authored by wangsheng

