How to simply use DS3231 RTC module with Arduino + OLED display
Hello and welcome to this tutorial about DS3231 Real time clock module, we gonna interface it with an Arduino board, I’m using Arduino UNO as usual but today instead of the over-used (in this website/channel) LCD screen, I’m using 128*32 OLED i2c display which is small and classy for little projects.
Before continuing, you can check my previous tutorials and projects about another RTC module which is the DS1302 :
- How to simply use DS1302 RTC module with Arduino board and LCD screen
- How to setup DS1302 RTC module with Keypad + Arduino + LCD
- Simple Arduino based Alarm Clock with DS1302 RTC
Or if you’re not familiar with the OLED display:Â Use OLED display + Arduino with examples
If you checked my previous DS1302 tutorials or tested it, you gonna notice that it isn’t very reliable as a module and has few problems, like if you’re using it for a long time you will notice that the time is late by few minutes which is a big problem if you need precision in your projects.
But for the one I’m using today, it is also a low cost module but it’s claimed to be “extremely accurate i²c module”, and thanks to its oscillator which is “Temperature compensated crystal oscillator (TCXO) and crystal”, also a good monitoring of the main power, when it goes off the battery automatically start to keep the time, and by my little experience with this module it’s very accurate and easy to use also, don’t forget that it has a “bonus” built in temperature sensor that can inform you of the ambient temperature which can come very handy in projects.
DS3231 Module pinout with Arduino UNO board
Wiring:
Both modules use i²c bus so the SDA and SCL for both are on A4 and A5 pins on the Arduino Uno board, those pins have double terminals as you can see, then for power the RTC uses the 5V while the OLED uses the 3.3V, and yeah I might think that using a 5V to 3.3V converter for SDA/SCL is better for the OLED.
Libraries:
- DS3231 RTC library:Â Download from Github, or Download it here. A massive thanks to the creator who made it very simple and easy to use.
If you have a later version of Arduino IDE than 1.8.5 you may need to check this library
Download here, thanks to Russell Gililland for mentionning the issue.
OLED libraries:
Codes:
Code 1: Library example – Simple
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
/* DS3231: Real-Time Clock. Simple example (c) 2014 by Korneliusz Jarzebski */ #include <Wire.h> #include <DS3231.h> DS3231 clock; RTCDateTime dt; void setup() { Serial.begin(9600); // Initialize DS3231 Serial.println("Initialize DS3231");; clock.begin(); // Set sketch compiling time clock.setDateTime(__DATE__, __TIME__); } void loop() { dt = clock.getDateTime(); // For leading zero look to DS3231_dateformat example Serial.print("Raw data: "); Serial.print(dt.year); Serial.print("-"); Serial.print(dt.month); Serial.print("-"); Serial.print(dt.day); Serial.print(" "); Serial.print(dt.hour); Serial.print(":"); Serial.print(dt.minute); Serial.print(":"); Serial.print(dt.second); Serial.println(""); delay(1000); } |
Code 2: Library example – Dateformat
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
/* DS3231: Real-Time Clock. Date Format (c) 2014 by Korneliusz Jarzebski */ #include <Wire.h> #include <DS3231.h> DS3231 clock; RTCDateTime dt; void setup() { Serial.begin(9600); // Initialize DS3231 Serial.println("Initialize DS3231");; clock.begin(); // Set sketch compiling time clock.setDateTime(__DATE__, __TIME__); // Set from UNIX timestamp // clock.setDateTime(1397408400); // Manual (YYYY, MM, DD, HH, II, SS // clock.setDateTime(2014, 4, 13, 19, 21, 00); } void loop() { dt = clock.getDateTime(); Serial.print("Long number format: "); Serial.println(clock.dateFormat("d-m-Y H:i:s", dt)); Serial.print("Long format with month name: "); Serial.println(clock.dateFormat("d F Y H:i:s", dt)); Serial.print("Short format witch 12h mode: "); Serial.println(clock.dateFormat("jS M y, h:ia", dt)); Serial.print("Today is: "); Serial.print(clock.dateFormat("l, z", dt)); Serial.println(" days of the year."); Serial.print("Actual month has: "); Serial.print(clock.dateFormat("t", dt)); Serial.println(" days."); Serial.print("Unixtime: "); Serial.println(clock.dateFormat("U", dt)); Serial.println(); delay(1000); } |
Code 3: Library example – Temperature
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
/* DS3231: Real-Time Clock. Temperature example (c) 2014 by Korneliusz Jarzebski */ #include <Wire.h> #include <DS3231.h> DS3231 clock; RTCDateTime dt; void setup() { Serial.begin(9600); // Initialize DS3231 Serial.println("Initialize DS3231");; clock.begin(); } void loop() { // The temperature registers are updated after every 64-second conversion. // If you want force temperature conversion use forceConversion() clock.forceConversion(); Serial.print("Temperature: "); Serial.println(clock.readTemperature()); delay(1000); } |
The other ones that I’ve used for the OLED display: Download here (Don’t forget these codes don’t setup the date and time, so it’s better to run the “Simple” example first).
Codes 4: This is a set of 8 different codes that I used in the video
Type 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
/* This code works with DS3231 RTC module and OLED display * It shows a format of date and time in the OLED screen and some cases the temperature too * Refer to http://www.Surtrtech.com for more details * This is Code #1 of the tutorial */ #include <SPI.h> //i2c and the display libraries + DS3231 #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <DS3231.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 32 // OLED display height, in pixels #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //Declaring the display name (display) DS3231 clock; RTCDateTime dt; void setup() { display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Start the OLED display clock.begin(); delay(1000); display.clearDisplay(); display.display(); } void loop() { //This code displays the Time only hours:minutes dt = clock.getDateTime(); display.clearDisplay(); display.setTextSize(3); //size of the text that will follow display.setTextColor(WHITE); //its color display.setCursor(1,1); //position from where you want to start writing display.print(dt.hour); //text todisplay display.print(":"); display.print(dt.minute); display.display(); } |
Type 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
/* This code works with DS3231 RTC module and OLED display * It shows a format of date and time in the OLED screen and some cases the temperature too * Refer to http://www.Surtrtech.com for more details * This is Code #2 of the tutorial */ #include <SPI.h> //i2c and the display libraries #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <DS3231.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 32 // OLED display height, in pixels #define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //Declaring the display name (display) DS3231 clock; RTCDateTime dt; void setup() { display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Start the OLED display clock.begin(); delay(1000); display.clearDisplay(); display.display(); } void loop() { //This code diplays the Time (Hours:Minutes;Seconds) dt = clock.getDateTime(); display.clearDisplay(); display.setTextSize(2); //size of the text that will follow display.setTextColor(WHITE); //its color display.setCursor(1,1); //position from where you want to start writing display.print(dt.hour); //text todisplay display.print(":"); display.print(dt.minute); display.print(":"); display.print(dt.second); display.display(); delay(1000); //Refresh every second } |
Type 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
/* This code works with DS3231 RTC module and OLED display * It shows a format of date and time in the OLED screen and some cases the temperature too * Refer to http://www.Surtrtech.com for more details * This is Code #3 of the tutorial */ #include <SPI.h> //i2c and the display libraries #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <DS3231.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 32 // OLED display height, in pixels #define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //Declaring the display name (display) DS3231 clock; RTCDateTime dt; void setup() { display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Start the OLED display clock.begin(); delay(1000); display.clearDisplay(); display.display(); } void loop() { //This code displays the Time (Hours:Minutes) and the Date (Day/Month) dt = clock.getDateTime(); display.clearDisplay(); display.setTextSize(3); //size of the text that will follow display.setTextColor(WHITE); //its color display.setCursor(1,1); //position from where you want to start writing display.print(clock.dateFormat("H:i",dt)); //text todisplay display.setCursor(100,1); display.setTextSize(2); display.println(dt.day); display.setCursor(100,20); display.print(dt.month); display.display(); delay(1000); } |
Type 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
/* This code works with DS3231 RTC module and OLED display * It shows a format of date and time in the OLED screen and some cases the temperature too * Refer to http://www.Surtrtech.com for more details * This is Code #4 of the tutorial */ #include <SPI.h> //i2c and the display libraries #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <DS3231.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 32 // OLED display height, in pixels #define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //Declaring the display name (display) DS3231 clock; RTCDateTime dt; void setup() { display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Start the OLED display clock.begin(); delay(1000); display.clearDisplay(); display.display(); } void loop() { //This code displays the Time (Hours:Minutes) and Date (Day of week/Day of month) dt = clock.getDateTime(); display.clearDisplay(); display.setTextSize(3); //size of the text that will follow display.setTextColor(WHITE); //its color display.setCursor(1,1); //position from where you want to start writing display.print(clock.dateFormat("H:i", dt)); //text todisplay display.setTextSize(1); display.setCursor(100,1); display.print(clock.dateFormat("D", dt)); display.setTextSize(2); display.setCursor(100,10); display.print(clock.dateFormat("d", dt)); display.setCursor(100,25); display.display(); delay(1000); } |
Type 4_1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
/* This code works with DS3231 RTC module and OLED display * It shows a format of date and time in the OLED screen and some cases the temperature too * Refer to http://www.Surtrtech.com for more details * This is Code #4_v2 of the tutorial */ #include <SPI.h> //i2c and the display libraries #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <DS3231.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 32 // OLED display height, in pixels #define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin) int a; Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //Declaring the display name (display) DS3231 clock; RTCDateTime dt; void setup() { display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Start the OLED display clock.begin(); delay(1000); display.clearDisplay(); display.display(); a=millis(); } void loop() { //This code displays the Time (Hours:Minutes) and Date (Day of week/Day of month) and every 6s the temperature instead of date dt = clock.getDateTime(); display.clearDisplay(); display.setTextSize(3); //size of the text that will follow display.setTextColor(WHITE); //its color display.setCursor(1,1); //position from where you want to start writing display.print(clock.dateFormat("H:i", dt)); //text todisplay display.setTextSize(1); display.setCursor(100,1); display.print(clock.dateFormat("D", dt)); display.setTextSize(2); display.setCursor(100,10); display.print(clock.dateFormat("d", dt)); display.setCursor(100,25); display.display(); if (millis()>=a+6000 && millis()<=a+7000){ //Temperature display refresh time (every 6-7 seconds) dt = clock.getDateTime(); a=millis(); display.clearDisplay(); display.setTextSize(3); //size of the text that will follow display.setTextColor(WHITE); //its color display.setCursor(1,1); //position from where you want to start writing display.print(clock.dateFormat("H:i", dt)); //text todisplay display.setTextSize(2); display.setCursor(100,1); display.print(clock.readTemperature(),0); display.setCursor(100,20); display.setTextSize(1); display.print("C"); display.setCursor(100,25); display.display(); delay(2000); //delay of displaying the temperature } } |
Type 5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
/* This code works with DS3231 RTC module and OLED display * It shows a format of date and time in the OLED screen and some cases the temperature too * Refer to http://www.Surtrtech.com for more details * This is Code #5 of the tutorial */ #include <SPI.h> //i2c and the display libraries #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <DS3231.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 32 // OLED display height, in pixels #define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //Declaring the display name (display) DS3231 clock; RTCDateTime dt; void setup() { display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Start the OLED display clock.begin(); delay(1000); display.clearDisplay(); display.display(); } void loop() { //This code displays the Time (Hours:Minutes) and the Date (Day of the week/Day of the month/ Month) dt = clock.getDateTime(); display.clearDisplay(); display.setTextSize(3); //size of the text that will follow display.setTextColor(WHITE); //its color display.setCursor(1,7); //position from where you want to start writing display.print(clock.dateFormat("H:i", dt)); //text todisplay display.setTextSize(1); display.setCursor(100,1); display.print(clock.dateFormat("D", dt)); display.setTextSize(2); display.setCursor(100,10); display.print(clock.dateFormat("d", dt)); display.setCursor(100,25); display.setTextSize(1); display.print(clock.dateFormat("M", dt)); display.display(); delay(1000); } |
Type 6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
/* This code works with DS3231 RTC module and OLED display * It shows a format of date and time in the OLED screen and some cases the temperature too * Refer to http://www.Surtrtech.com for more details * This is Code #6 of the tutorial */ #include <SPI.h> //i2c and the display libraries #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <DS3231.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 32 // OLED display height, in pixels #define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //Declaring the display name (display) DS3231 clock; RTCDateTime dt; void setup() { display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Start the OLED display clock.begin(); delay(1000); display.clearDisplay(); display.display(); } void loop() { //This code displays the Time(Hours:minutes) as 12h format with (AM or PM) and the Date (Month/Day of the month and year) dt = clock.getDateTime(); display.clearDisplay(); display.setTextSize(3); //size of the text that will follow display.setTextColor(WHITE); //its color display.setCursor(1,1); //position from where you want to start writing display.print(clock.dateFormat("g:i", dt)); //text todisplay display.setTextSize(1); display.setCursor(90,1); display.print(clock.dateFormat("A", dt)); display.setCursor(90,10); display.print(clock.dateFormat("M j", dt)); display.setCursor(90,20); display.print(clock.dateFormat("Y", dt)); display.display(); delay(1000); } |
Type 7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
/* This code works with DS3231 RTC module and OLED display * It shows a format of date and time in the OLED screen and some cases the temperature too * Refer to http://www.Surtrtech.com for more details * This is Code #7 of the tutorial */ #include <SPI.h> //i2c and the display libraries #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <DS3231.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 32 // OLED display height, in pixels #define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //Declaring the display name (display) DS3231 clock; RTCDateTime dt; void setup() { display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Start the OLED display clock.begin(); delay(1000); display.clearDisplay(); display.display(); } void loop() { //This code displays the Time( Hours:Minutes) as 12h format with (AM/PM) and Date(day of the week/Month/Day of the month) //And the temperature in Fahrenheit dt = clock.getDateTime(); display.clearDisplay(); display.setTextSize(3); //size of the text that will follow display.setTextColor(WHITE); //its color display.setCursor(1,10); //position from where you want to start writing display.print(clock.dateFormat("g:i", dt)); //text todisplay display.setTextSize(1); display.setCursor(60,1); display.print(clock.dateFormat("A", dt)); display.setCursor(95,1); display.print(clock.dateFormat("D", dt)); display.setCursor(95,10); display.print(clock.dateFormat("Mj", dt)); display.setCursor(95,20); float f=(clock.readTemperature() * 1.8) + 32; //Temperature is read in C and we convert it to F display.print(f,0); display.print(" F"); display.display(); delay(1000); } |
Categories
Yassine View All
Automation and Electrical Engineer, Electronics amateur trying to share my little projects.
hi how can i added 2 key for edit hour and minutes to this project??
You need a lot of modifications, better learn how to use an RTC first then use the 2 keys you want until you get that then merge it with this project.