Setting the time on the Tildagon
I'm beginning my adventures in MicroPython in the hope that I'll have something interesting working on the Tildagon Badge for EMF2026. Here's a basic implementation of a clockface.
Here's how to set the time on the badge. There's a hardware clock which should keep time between reboots.
- Install mpremote on your computer.
- Connect the Tildagon to your computer using a USB-C data cable
- On your computer's command line, run
mpremote
. You should see: >Connected to MicroPython at /dev/ttyACM0
>Use Ctrl-] or Ctrl-x to exit this shell
- Hold down the ctrl key on your computer. While holding it down, press the C key on your computer. This will open up a shell for you to enter commands.
- Enter the following commands one at a time, followed by enter
Python 3from machine import RTC
rtc = RTC()
rtc.datetime()
That will display the time that the badge currently thinks it is.
For example: (2000, 1, 1, 5, 0, 1, 47, 984022)
This is a slightly unusual format. It is: year, month, day, weekday, hours, minutes, seconds, subseconds.
The "weekday" is 0 for Monday, 1 for Tuesday etc.
This is an array. So, to access individual elements of the time, you can say:
Python 3year = rtc.datetime()[0]
Alternatively, you can do:
Python 3import time
time.localtime()
That will return something like: (2024, 6, 11, 15, 11, 39, 1, 163)
Which, according to the documentation, is "year, month, mday, hour, minute, second, weekday, yearday".
Setting the clock
To manually set the date and time, run:
Python 3rtc.datetime((2023, 6, 16, 1, 15, 36, 0, 0))
NTP
If you want to use NTP to synchronise the time with an Internet-based atomic clock, here's what you need to do.
- Connect your badge to WiFi.
- As above, connect with USB and run
mpremote
, then obtain a shell. - Enter the following commands one at a time, followed by enter:
Python 3import ntptime
ntptime.settime()
rtc.datetime()
That will now show you the synchronised time. Note, it will be set to UTC. There's no way to set the timezone - you'll have to deal with that in your code elsewhere.
You can also read the reference documentation about the Real Time Clock.
Bugs said on mastodon.social:
@Edent really useful write-up, thanks! Looking forward to giving this a go in the next few days.
More comments on Mastodon.