Using the Pi Pico RTC and micropython
I have a project in mind for a crude clock. Since the Pico has a built in RTC i though it would be ideal to test the concept.
I'm using utime to get the current time but it is the wrong date and time. I cant figure out how to set the internal clock. looking at micropython.org I can see the line
"The current calendar time may be set using
machine.RTC().datetime(tuple) function, and maintained by following means:"
This has me stumped, all the examples Ive tried while googling have "from machine import RTC" at the top of the code but i get an error "AttributeError: 'module' object has no attribute 'RTC'"
Can anyone help me out please?
ps Here is the code in its simplest form. It should get the current minute of the hour and flash an led accordingly
Spoilerthe codeimport machine
sleep = 0.5 # sleep variable for easy alteration
led = machine.Pin(15, machine.Pin.OUT) #assign pin to GPIO pin 15 (pin 20)
epoch = utime.time() # get time since epoch
time = utime.gmtime(epoch) # store time in variable in time
mins = time # access the minute from the tupple
print(epoch) #see second since epoch
print(time) # see the tupple. ##### Why is it the wrong time and how can i set it? ###
print(mins) # see the extracted minutes from the tupple
for _ in range(mins): # flash an led 'mins' amount of time
Can anyone help me out please
At quick glance I'm wondering where you intend to get your current time to seed the RTC. You will need to manually input it or get it from a source that your Pico is in communication with. Anyway the following link may give you some pointers.
That's a good point, I guess I hadn't fully thought about it, I've used the datetime module in python before and I assumed it would work the same, although now that I think about it I'm not sure if that gets the time from the computers clock or the internet.
anyway thanks for the reply! I'll take a look 🙂
It appeared to work i.e the onboard led flashed rapidly every second then went out when I ran the picoTimeSync.py file, however the date and time are still incorrect.
its given me some things to think about though,
The problem here is that the Pico is resetting its RTC to New Years' day 2021 every time it is powered up. So if you don't have an external device to reset the time every time it boots then its functionality as a real-time clock is kind of limited.
Not sure if this code I found on the Raspberry Pi forum will be of use, but it might help you set your clock. If it does help you can thank a user named "DWiskow". It sets the clock and then displays the time once every second.
print(" YYYY MM DD HH MM SS")
dateTime = (input ("Enter current date & time: "))+' 0 0'
synchronisedTime = utime.mktime(list(map(int, tuple(dateTime.split(' ')))))
timeDelta = synchronisedTime - int(utime.time())
return utime.localtime(utime.time() + timeDelta)
dateTime = timeNow()
Of course even after you have the clock set you would still need to keep the Pico powered, otherwise you'll lose the time.
"Never trust a computer you can’t throw out a window." — Steve Wozniak
Thanks for the replies
The board didn't lose power between running picoTimeSync.py and running my test code so I'm not sure what i did wrong, anyway I ended up using a simple counter to get the info i needed
# get time from user
h = input("enter hour:")
m = input("enter minute:")
s = input("enter second: ")
# cast to int
hour = int(h)
minute = int(m)
second = int(s)
second += 1 #increment the second value by 1 each second
if second > 59:
second = 00
minute +=1 # increment minute
if minute > 59:
minute = 00
hour += 1 # increment hour
if hour > 12:
hour = 1
time = [hour, minute, second]
print(hour, minute, second)
thanks again guys
I'm using a DS1307 real time clock module. In a previous project I just took the date/time from that. Now I'm trying to use it to synch the clocks on two Picos, so that when they log events they are set to precisely the same time. If it helps I can post some working demo code for using just one Pico with a DS1307, but you'd still have the problem of obtaining one from somewhere.