🕒 Arduino DS3231 Real-Time Clock (RTC) Interface
📘 Overview
This project demonstrates how to read and write real-time data (time and date) to the DS3231 RTC module using the Arduino Wire (I²C) library. The DS3231 is a highly accurate clock module with an integrated temperature-compensated crystal oscillator (TCXO) and battery backup, ideal for timekeeping even when power is lost.
🧩 Components Required
Arduino UNO or Nano
DS3231 RTC Module
Jumper wires
(Optional) CR2032 backup battery for the RTC module
⚙️ Circuit Connections DS3231 Pin Function Arduino UNO Pin VCC +5V 5V GND Ground GND SDA I²C Data A4 SCL I²C Clock A5
Note: The DS3231 operates at 3.3V to 5V, so it can safely interface with Arduino UNO.
💻 Arduino Code
#include <Wire.h>
void setup()
{
Wire.begin();
Serial.begin(9600);
// Uncomment the below line once to set initial time & date
// DS3231_Write(00, 00, 00, 4, 01, 01, 26); // sec, min, hour, day(1=Mon), date, month, year(YY)
}
void loop()
{
delay(1000);
Serial.println("---------------");
Serial.print("Time-");
Serial.print(DS3231_Read(0x02)); Serial.print(':');
Serial.print(DS3231_Read(0x01)); Serial.print(':');
Serial.println(DS3231_Read(0x00));
Serial.print("Date-");
Serial.print(DS3231_Read(0x04)); Serial.print('/');
Serial.print(DS3231_Read(0x05)); Serial.print('/');
Serial.println(DS3231_Read(0x06));
Serial.println("---------------");
}
char decimal_to_bcd(unsigned char value)
{
unsigned char msb, lsb, hex;
msb = value / 10;
lsb = value % 10;
hex = ((msb << 4) + lsb);
return hex;
}
void DS3231_Write(char _second, char _minute, char _hour, char _day, char _date, char _month, char _year)
{
Wire.beginTransmission(0x68);
Wire.write(0x00);
Wire.write(decimal_to_bcd(_second));
Wire.write(decimal_to_bcd(_minute));
Wire.write(decimal_to_bcd(_hour));
Wire.write(decimal_to_bcd(_day));
Wire.write(decimal_to_bcd(_date));
Wire.write(decimal_to_bcd(_month));
Wire.write(decimal_to_bcd(_year));
Wire.endTransmission();
}
uint8_t DS3231_Read(uint8_t addr)
{
byte data;
Wire.beginTransmission(0x68);
Wire.write(addr);
Wire.endTransmission();
delay(5);
Wire.requestFrom(0x68, 1);
delay(10);
if (Wire.available())
{
data = Wire.read();
data = ((data >> 4) * 10) + (data & 0x0F);
}
return data;
}🧠 How It Works
I²C Communication: The DS3231 uses I²C address 0x68. Arduino’s Wire library handles all low-level communication.
BCD (Binary-Coded Decimal) Conversion: The RTC stores data in BCD format.
Example: 0x25 → 25 seconds. The decimal_to_bcd() and decoding in DS3231_Read() handle conversions.
Write Operation: The function DS3231_Write() sets the initial time and date (uncomment and modify once).
Read Operation: Each byte is read from RTC registers and converted to decimal for display via Serial Monitor.
🧾 DS3231 Register Map (for reference)
Register Address Function Description
0x00 Seconds 00–59
0x01 Minutes 00–59
0x02 Hours 00–23
0x03 Day 1=Mon, 7=Sun
0x04 Date 01–31
0x05 Month 01–12
0x06 Year 00–99
Run DS3231_Write() only once to set the time. Then comment it out to prevent overwriting.
Ensure your Serial Monitor is set to 9600 baud.
DS3231 keeps accurate time even when Arduino is powered off (if backup battery is inserted).
🚀 Next Steps
Display date and time on a 16x2 LCD or OLED (SSD1306).
Add alarm functionality using DS3231’s built-in alarm registers.
Log timestamped sensor data to an SD card.