Tutorial: How to use DS3231 RTC in Arduino

In this tutorial, we will learn how to use the DS3231 Real Time Clock (RTC) module.

  1. DS3231 RTC module with coin cell battery inserted.
  2. Arduino Uno board, or any Arduino compatible boards.
  3. 0.96 OLED display.
  4. A breadboard and jumper wires for connecting the circuits.

  1. Connect the DS3231 VCC pin to Arduino Uno 5V pin.
  2. Connect the DS3231 GND pin to Arduino Uno GND pin.
  3. Connect the DS3231 SDA pin to Arduino Uno A4 pin.
  4. Connect the DS3231 SCL pin to Arduino Uno A5 pin.
  5. Connect the OLED VCC pin to Arduino Uno 5V pin.
  6. Connect the OLED GND pin to Arduino Uno GND pin.
  7. Connect the OLED SDA pin to Arduino Uno A4 pin.
  8. Connect the OLED SCL pin to Arduino Uno A5 pin.
  9. Copy and paste the source code below to Arduino IDE.
  10. Upload the sketch to the Arduino board.
  11. Please feel free the example source code and enjoy tinkering.

If you have any question regarding this tutorial, please do not hesitate to write it in the comment box.

If you enjoy this tutorial, please consider supporting me by Subscribing. Click this to Subscribe.

Thank you and stay safe always.

  1#include "Adafruit_GFX.h"
  2#include "Adafruit_SSD1306.h"
  3#include "RTClib.h"
  4
  5RTC_DS3231 rtc;
  6char daysOfTheWeek[7][4] = {"SUN", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"};
  7
  8// OLED display size in pixels
  9#define SCREEN_WIDTH 128 
 10#define SCREEN_HEIGHT 64 
 11
 12#define OLED_RESET -1
 13Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
 14DateTime now;
 15
 16void setup() {
 17  Serial.begin(9600);
 18  delay(3000); // wait for console opening
 19
 20  initializeRTC();  
 21  initializeOLED();
 22}
 23
 24void loop() {
 25  now = rtc.now();
 26  displayDay();
 27  displayDate();
 28  displayTime();
 29  displayTemperature();
 30}
 31
 32void initializeRTC() {
 33  if (! rtc.begin()) {
 34    Serial.println("Couldn't find RTC");
 35    while (1);
 36  }
 37
 38  if (rtc.lostPower()) {
 39    Serial.println("RTC lost power, lets set the time!");
 40    // following line sets the RTC to the date & time this sketch was compiled
 41    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
 42    // This line sets the RTC with an explicit date & time, for example to set
 43    // January 21, 2014 at 3am you would call:
 44    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
 45  }  
 46}
 47
 48void initializeOLED() {
 49  oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
 50  oled.clearDisplay();
 51  oled.display(); 
 52  oled.setTextColor(WHITE, BLACK);
 53  oled.setTextSize(1);
 54  
 55  oled.setCursor(117,55);
 56  // Enable the Code Page 437
 57  // https://en.wikipedia.org/wiki/Code_page_437
 58  oled.cp437(true);
 59  // Then print the chosen characters
 60  // which in this case is the degrees symbol
 61  // https://www.ascii-codes.com/
 62  oled.write(248);
 63
 64  oled.setCursor(0,55);
 65  oled.print("TEMPERATURE =");
 66  oled.setCursor(122,55);
 67  oled.print("C");
 68}
 69
 70void displayDay() {
 71  oled.setTextSize(1);
 72  oled.setCursor(5,15);
 73  oled.print(daysOfTheWeek[now.dayOfTheWeek()]);
 74}
 75
 76void displayDate() {
 77  char currentDate [16];
 78  uint8_t thisDay, thisMonth ;
 79  thisDay = now.day();
 80  thisMonth = now.month();
 81  sprintf (currentDate, "%02d/%02d/", thisDay, thisMonth); //add leading zeros to the day and month
 82  
 83  oled.setTextSize(1);
 84  oled.setCursor(62,15);
 85  oled.print(currentDate);
 86  
 87  oled.setTextSize(1);
 88  oled.setCursor(102,15);
 89  oled.print(now.year(), DEC);  
 90}
 91
 92void displayTime() {
 93  char buffer [16];
 94  char AmPm[3];
 95  uint8_t thisSec, thisMin, thisHour;
 96  thisSec = now.second();
 97  thisMin = now.minute();
 98  thisHour = now.hour();
 99  
100  if (thisHour > 12) {
101    thisHour = thisHour - 12;
102    strcpy(AmPm,"PM");
103  } else strcpy(AmPm,"AM");
104  
105  sprintf (buffer, "%02d:%02d:%02d", thisHour, thisMin, thisSec);
106  
107  oled.setTextSize(2);
108  oled.setCursor(6,32);
109  oled.print(buffer);
110  
111  oled.setTextSize(1);
112  oled.setCursor(110,32);
113  oled.print(AmPm);
114}
115
116void displayTemperature() {
117  oled.setTextSize(1);
118  oled.setCursor(82,55);
119  oled.print(rtc.getTemperature());
120  oled.display(); 
121}
...
cpp


Posts in this series



GitHub-flavored Markdown & a sane subset of HTML is supported.