micropythonraspberry-pi-pico

How to have the Pico W set correct time on boot


When running on batteries the Pico W's clock/utime starts at 2021-01-01 00:00:00 by default.

At every boot it obviously should:

Any good established technique out there to achieve this?

Using micropython.


Solution

  • You can use the ntptime module available from the micropython library. Once that's installed, using it is simple:

    >>> import time
    >>> import ntptime
    >>> time.localtime()
    (2000, 1, 1, 0, 0, 57, 5, 1)
    >>> ntptime.settime()
    >>> time.localtime()
    (2022, 7, 21, 1, 49, 1, 3, 202)
    

    Put something like this in your main.py and you should be all set.