DS3231 Real-Time Clock Module with Arduino

Real-Time Clock with DS3231 Experiment

Introduction

In this experiment, we will use the DS3231 Real-Time Clock (RTC) module to keep track of the current time and date. This module communicates over I2C and provides precise timekeeping for projects that need to track time or display real-time data.

DS3231 Real-Time Clock Module

Components Needed

Circuit Setup

  1. Connect the VCC pin of the DS3231 to the 5V pin on the Arduino.
  2. Connect the GND pin to GND on the Arduino.
  3. Connect the SCL pin to pin A5 on the Arduino.
  4. Connect the SDA pin to pin A4 on the Arduino.

Ensure the DS3231 module is properly connected and powered before uploading the code.

DS3231 Circuit Setup

Code for DS3231 Real-Time Clock

Upload the following code to your Arduino to read and display the current time from the DS3231 RTC:


#include 
#include 

RTC_DS3231 rtc;

void setup() {
  Serial.begin(9600);
  
  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }
}

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();

  delay(1000);
}
            

Explanation

This code retrieves the current date and time from the DS3231 RTC module and displays it on the serial monitor. Here's a breakdown of the code:

Troubleshooting