There seems to be a lot of confusion on how to set the time on the ds3231 or other similar time clocks. There is talk about time zones and even a sketch in the Arduino IDE for help in setting the time. All of this just makes for confusion because it is really simple to set the ds3231.
//rtc.now(DateTime(year,month,day,hour,minute,second))//upload then delete sets clock
You just have to copy up to the second bracket. Then enter for year; 2024, month if September; 09, day if September 22; 22, hour if 4 in the afternoon; 16, minute if 4:30; 30, seconds if you want to be that accurate; 09.
Remember to always make 2 digits.
This is done in setup. When you have uploaded the time and date go back to the sketch and delete the time you have set but keep it as a comment if you like. Upload again. Otherwise the time will be reset every time you power up or use the serial monitor.
Many examples set the time based on the compilation timestamp, which causes the time to reset every time the processor restarts or is reprogrammed. This approach is problematic because it doesn't maintain accurate time across power cycles.
Here's a simple example of how to set up an RTC (Real-Time Clock) module like the DS3231 to maintain time even when the processor is restarted or reprogrammed. This program will set the time only once and keep it running independently on the RTC.
Hardware Required:
- Arduino (any model)
- DS3231 RTC module
- I2C wiring (SDA to A4, SCL to A5 for standard Arduino boards) Be sure the pull up resistors are installed.
Libraries Required:
Wire.h(for I2C communication)RTClib.h(for interfacing with the DS3231 module)
#include <Wire.h>
#include <RTClib.h>
RTC_DS3231 rtc; // Create an RTC object
// Uncomment the following line and set the desired date and time for initial setup.
// This should be commented out after the first successful upload to avoid resetting the time again.
// DateTime now(2024, 9, 22, 15, 30, 0); // YYYY, MM, DD, HH, MM, SS
void setup() {
Serial.begin(9600);
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, setting the time!");
// Set the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// Set the RTC to the manual date & time defined above
// rtc.adjust(now);
} else {
Serial.println("RTC is running with correct time.");
}
}
void loop() {
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
delay(1000); // Print time every second
}
Instructions:
- Initial Setup:
- Uncomment the line
rtc.adjust(now);and define your desired date and time in theDateTime now(...)object. - Upload the sketch once to set the RTC time.
- Once the time is set, comment out the
rtc.adjust(now);line to prevent the time from resetting every time the sketch is uploaded.
- Normal Operation:
- After the initial setup, the RTC module will maintain the correct time even if the Arduino loses power or is reprogrammed.
- Library Installation:
- Make sure you have the
RTCliblibrary installed in your Arduino IDE. You can install it via the Library Manager.
This approach ensures that the time is set once and maintained by the RTC, avoiding the issue of resetting the time each time the processor is restarted.