Has anyone found how do do this please? I can of course set either system time or a manually configured time, but how to do it interactively, i.e. without editing and uploading again?
You can e.g. use the serial monitor to send the date and time. Or you can use a couple of buttons.
Thanks. Not possible to use serial monitor without PC, so it must be buttons. Or even a single button that would call a a function to do it at say 12:00 on the day it’s pressed - at exactly 12:00 from an accurate source,
I’m hoping for a pointer to example library code to save reinventing some. If I do have to start from scratch then my only idea so far is to replace the hard coded parameters like these with their respective variables. If so, I hope they prove easy to derive.
rtc.adjust(DateTime(2023, 9, 26, 11, 28, 0));
If all you have if one button and that button means "set the time to noon today" then you just need to adjust your hours
if (digitalRead(clockAdjustPin) == PUSHED) {
DateTime current= rtc.now();
DateTime noon = DateTime(current.year(), current.month(), current.day(), 12, 0, 0);
rtc.adjust(noon);
}
My coffee maker uses one button to set the time. It works this way: press the Time button and minutes start incrementing. Slow the first few counts, then faster. When it gets past 59 it starts incrementing the hour. Release the button and the process stops.
I'm afraid it needs a bit more that. What if the RTC was never set? Or (maybe not so likely) lost a day?
I think that the minimum number of buttons to make it reasonably user friendly would be 3; up button and down button and a select button to select which field (year/month/day/hours/minutes/seconds) to change. Two buttons will be possible when using long and short presses for up/down.
But you need some form of feedback to the user (lcd?).
If the clock has never been set, that can usually be detected in setup() and set to some reasonable default date/time as a starting point.
Single button with detection of long press / short press world be cumbersome, but I think doable.
First press initiates setting of year, you can use short presses to cycle through a decent range of years, such as 2000 - 2099. Long press then switches to months, which cycles from 1 to 12. Same for days, hours, minutes, and am/pm if you want 12-hour clock. Last press can just set seconds to 00 at the release time of the long press.
@sterretje , @david_2018
If I'm happy with anything up to a few seconds accuracy, and as it's a working, cased project with three buttons used already (one of them short/medium/long) I'm hoping my single button idea would be OK? Albeit obviously less flexible
The project uses a 16x2 LCD which shows the RTC time every second. When inaccuracy becomes unacceptable (well before its current state of over a minute slow) I'd be satisfied with waiting and pressing the button at exactly noon.
That is a bit unfriendly as you say, so alternatively maybe I could count up from 0 to 24 to set just the hour. With RTClib variable values for Year, Month and Day, plus zero for Minutes and Secs. Despite having pasted in code previously for my 'short/med/long' button, I favour simple quick presses.
I'll have a good think about the options, and others that may arise.
If you are only needing to adjust for a minute or two inaccuracy, you could use a single button to round to the nearest minute. What I mean by that, is if the button is pressed with seconds from :00 to :29, the time is set to the current hour/minute at :00 seconds, if seconds are from :30 to :59 the time is set to current hour/ (minute + 1) at :00 seconds. That would take a couple of minutes to shift time back/forward a whole minute, but would be simple to implement.
Thanks. I’ve just started playing with the code and expect all will become clearer once I get it working in simplest form.
I’d rather hoped there might be some ready made library example I could use as a basis, as I’m impatient to finish the project!
My simple test code did what it was supposed to, so I've shown it below in case it's of help to others with similar requirement.
I'd welcome any improvements/refinement please.
void setClock() // Called in loop().
{
// A0 (one of only two remaining pins) is wired to a new btn
// on my breadboard. When pressed, A0 goes HIGH.
bool clockSetFlag; // To avoid unwanted repetition.
if (digitalRead(A0) == 1)
{
{
if (clockSetFlag == 0) // Change has NOT already been done.
{
// At exactly 16:35 today, pressing the A0 button will
// apply the following change of time. Only want to do
// this once, so use flag to ensure that.
rtc.adjust(DateTime(Year, Month, Day, 16, 35, 0));
delay(1000);
Serial.print(F("/n Clock has been set"));
clockSetFlag = 1; // Change HAS already been done.
}
}
Serial.print(F("/nsetClock() was run"));
}
}
For those using a different library to RTClib, some changes to the parameter names will probably be needed.
clockSetFlag needs to be declared 'static' and given an initial value, otherwise it will get a random value each time the function is called.
Why do you only want to be able to set the clock once?
@david_2018 has identified a significant issue with your code.
Rather than using control variables, why not just monitor for a state change to pressed on A0 and set the clock when you see that.
Thanks, duly changed to static.
Thanks, I see your point about the flag. I obviously only want to do it once on the chosen day. But as it stands it will prevent changes on later dates. Revised code below:
void setClock() // Called in loop().
{
// A0 (one of only two remaining pins) is wired to a new btn
// on my breadboard. When pressed, A0 goes HIGH.
if (digitalRead(A0) == 1)
{
// At exactly noon today, pressing the A0 button will
// apply the following change of time.
rtc.adjust(DateTime(Year, Month, Day, 12, 0, 0));
delay(100); // May be redundant
Serial.println(F("setClock() was run"));
}
}
Directly after running that sketch yesterday (Uno, DS3231 with RTClib) it was matching the online clock here
precisely.
Less than 24 hours (86400 seconds) later it's lost about two seconds.
This seems over ten times more than the datasheet
would imply:
"Accuracy ±2ppm from 0°C to +40°C"
which works out at about 0.17s per day.
I'm using a 5V supply, not 3.3V as specified, but I'm sure that would not account for the difference? Curious to hear what other users are seeing please?
Look to make sure you don't actually have a DS3231M, it is not as accurate (datasheet)
The ones I have don't seem to be as accurate as the data sheets specify, but they are unbranded knockoffs of the original.
No 'M' suffix. Amazon UK:
"AZDelivery 3 x DS3231 RTC Real Time Clock I2C compatible with Arduino and Raspberry Pi, 4.3 out of 5 stars, 1,557 ratings"
So still looking for possible cause.
Losing as much as mine in this project?
I don't recall the last time I ran a long-term test, but mine are the DS3231M version and if they met the specs it was just barely. Not really unexpected for what I paid, just wanted actual hardware for code testing purposes.
Incidentally, some of the boards have a circuit that supplies a small charge current to the backup battery, that needs to be disabled if a non-rechargeable battery is used or it can destroy the battery.
using a rotary encoder with its embeded switch and leveraging your LCD would make it relatively easy to set each element separately
The chip marking should be either DS3231SN or DS3231M. But I have one marked SN which behaves like an M. So the marking may not tell the truth. The way to tell for sure is that the M's only option for the square wave output is 1Hz. If you set the register for a higher rate, it will still only produce 1Hz, whereas the SN will do 8KHz or whatever you select.
I would suspect that AZDelivery would deliver either an M or a fake SN. But the good news is you can adjust the Aging register to speed up or slow down the oscillator and give you very accurate timekeeping. The range of permitted values is -128 to +127. The default value is zero. If it's running slow, you need to reduce the Aging register value.