DS3231 RTC

Yer bog-standard real-time clock

Hardware / Pinout

For basic operation, ignore the SQW & 32K pins. Connect the remaining four as follows:

  • Vin – depends on module (i.e. 5V / 3.3V)
  • GND – Arduino/ESP32 (GND)
  • SDA – Arduino Uno/Nano (A4) / ESP32 (GPIO 21)
  • SCL – Arduino Uno/Nano (A5) / ESP32 (GPIO 22)

For reference; the above pictured module uses a LR2032 3.6V coin battery.


Code

This is the bare bones to get the time.

There are other functions available which have not been shown.

// Display date & time to serial monitor
// 20/10/2023

#include <Wire.h>
#include <DS3231.h>

RTClib myRTC;

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

void loop() {
  //GET TIME AND DATE
  DateTime now = myRTC.now();

  //PRINT DATE AND TIME TO SERIAL MONITOR
  Serial.print(now.year());
  Serial.print("/");
  Serial.print(now.month());
  Serial.print("/");
  Serial.print(now.day());
  Serial.print(" - ");
  Serial.print(now.hour());
  Serial.print(":");
  Serial.print(now.minute());
  Serial.print(":");
  Serial.println(now.second());

  //WAIT ONE SECOND
  delay(1000);
}

If strange times are being produced then reset the clock with


Last updated: 20/10/2023