PCF8523 RTC - Setting date and time

I am using the RTClib.h for reading time off a PCF8523 along with a Mega 2560 board. And using this library, the Date Time adjust of the RTC is automatically done using compile time values. Easy and nice. But my requirement is different.

On the end application, MCU talks to an external App via the Serial port and when user wants to adjust the RTC, he sends a update command. The Year / Month / date / Hour / Minute / Second values are sent as int variables picked up from the PC clock.

Now problem is : How to update the PCF8523 with these values got via Serial?

When using that library, you have to pass a DateTime object, but it's actually quite simple to do.

All of the variables here are INT. So, as long as you read the serial data and make sure the values are INTs, it should work.
RTC.adjust(DateTime(year,month,day,hour,minute,second));

If you just want to update only, for example, the hour value, you can read the rest of the values and pass them straight back, but including the new INT hours value
DateTime now = RTC.now(); //this will read the current data and time
//then adjust the hours value and then.....
RTC.adjust(DateTime(now.year(),now.month(),now.day(),hours,now.minute(),0)); //hours is the updated value

It looks complex, but there are lots of examples out there.

2 Likes

@bezelsanddisplays Thanks .. The exact response I was looking for !!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.