Connect DS3231 real time clock to Arduino NANO

DS1302, DS1307, DS3231 is a real-time modules for Arduino and Raspberry PI. Let’s connect DS3231 to our Arduino NANO.

To work with this module we will use universal library: iarduino_RTC (https://github.com/tremaru/iarduino_RTC).

Download latest version of this library you can here: https://github.com/tremaru/iarduino_RTC/archive/refs/tags/1.3.4.zip

First of all connect DS3231 module to Arduino:

GND (-) --> GND
VCC (+) --> +5V
SDA (D) --> A4
SCL (C) --> A5

Include library and initialize module:

#include <iarduino_RTC.h>
iarduino_RTC time(RTC_DS1307);

To set current time in DS3231 module tuesday write next functions in function setup():

void setup() {
    Serial.begin(9600);
    time.begin();
    time.settime(0,20,17,30,3,21,2);  
    // 0  sec, 20 min, 17 hour, 30 day, 3 mon, 2021 year, tuesday
}

In function loop() let’s write to serial port current time:

void loop(){
    if(millis() % 1000 == 0) {
        Serial.println(time.gettime("d-m-Y, H:i:s, D"));
        delay(1);
    }
}

This sketch you need to run once time, when configure DS3231 module.

If you want just to show current time – use this source code:

#include 
iarduino_RTC time(RTC_DS1307);

void setup() {
    Serial.begin(9600);
}
void loop() {
    if(millis() % 1000 == 0) {
        Serial.println(time.gettime("d-m-Y, H:i:s, D"));
        delay(1);
    }
}