Hardware components | ||||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 |
In this tutorial I will show you how to use DS3231 RTC module. It will explain how I2C connectivity works.
Also I will show you how to create a digital clock that reads date and time from DS3231 and displays it in thice graphical format on the OLED display.
Hope that you will find this video useful.
Here is the link to a tutorial:
If you like this content and you want to support me in creating similar videos go to my Patreon webpage
https://www.patreon.com/MariosIdeas
Or
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=7PD67JWZ9S3EJ&source=url
//Marios Ideas
//DS3231 Tutorial
//Using DS3232.h library
//Formating date and time with dateFormat function (for Arduino board with >2kb SRAM)
#include <Wire.h>
#include <DS3231.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
DS3231 clock;
RTCDateTime dt;
String txt;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // 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);
void setup() {
Serial.begin(9600);
clock.begin();
// Set sketch compiling time
clock.setDateTime(__DATE__, __TIME__);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.display();
delay(2000); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
display.display();
}
void loop() {
dt = clock.getDateTime();
display.fillRect(0,0,128,16,SSD1306_WHITE);
display.fillRect(0,17,128,16,SSD1306_BLACK);
display.fillRect(0,31,128,33,SSD1306_WHITE);
display.setCursor(1,1);
display.setTextSize(2);
display.setTextColor(SSD1306_BLACK);
display.println(clock.dateFormat("l", dt));
display.setCursor(1,18);
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.println(clock.dateFormat("M jS Y ", dt));
display.setCursor(3,35);
display.setTextSize(3);
display.setTextColor(SSD1306_BLACK);
display.println(clock.dateFormat("H:i", dt));
display.setCursor(100,35);
display.setTextSize(2);
display.setTextColor(SSD1306_BLACK);
display.println(clock.dateFormat("s", dt));
display.display();
delay(1000);
}
//Marios Ideas
//DS3231 Tutorial
//Using DS3232.h library
//Formating date and time with custom functions
#include <Wire.h>
#include <DS3231.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
int pause=1000;
DS3231 clock;
RTCDateTime dt;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // 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);
void setup() {
Serial.begin(9600);
clock.begin();
// Set sketch compiling time
clock.setDateTime(__DATE__, __TIME__);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.display(); //display initial Adafruit logo
delay(2000);
// Clear the buffer
display.clearDisplay();
display.display();
}
String DayOfTheWeek(uint8_t Day){
String DayText;
if (Day==1) DayText="Monday";
if (Day==2) DayText="Tuesday";
if (Day==3) DayText="Wednesday";
if (Day==4) DayText="Thursday";
if (Day==5) DayText="Friday";
if (Day==6) DayText="Saturday";
if (Day==7) DayText="Sunday";
return DayText;
}
String DayMonthYear(uint8_t Day,uint8_t Month,uint16_t Year){
String DayMonthYearText;
if (Month==1) DayMonthYearText="JAN ";
if (Month==2) DayMonthYearText="FEB ";
if (Month==3) DayMonthYearText="MAR ";
if (Month==4) DayMonthYearText="APR ";
if (Month==5) DayMonthYearText="MAY ";
if (Month==6) DayMonthYearText="JUN ";
if (Month==7) DayMonthYearText="JUL ";
if (Month==8) DayMonthYearText="AUG ";
if (Month==9) DayMonthYearText="SEP ";
if (Month==10) DayMonthYearText="OCT ";
if (Month==11) DayMonthYearText="NOV ";
if (Month==12) DayMonthYearText="DEC ";
DayMonthYearText=DayMonthYearText+Day;
if (Day==1)DayMonthYearText=DayMonthYearText+"st ";
if (Day==2)DayMonthYearText=DayMonthYearText+"nd ";
if (Day>2)DayMonthYearText=DayMonthYearText+"th ";
DayMonthYearText=DayMonthYearText+Year;
return DayMonthYearText;
}
String AddLeadingZero(uint8_t x){
String AddLeadingZeroText;
if(x<10) AddLeadingZeroText="0";
else AddLeadingZeroText="";
AddLeadingZeroText=AddLeadingZeroText+x;
return AddLeadingZeroText;
}
String CurrentTime(uint8_t H, uint8_t I ){
String CurrentTimeText="";
CurrentTimeText=CurrentTimeText + AddLeadingZero(H) +":"+AddLeadingZero(I);
return CurrentTimeText;
}
void loop() {
dt = clock.getDateTime();
display.fillRect(0,0,128,16,SSD1306_WHITE);
display.fillRect(0,17,128,16,SSD1306_BLACK);
display.fillRect(0,31,128,33,SSD1306_WHITE);
display.setCursor(1,1);
display.setTextSize(2);
display.setTextColor(SSD1306_BLACK);
display.println(DayOfTheWeek(dt.dayOfWeek));
display.setCursor(1,18);
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.println(DayMonthYear(dt.day,dt.month,dt.year));
display.setCursor(3,35);
display.setTextSize(3);
display.setTextColor(SSD1306_BLACK);
display.println(CurrentTime(dt.hour,dt.minute));
display.setCursor(100,35);
display.setTextSize(2);
display.setTextColor(SSD1306_BLACK);
display.println(AddLeadingZero(dt.second));
clock.forceConversion();
display.setCursor(85,18);
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.print(clock.readTemperature());
display.setCursor(117,16);
display.print("o");
display.display();
delay(1000);
}
Comments