Arduino Tutorial: Real Time clock with DS3231 module

Hi friends, Its Nick again, welcome to today’s tutorial, In today’s video, we will be building a Real Time Clock with temperature monitoring capabilities using the DS3231 chip, the Arduino Mega and the Arduino LCD shield which will be used to display the information from the DS3231 module.

Arduino Project: Real-time clock (RTC) and temperature monitor using the DS3231 module.

The DS3231 module is made up of the DS3231 chip which is a cheap and accurate RTC chip capable of storing time and date information for years after being set, as long as the chip keeps getting power from an attached coin cell battery like the CR2032 battery. The Module even automatically makes variations for leap years in its memory once it is set. It works with either 5 or 3.3v voltage level and thus can be used easily with any platform provided it is I2c enabled as the module communicates with the microcontroller over I2C.

To add to its impressive list of attributes, the chip also comes with temperature measuring capabilities which makes it really versatile and useful.

Required Components/Parts and Where to Buy

The following components are required to build this project and they can be bought clicking on the link in front of each component.

1. Arduino Mega ▶ https://educ8s.tv/part/ArduinoMega

2. DS3231 RTC ▶ https://educ8s.tv/part/DS3231

3. Keypad display ▶ https://educ8s.tv/part/KeypadShield

4. Small breadboard ▶ https://educ8s.tv/part/SmallBreadboard

5.Wires ▶ https://educ8s.tv/part/Wires

 

Schematics

Connect the parts listed above as shown in the project schematics below;

Schematics

 

The connection between the 1602 LCD shield and the Arduino is very simple as all we need to do is to plug the module directly on the Arduino.


The pin connections between the DS3231 and the Arduino is however detailed below.

Ds3231 ▶ Arduino Mega

VCC   5v

GND   GND

SDA   D20/SDA

SCL   D21/SCL

 

Check the connections to ensure everything adds up, then proceed to the code. if you have any difficulty, feel free to reach out to me via the comment section.

Code

The code for this project is fairly easy, we will need the DS3231 library which can be downloaded from the link below for easy communication with the RTC chip. We will also need the liquid crystal library which comes with the Arduino IDE to interact easily with the LCD and the wire.h library for I2C communication.

——————–
LIBRARIES
——————–

DS3231 Library: https://github.com/rodan/ds3231

[adsense]

The goals of the project is simple, we want to be able to display the current date and time as reported to us by the DS3231 module on the LCD with the system updating itself automatically as the time and(or) temperature changes.

To begin, the first thing we do in the code, as usual, is to include the libraries that we will be using.

#include <Wire.h>
#include "ds3231.h"
#include "rtc_ds3231.h"
#include <LiquidCrystal.h>

Next, we create an object of the liquid crystal class and declare the pins to which the LCD is connected to on the Arduino. Next, we declare the global variables which will be used later in the code.

LiquidCrystal lcd(8,9,4,5,6,7); 

#define BUFF_MAX 128

uint8_t time[8];
char recv[BUFF_MAX];
unsigned int recv_size = 0;
unsigned long prev, interval = 1000;

Next, we write the void setup function which features the initialization of the serial communication, the I2C communication via the wire.h library and the LCD and the RTC itself.

void setup()
{
    Serial.begin(9600);
    Wire.begin();
    DS3231_init(DS3231_INTCN);
    memset(recv, 0, BUFF_MAX);
    Serial.println("GET time");
    lcd.begin(16, 2);
    lcd.clear();
    
    //Serial.println("Setting time");
    //parse_cmd("T302911604102014",16);
}

We also set the time and date of the RTC under the void setup function using the parse_cmd(“T302911604102014”,16) line of code. Do note that this line should be commented out after the time has been set.The time can be sent by uncommenting the line of code when the code is been uploaded to the Arduino board for the first time. you can take a look at the video to get more about setting the time.

Next, we move to the void loop function, which is responsible for the continuous update of the display with the correct time and temperature as supplied by the RTC.

void loop()
{
    char in;
    char tempF[6]; 
    float temperature;
    char buff[BUFF_MAX];
    unsigned long now = millis();
    struct ts t;

    // show time once in a while
    if ((now - prev > interval) && (Serial.available() <= 0)) {
        DS3231_get(&t); //Get time
        parse_cmd("C",1);
        temperature = DS3231_get_treg(); //Get temperature
        dtostrf(temperature, 5, 1, tempF);

        lcd.clear();
        lcd.setCursor(1,0);
        
        lcd.print(t.mday);
        
        printMonth(t.mon);
        
        lcd.print(t.year);
        
        lcd.setCursor(0,1); //Go to second line of the LCD Screen
        lcd.print(t.hour);
        lcd.print(":");
        if(t.min<10)
        {
          lcd.print("0");
        }
        lcd.print(t.min);
        lcd.print(":");
        if(t.sec<10)
        {
          lcd.print("0");
        }
        lcd.print(t.sec);
        
        lcd.print(' ');
        lcd.print(tempF);
        lcd.print((char)223);
        lcd.print("C ");
        prev = now;
    }

    
    if (Serial.available() > 0) {
        in = Serial.read();

        if ((in == 10 || in == 13) && (recv_size > 0)) {
            parse_cmd(recv, recv_size);
            recv_size = 0;
            recv[0] = 0;
        } else if (in < 48 || in > 122) {;       // ignore ~[0-9A-Za-z]
        } else if (recv_size > BUFF_MAX - 2) {   // drop lines that are too long
            // drop
            recv_size = 0;
            recv[0] = 0;
        } else if (recv_size < BUFF_MAX - 2) {
            recv[recv_size] = in;
            recv[recv_size + 1] = 0;
            recv_size += 1;
        }

    }
}

The full code for this project can be downloaded from the link below. Upload the code to your Arduino board and you should see the display come alive as shown in the image below.

RTC in Action

——————–

CODE OF THE PROJECT
——————–

 

 

That’s it for today’s tutorial, thanks for reading/watching. If you get stuck at any point while building this project, feel free to reach out to me, will be glad to answer whatever questions you might have. Don’t forget to share, like and subscribe on youtube. Thanks!

——————–
SUBSCRIBE ON YOUTUBE
——————–

Never miss a video: Subscribe to educ8s.tv