Hi I have made a digital clock with a DS3231, an OLED and an Arduino UNO. When I switch it off it loses time. Could anyone help me to change the sketch to remember the time, or/and to set some push-button time set buttons? Probably easier to just alter the sketch to remember the time. I really appreciate any help. ty.
Here are photos of it.
//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);
}
did you removed charging circuit on RTC module?
In the photos above the 7 segment lcd display clock isnt part of this project. It is one I soldered a few years ago. ty.
No I put a new lithium battery into it. It should work. I tried another new DS3231 I bought and that was the same. They have never remembered. I didnt program them with the DS3231 software at first, but now have, and still no memory. ty for your reply
You are setting the RTC to the compile time of the sketch ever time it runs.
Oh? How do I remedy it? I have been reading Arduino programming books but am not very good at it at the moment. How do I rememdy it? Do I put NOW in the spaces?
i have had problem. i make 4 module ready to use in the project and then 1 stop to remember the time. i took other module.
Run the sketch once with the call to setDateTime() to set the date/time in the RTC, then comment out that line and upload the sketch again.
There are videos out there on how to do this but essentially, you can set the time manually by putting your own time in the lines above (clock.setDateTime etc.... ) or by using the automatic feature which will extract the current time from your pc.
However, you do this just once, then open your sketch and comment out the time setting feature and re-program.
Note, best to remove the diode off the DS3231 board (stop it charging the button cell) and use a basic button cell.
I think the current ones will give you 10 years service.
Details on how to do this also out on the web.
In the set date time parameters I set the time using this format 18/06/2023 and 10:37, and the compiler lists several faults.
error: 'Serial' does not name a type
Serial.begin(9600);
error: 'clock' does not name a type; did you mean 'fclose'?
clock.begin();
^~~~~
fclose
error: 'clock' does not name a type; did you mean 'fclose'?
clock.setDateTime(18/06/2023, 1034);
^~~~~
fclose
error: expected unqualified-id before 'if'
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
^~
error: 'display' does not name a type; did you mean 'delay'?
display.display(); //display initial Adafruit logo
^~~~~~~
delay
error: expected constructor, destructor, or type conversion before '(' token
delay(2000);
^
error: 'display' does not name a type; did you mean 'delay'?
display.clearDisplay();
^~~~~~~
delay
error: 'display' does not name a type; did you mean 'delay'?
display.display();
^~~~~~~
delay
error: expected declaration before '}' token
}
^
Which are the parameters or format for putting the date and time into the sketch? Is it for example (18/06/2023, 10:34) Or do I not use colons for the time and also put the seconds in?
Probably a good time to post your complete program.
Hi, ty for your reply I posted the program near the beginning of this thread.
Oh, sorry the complete program is here.
//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);
}
The compiler lists errors
error: 'DS3231 clock' redeclared as different kind of symbol
DS3231 clock;
^~~~~
note: previous declaration 'clock_t clock()'
extern clock_t clock(void);
^~~~~
error: 'RTCDateTime' does not name a type; did you mean 'DateTime'?
RTCDateTime dt;
^~~~~~~~~~~
DateTime
error: request for member 'begin' in 'clock', which is of non-class type 'clock_t() {aka long unsigned int()}'
clock.begin();
^~~~~
error: request for member 'setDateTime' in 'clock', which is of non-class type 'clock_t() {aka long unsigned int()}'
clock.setDateTime(DATE, TIME);
^~~~~~~~~~~
error: 'dt' was not declared in this scope
dt = clock.getDateTime();
There are many more errors besides these.
The compiler says also that
Multiple libraries were found for "DS3231.h"
The clock works perfectly but it doesnt remember the time and date when its switched off.
I have editted the original sketch at the top of the thread. It wasnt all on it at first. Now the complete sketch is there.