Comments on: Raspberry Pi Pico: DS3231 RTC (Real-Time Clock) – Get Time and Set Alarms (MicroPython) https://randomnerdtutorials.com/raspberry-pi-pico-ds3231-rtc-micropython/ Learn ESP8266, ESP32, Arduino, and Raspberry Pi Mon, 26 May 2025 11:57:22 +0000 hourly 1 https://wordpress.org/?v=6.8.2 By: Sara Santos https://randomnerdtutorials.com/raspberry-pi-pico-ds3231-rtc-micropython/#comment-1050194 Mon, 26 May 2025 11:57:22 +0000 https://randomnerdtutorials.com/?p=165176#comment-1050194 In reply to Harald Bohnenkamp.

Hi.
You can make a request to a TimeAPI to synchronize the time of the clock according to a specific timezone. You can make a request once a day for example.
This way, the time will be always updated and prevents drifting.
At the moment, we don’t have a tutorial like that published here.
You can take a look at APIs like timeapi for example.
Regards,
Sara

]]>
By: Harald Bohnenkamp https://randomnerdtutorials.com/raspberry-pi-pico-ds3231-rtc-micropython/#comment-1049309 Sun, 25 May 2025 14:50:57 +0000 https://randomnerdtutorials.com/?p=165176#comment-1049309 Hi, I would like this project (RPi Pico W) in the Arduino IDE.
I like NTP request and automatic Summertime / Wintertime, so I have not to touch this Clock anymore.
Is there any tutorial for my request?

thx Harald

]]>
By: Kees https://randomnerdtutorials.com/raspberry-pi-pico-ds3231-rtc-micropython/#comment-1026953 Mon, 14 Apr 2025 18:02:29 +0000 https://randomnerdtutorials.com/?p=165176#comment-1026953 Hi Sara,

I need to correct something.
In my last comment I wrote that I had only commented out the line “rtc.datetime(initial_time)” before I uploaded the file to The Pi Pico as main.py to get the right date and time.
I repeated sending your program (with correct time In the DS3231) to the Pi Pico with that out commented line then unfortunately the time was copied from the Pi Pico (01-01-2021).
After I had uploaded your program without your lines printed in bold, then the Pi Pico shows the right date and time.

]]>
By: Kees https://randomnerdtutorials.com/raspberry-pi-pico-ds3231-rtc-micropython/#comment-1026145 Sat, 12 Apr 2025 11:43:44 +0000 https://randomnerdtutorials.com/?p=165176#comment-1026145 In reply to Sara Santos.

Everything takes its time (especially with me :-)) and after reading your extensive answer it is also understandable that after inserting the correct time in the DS3231 there should be no new sync.
After the sync I have only commented out the line “rtc.datetime(initial_time)”.
The program is now working!
Now I can take the hardware to a location without a power supply, but substituting it with a solar panel and then see if I can get the power consumption down even further by turning off the LED on the DS3231 (scratching through the track) and implementing one of your “deep sleep” examples.
A nice side effect is to discover the time after reset of the Pi Pico is 01-01-2021.

]]>
By: Sara Santos https://randomnerdtutorials.com/raspberry-pi-pico-ds3231-rtc-micropython/#comment-1026119 Sat, 12 Apr 2025 10:18:52 +0000 https://randomnerdtutorials.com/?p=165176#comment-1026119 In reply to Kees.

Hi.
The code you’re running, sets the time on the DS3231 from the localsystem:

#Or get the local time from the system

initial_time_tuple = time.localtime() # tuple (microPython)
initial_time_seconds = time.mktime(initial_time_tuple) # local time in seconds

It can get the correct time because the pico is connected to the computer.
When you disconnect it, the Pico will get the system time again, which in this case is the time set on the pico from the manufactuer.

What you need to do, is to run the code to set the DS3231 time first (our code as it is).
After running that code with the Pico connected to your computer for the first time, it will set the correct time on the RTC.

After that, the RTC is synchronized, you don’t need to sync it again.
So, upload the same code, but without the lines that set the time.

Which means, that after sync the RTC, upload the same code, but without the following lines

#Or get the local time from the system
initial_time_tuple = time.localtime() # tuple (microPython)
initial_time_seconds = time.mktime(initial_time_tuple) # local time in seconds

#Convert to tuple compatible with the library
initial_time = urtc.seconds2tuple(initial_time_seconds)

#Sync the RTC
rtc.datetime(initial_time)

I hope this is clear.

Regards,
Sara

]]>
By: Kees https://randomnerdtutorials.com/raspberry-pi-pico-ds3231-rtc-micropython/#comment-1025661 Thu, 10 Apr 2025 19:56:36 +0000 https://randomnerdtutorials.com/?p=165176#comment-1025661 Hi Sara,

The code works fine (taking into account adding the + signs in the formatted_datetime and uncomment the line 34 to get temperature as written in my comment above).
When I upload the code (to get date and time) to the Pi Pico W and name it as main.py, then I hoped that I could use this code unplugged from Thonny and connect the Pi Pico and DS3231(get its voltage from the Pi Pico 3.3 V and there is a rechargeable battery LIR2032 battery in the DS3231 module and the battery voltage is > 3 Volt)
to an external supply (e.g. a Solar Power bank with USB). If I do so then the time is reset and start to count from 01-01-2021 00:00:00 (hms). Do you know why this happen?
Is this issue solvable?
Thank you in advance for your help.

]]>
By: Kees https://randomnerdtutorials.com/raspberry-pi-pico-ds3231-rtc-micropython/#comment-1015097 Sun, 09 Mar 2025 20:20:03 +0000 https://randomnerdtutorials.com/?p=165176#comment-1015097 Hi,

Thank you for your tutorial about the DS3231 RTC.
After I had uploaded the urtc.py to my Pi PicoW with Thonny
and after I had launched the RealTimeClock in Thonny
then two error message popped up in the console.

The first message was: AttributeError: ‘DS3231’ object has no attribute ‘get_temperature’
It is in the code at line 34: temperature = rtc.get_temperature() where rtc = urtc.DS3231(i2c)
I have investigated the Github Adafruit urtc.py library and I cannot find anything about temperature.
Maybe you have another Adafruit library version than the one you have published in your tutorial.

The second message was (File “”, line 50): SyntaxError: invalid syntax

46 formatted_datetime = (
47 f”{days_of_week[current_datetime.weekday]}, ”
48 f”{current_datetime.year:04d}-{current_datetime.month:02d}-{current_datetime.day:02d} ”
49 f”{current_datetime.hour:02d}:{current_datetime.minute:02d}:{current_datetime.second:02d} ”
50 )

Finally after placing a “+” sign at the end of line 47 and 48 (but not line 49) solved this Thonny SyntaxError.
This solution was needed after I had installed the MicroPython variant Raspberry Pi.Pico W/Pico WH and version 1.23.0
However if I installed version 1.24.1 then the ‘+’ sign is NOT needed.

Hopefully you can point me to where the “get_temperature” attribute can be found.

]]>