Follow my blog with Bloglovin

The DS3231 is a very accurate & widely used Real Time Clock (RTC) module. This module is used in Arduino based projects. This module also works with Raspberry Pi. In this post, I will share how to make Real-time clock with temperature using the DS3231 RTC module & SSD1306 OLED display in Arduino UNO.

Components Used:(Click to Buy)

If you do not know how to set present clock time you can follow this Post.

How to Set Time on DS3231 Real Time Clock Module with Arduino UNO

SSD1306 is a very popular OLED display. In this tutorial we will use 128 by 64 pixel display.

DS3231 Module setup

First, you have to go to this link where you will find the DS3231 library in GitHub. There are many DS3231 libraries. But I only found this one working with getting the temperature from the sensor.

SSD1306 OLED Display Setup

Most useful & easy library of SSD1306 OLED display is provided by Adafruit. You have to use the following two libraries to set your display perfectly working.

Adafruit SSD1306 OLED library
Adafruit GFX library


You can also search these libraries inside Arduino IDE to install it.

Connect DS3231 & SSD1306 with Arduino UNO

Connect all the wires according to the following diagram.

Clock with Temperature using DS3231 & SSD1306 in Arduino

Code Explain

The code is a very simple one. It will display the present date time with temperature in the display. The library function will return integer values of the day of the week from 1 to 7 where 1 is for Sunday and 7 for Saturday. To display it as Sunday, Monday we are saving all the texts in an array. This is the same for the months too where we have saved 12 months in the array.

But in the array, the indexing starts from 0. So when we will display data we will index the previous index. For example, if DS3231 gives us index 5, we will print the value of index 4 of the array. We have done this in the code line no 32 & 34. We will use the century variable as false. If you are using this after the year 2099 you have to use it as true.

In line 47, we are sending parameter 1 to the function getTemperature() to get floating value with one digit after the decimal point. All the temperature is in the Celcius format.
In the line 40, Clock.getHour(h12, pm) passing two variable for the 12 hour format. But I found it only supports 24 hour format with this library.


Arduino Sketch

/*
https://somtips.com
https://youtube.com/somtips
*/
#include <Wire.h>
#include <DS3231.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#define OLED_ADDR   0x3C
DS3231 Clock;
bool Century=false,h12,pm;
char week[7][20] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
char mon[12][4] = {"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"};
Adafruit_SSD1306 display(-1);

void setup() {
   Serial.begin(9600);
   Wire.begin();
  delay(3000);
  display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
  display.clearDisplay();
  display.display();
 
}

void loop() {
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(30,0);
  display.println("Som  Tips");
  display.print(week[Clock.getDoW()-1]);
  display.print("  ");
  display.print(mon[Clock.getMonth(Century)-1]);
  display.print(" ");
  display.print(Clock.getDate());
  display.print("  20");
  display.println(Clock.getYear());
  display.print("    ");
  display.print(Clock.getHour(h12, pm));
  display.print(" : ");
  display.print(Clock.getMinute());
  display.print(" : ");
  display.print(Clock.getSecond());
  display.println(" HRS");
  display.print("    Temp: ");
  display.print(Clock.getTemperature(), 1);
  display.println("  C");
  display.display();
  delay(1000);
  
}

Output

Here you can see the output in the display.

Clock with Temperature using DS3231 & SSD1306 in ESP8266
httpv://www.youtube.com/watch?v=AtpI6dl8J3o

1 COMMENT

Comment Box