Notifications
Clear all

Using the Pi Pico RTC and micropython


Existential_Fred
(@existential_fred)
Active Member
Joined: 1 year ago
Posts: 9
Topic starter  

Hi,

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

Many thanks

Fred

Spoiler
the code
import machine
import utime

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[4] # 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
led.value(1)
utime.sleep(sleep)
led.value(0)
utime.sleep(sleep)
This topic was modified 1 year ago by Existential_Fred

Quote
b
 b
(@b)
Prominent Member
Joined: 3 years ago
Posts: 912
 
Posted by: @existential_fred

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. 

https://www.raspberrypi.org/forums/viewtopic.php?t=301502


ReplyQuote
Existential_Fred
(@existential_fred)
Active Member
Joined: 1 year ago
Posts: 9
Topic starter  

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 🙂


Sean451 liked
ReplyQuote
Existential_Fred
(@existential_fred)
Active Member
Joined: 1 year ago
Posts: 9
Topic starter  

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,

 

thanks again

 


ReplyQuote
DroneBot Workshop
(@dronebot-workshop)
Workshop Guru Admin
Joined: 3 years ago
Posts: 1111
 

@existential_fred

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.

import utime
print()
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())

def timeNow():
return utime.localtime(utime.time() + timeDelta)

while True:
dateTime = timeNow()
print("{04d} {02d}".format(dateTime[2],dateTime[1],dateTime[0],dateTime[3],dateTime[4],dateTime[5]))
utime.sleep(1)

 

Of course even after you have the clock set you would still need to keep the Pico powered, otherwise you'll lose the time.

😎

Bill

 

"Never trust a computer you can’t throw out a window." — Steve Wozniak


ReplyQuote
Existential_Fred
(@existential_fred)
Active Member
Joined: 1 year ago
Posts: 9
Topic starter  

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

Spoiler
clockCounter
import utime

# 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)


while True:
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
utime.sleep(1)
time = [hour, minute, second]
print(time)
print(hour, minute, second)

thanks again guys


ReplyQuote
RoosterCogburn
(@roostercogburn)
Active Member
Joined: 1 year ago
Posts: 5
 

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.


ReplyQuote