SurtrTech
Published © CC BY

DS3231 RTC Module + OLED Tutorial

Easy tutorial for the DS3231 module with multiple examples.

EasyFull instructions provided1,564
DS3231 RTC Module + OLED Tutorial

Things used in this project

Story

Read more

Schematics

Wiring

Wiring qtq9kgjsq9

Code

Code_1.ino

Arduino
/* 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 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();

}

Code_2.ino

Arduino
/* 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 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
}

Code_3.ino

Arduino
/* 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 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);
}

Code_4.ino

Arduino
/* 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 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);
}

Code_4_1.ino

Arduino
/* 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 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
  
  }
}

Code_5.ino

Arduino
/* 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 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);
}

Code_6.ino

Arduino
/* 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 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);
}

Code_7.ino

Arduino
/* 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 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);
}

Github

https://github.com/jarzebski/Arduino-DS3231

Credits

SurtrTech

SurtrTech

1 project • 41 followers
Electronics amateur, sharing with people my few projects

Comments