Cornell University ECE4760
Using LWIP to set the RTC

Pi Pico RP2040

 

Real_time clock (RTC) on RP2040 PicoW
The rp2040 has a RTC in which you can set hours, minutes, seconds, day, month, and year, then read back the current time later. Unfortunately the Pico board clock oscillator is rated at ±30 ppm, or accuracy of 3x10-5. Since there are 86400 seconds/day, this means up to 2.6 sec/day drift. This works out to about 15.8 minutes/year, not very good compared to the accuracy people expect from even $20 wrist watches! The solution is to use Network Time Protocol (NTP) to reset the RTC often, perhaps once or a few times per day. NTP is usually accurate to better than a few tens of milliseconds. The Picow has a WIFI module that can be used to connect to the internet, given a TCP/IP package running on the rp2040.

The structure of the program is built around Protothreads, with a thread to get the network time, a thread interfacing to a serial terminal for debugging and commands, and a couple of blink threads just to show that the networking had not killed the threader. Actual low level packet receive is performed by an ISR set up by LWIP. The NTP code is copied directly from the picow examples repositiory, then converted to run in a thread, with a control semaphore signaled by the serial thread.

The serial thread commands are:

Code, project ZIP
Note that the code has defines for WIFI SSID and password that must be specified for your system.
Also note that date is not handled very well if the actual UTC is less than the local time zone.


 


Copyright Cornell University July 2, 2023