Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8082

MicroPython • Re: How to set the right Pi Pico system time? With the use of DS3231?

$
0
0
So rather than doing all that malarky I suggest you go for the library that I suggested from Peter Hinch, and I post this code below. Copy it into a file called ds3231_gen.py and place it on your pico.
I also enclose an example program that should set the rtc on the pico the the ds3231 time. This example was bodged from an example I had in my snippets of getting an NTP time from my router that also has a timeserver capability. However I commented out the setting of the time from the timeserver and change it to setting the time from the DS3231 as read via the DS3231 class to be found in the ds3231_gen.py file.
Thank you so much! The lunch cup of coffee has inspired you to find the solution. It works now as hoped.
I took the necessary code from your example file:

Code:

# Example of SirFrico to get the right time in the Pi Pico, derived from a DS3231# Firstly print the time on pico startup, then get and print the time from the ds3231# April 28 2025from time import sleep, localtimefrom machine import RTC, I2C, Pinfrom ds3231_gen import  DS3231rtc = RTC()# print time on pico startup# local time format (year,month,day,hour,min,sec,weekday,yearday)print(localtime())# time from ds3231 via peter hinch class# first set the pico i2c pins and i2c bus that have been used to connect to the DS3231i2c = I2C(id=0, scl=Pin(21), sda=Pin(20))# instanciate the DS3231 class (as found in ds3231_gen.py)drtc = DS3231(i2c)# get the time from the ds3231tm = drtc.get_time() # rtc.datetime args are (year, month, day, weekday, hours, minutes, seconds, subseconds)# set the time on the picortc.datetime((tm[0], tm[1], tm[2], tm[6] + 1, tm[3], tm[4], tm[5], 0))# print time after setting pico RTCprint(localtime())
It shows two time momentsof the Pi Pico (after startup and after getting the DS3231 actual time)
Pi Pico time after startup(2021, 1, 1, 0, 0, 1, 4, 1) and after getting the DS3231 actual time (2025, 4, 28, 22, 24, 18, 0, 118)
And then I added at the end of your script some code to write something to a file on a SD card and checked the creationdate. And that corresponds now with the actual date/time at the moment the file was created.
It can now work standalone with a 5V USB adapter.
The code I added (then extra needed import sdcard, os and import sys and from machine import SPI):

Code:

# Create a file and write "abc" to itdef mount_sdcard(spi, cs_pin):    try:                        sd = sdcard.SDCard(spi, cs_pin)        # Mount microSD card        os.mount(sd, SD_MOUNT_PATH)        # List files on the microSD card        #print(os.listdir(SD_MOUNT_PATH))                       # Open a file        path = '/sd'        dirs = os.listdir( path )        # Print all the files and directories        for file in dirs:            print(file)                except Exception as e:        print('An error occurred mounting the SD card:', e)        # stop the program when no SD card is detected        sys.exit(1)# Mount SD cardmount_sdcard(spi, cs)# Write "abc" to SD card  with open(FILE_PATH, 'a') as file:    file.write("\n"+"abc")    sys.exit(1)
Thanks once more!
Thanks to everyone for thinking along.

Statistics: Posted by user-10 — Mon Apr 28, 2025 9:15 pm



Viewing all articles
Browse latest Browse all 8082

Trending Articles