Tutorial: How to use DS3231 RTC in Arduino
Introduction
In this tutorial, we will learn how to use the DS3231 Real Time Clock (RTC) module.
Bill Of Materials
- DS3231 RTC module with coin cell battery inserted.
- Arduino Uno board, or any Arduino compatible boards.
- 0.96 OLED display.
- A breadboard and jumper wires for connecting the circuits.
Circuit Diagram
Hardware Instruction
- Connect the DS3231 VCC pin to Arduino Uno 5V pin.
- Connect the DS3231 GND pin to Arduino Uno GND pin.
- Connect the DS3231 SDA pin to Arduino Uno A4 pin.
- Connect the DS3231 SCL pin to Arduino Uno A5 pin.
- Connect the OLED VCC pin to Arduino Uno 5V pin.
- Connect the OLED GND pin to Arduino Uno GND pin.
- Connect the OLED SDA pin to Arduino Uno A4 pin.
- Connect the OLED SCL pin to Arduino Uno A5 pin.
- Copy and paste the source code below to Arduino IDE.
- Upload the sketch to the Arduino board.
- Please feel free the example source code and enjoy tinkering.
Video Demonstration
Call To Action
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.
Source Code
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}
Posts in this series
- How to Get Started with ATTiny85 in Arduino IDE
- Tutorial: How to use MFRC522 RFID module using Arduino
- SOS Flasher Using Millis Function with Enable Switch
- Tutorial: How to use 0.96 OLED - a small and cute display
- Tutorial: Getting Started with the NRF24L01 | How to use | Arduino
- Tutorial: How to use SIM800L GSM Module for Controlling Anything | Arduino
- Tutorial: How to use Keypad | Text Entry Mode | Arduino
- Tutorial: How to use 4x4 Keypad | Arduino
- Project Idea: Arduino Voltmeter
- Project Idea: Door Lock Security | Arduino
- Multitasking with Arduino | Relay Timer Controller | using millis
- Tutorial Understanding Blink Without Delay | How to millis
- Arduino Simple LCD Menu
- How to use SIM800L GSM Module using Arduino | Make or Answer Voice Calls
- Tutorial: How to Use Arduino Uno as HID | Part 2: Arduino Mouse Emulation
- Tutorial: How to Use Arduino Uno as HID | Part 1: Arduino Keyboard Emulation
- Tutorial: How to use SIM800L DTMF to Control Anything | Arduino
- Tutorial: Arduino EEPROM
- How to use SIM800L GSM Module | Arduino | Send and Receive SMS
- 16x2 LCD Menu for Arduino
- Tutorial: Arduino GPIO | How to use Arduino Pins
- MIT App Inventor for Arduino
- RC Car using L298N, HC-06, and Arduino Uno
- How to Use LCD Keypad Shield for Arduino
- How to Use Arduino Interrupts
- Project: Automatic Alcohol Dispenser
- TUTORIAL: How to use HC-SR04 Ultrasonic Sensor with Arduino
- Source Code: Astronomia Meme and Funeral Dance | melodies the Arduino way
- How to Get Started with L293D Motor Driver Shield with Arduino
- How to Get Started with L298N Motor Driver module using Arduino
- Part 2: Wav Music Player with Lyrics Using Arduino and SD Card
- Interfacing Infrared to Arduino Uno
- Part 1: Wav Music Player Using Arduino Uno and SD Card
- How to Interface Stepper Motor to Arduino Uno
- How To Play MP3 Files on Arduino from SD Card
- What is Arduino Software Serial
- How to Interface SD card to Arduino (without SD card shield)?
- Playing Melodies Using Arduino
- 8 Degrees Of Freedom (DOF) Robot Using Arduino Uno
- How to Interface PS2 Controller to Arduino Uno
- Part 3: DF Player Mini Tinkering with Arduino Nano and LCD
- How to Interface HC-06 to Arduino
- How to make a Remote Control RC car using Arduino and HC-06 bluetooth module
- Part 2: DF Player Mini Tinkering with Arduino Nano
- Part 1: DF Player Mini - a mini cheap mp3 player
No comments yet!