This is only a preview of the December 2026 issue of Silicon Chip. You can view 37 of the 104 pages in the full issue, including the advertisments. For full access, purchase the issue for $10.00 or subscribe for access to the latest issues. Articles in this series:
Items relevant to "Advanced Cable Analyser":
Items relevant to "Inrush Current Limiter":
Articles in this series:
Items relevant to "NeoPixel RGB LED Analog Clock, Part 2":
Items relevant to "The Magic Candle":
Purchase a printed copy of this issue for $15.00. |
W
e described how this new
clock works in the main
article last month, including the assembly instructions. What we didn’t have space to
explain properly is the user interface,
which is accessed via a web page on
the local network. We also didn’t
discuss how the somewhat complex
firmware works. This article will go
over both.
#1 Status display
At the top of the web page, we show
various status fields so you can see
what the Clock is doing:
A. Time – this is the same time
shown on the clock face during normal operation
B. Time source:
↳ “NTP” means the clock is keeping time using its internal oscillator,
synchronised hourly with the selected
NTP server. It should remain accurate
within less than a second. The time
is considered valid as long as it was
successfully synchronised within the
last four hours.
↳ “GNSS” means it’s receiving valid
UTC time from the GNSS module and
converting that to local time using the
time zone rules. This is considered
valid as long as valid time data was
received from the module within the
last three seconds.
↳ “Internal” is the same as NTP
except that it will no longer try to keep
it synchronised. This can be used if
NTP and GNSS are not available, but
you’ll have to correct it manually if it
drifts over time.
C. Last NTP sync – the time on the
clock face the last time NTP synchronisation was successful.
D. GNSS status:
↳ “No data” means the Pico W isn’t
receiving valid NMEA serial data from
the module.
↳ “Receiving, no fix, N satellites”
means the module is responding but
56
Silicon Chip
Ne o
The key to this new Clock is the web
interface, shown in Screen 1 (reproduced from last month). It provides
all the status and configuration details,
apart from what’s on the clock face.
The only additional controls are two
pushbuttons on the front of the clock;
their functions can be configured, as
described below. Let’s now go through
the fields one-by-one so we can explain
how they work.
P
Detailed usage instructions
R
l
e
ix
E
L
B
G
This new
clock uses
addressable
RGB LEDs
and has builtin Wi-Fi and
NTP support; plus,
it can connect to a
GNSS/GPS module.
After describing it
last month and providing
assembly details, we now have
full usage instructions and go into
depth on the firmware that runs it.
Australia's electronics magazine
siliconchip.com.au
ED ‘Ana 2 by N
l o g ic
’
Par
t
V
las
l
ho
C
i ne
n
ock
doesn’t have enough data to provide
an accurate UTC time yet
↳ “Valid time, no fix, N satellites”
means the module can’t provide a
location, but it is giving an accurate
UTC time.
↳ “Valid fix, N satellites” means the
module is providing both the accurate
UTC time and a valid location fix
E. Time zone – the currently calculated time zone based on the rules
below. This is what’s used to convert
the UTC time to local time for display
on the clock face.
F. Wi-Fi – the name of the Wi-Fi
network the unit is connected to, or
the access point name it is currently
providing
G. IP address – the unit’s current IP
address on the Wi-Fi network.
H. Wi-Fi signal – the signal strength
of the current Wi-Fi network
I. Auto brightness – if auto brightness is enabled, this is the current
brightness percentage determined
using the rules set below
J. LDR – the raw LDR data. This
should change if you cover the LDR or
shine a bright light on it. It’s updated
a couple of times a second.
K. Saved settings – indicates whether
there are any changes to the settings
that haven’t been written to permanent
(flash) memory
L. Firmware – the current firmware
version and how many times it has
been updated
M. Uptime – how long the Clock has
been running since it was last powered up
#2 Wi-Fi settings
A. Mode – if Mode is set to Connect
to network, the Clock will attempt
to connect to the network with the
given SSID and password the next
time it boots or the Reconnect button
is pressed. Otherwise, it will create its
own network using the given SSID,
and you will need to use the specified
password to connect to it (the network
will be unsecured if the password field
is left blank).
B. SSID – in AP mode, the network
created has this name. In network connection mode, it will try to connect to
a network with this name.
C. Password – in AP mode, if blank,
the network will be unsecured. Otherwise, you will need this password to
access it. In network connection mode,
it will use this password to connect to
the secured network specified.
siliconchip.com.au
Australia's electronics magazine
December 2026 57
D. Hostname – the Clock will identify
itself by this name. It will show up
in the router/access point connected devices list under this
name, and the web interface should be accessible
via https://<hostname>
(you may need to add
.local to the end).
E. Reconnect –
this will drop the
Wi-Fi connection
and then try to
connect again. If
you’ve changed
any of the above,
that will cause it
to use the new
settings.
F. Scan for networks – this will
cause a new window to open, and
after a few seconds, it
should populate with
a list of the Wi-Fi networks that were found,
along with their signal
strengths. Clicking one will
copy its SSID into the SSID field
on the main web interface and close
the new window.
G. Forget network – this blanks out
the SSID and password fields and
saves the new configuration to flash,
so the current network details will
be erased (not necessarily securely,
though).
H. Disable Wi-Fi – this will allow the
clock to keep running but switch Wi-Fi
off. You’ll likely need to reboot it to
restore Wi-Fi operation, unless you’ve
configured one of the buttons to toggle it. If set to use NTP time, this will
cause the time to eventually become
invalid, so it’s better to use GNSS time
if using this feature.
#3 Time settings
A. Time source. This can be:
↳ NTP – use NTP only. Typically
used when no GNSS module is connected.
↳ GNSS – use GNSS only. If the
GNSS module is not giving a valid
time, the display will switch to a waiting pattern.
↳ GNSS Then NTP – it will use the
time from the GNSS module as long
as it’s valid. If not, it will fall back on
NTP time.
↳ NTP then GNSS – it will use the
NTP time as long as it’s valid. If it
58
Silicon Chip
becomes invalid, it will fall back on
the GNSS time, if that is available.
↳ Internal – similar to NTP mode
but it will show the time regardless of
whether a valid NTP synchronisation
has been performed in the last four
hours. If selected after boot, the clock
will start at 12:00 noon.
B. Current time – this echoes the
time field in the status area
C. NTP server – allows you to
change which server is used for NTP
synchronisation. The default is pool.
ntp.org.
D. GNSS baud rate – the baud rate
used to receive data from the GNSS
module. Changing this has immediate
effect. Typical values are 4800, 9600,
14400, 19200, 38400, 57600, 115200,
230400, 460800 and 921600. If unsure,
check your module’s data sheet; some
modules can be configured for various baud rates, but there’s usually a
default.
E. Synchronise – if connected to
a Wi-Fi network, this will trigger an
immediate NTP synchronisation. If
you’re connected to the USB serial
console, you will see messages indicating when it’s triggered and when
Australia's electronics magazine
synchronisation is completed, usually a few seconds later.
F. GNSS Debug Window
– opens a separate window that streams serial
data from the GNSS
module with options
like pausing the display and changing the baud rate.
This can help you
decide if the unit
is getting valid
data from the
module without
having to use a
separate USB/
serial adaptor.
G. Set internal time – if the
time source is set
to Internal, you
can enter the UTC
time and date here
in the format YYYYMM-DDThh:mm and
click Set. The clock’s time
and date will be updated,
with the seconds value set to
zero. The date is important so
that daylight saving changes will
be made correctly in regions that
observe DST.
#4 Time zone
A. You can set two different time
zone offsets from UTC here. They
are set in terms of hours and minutes
(they are typically between -12:00 and
+12:00 but there are some outside that
range).
If your region doesn’t use daylight
saving, you can set them identically. If
it does, set the first one to the time zone
offset when DST is not in effect and the
second one to when it is. The defaults
suit NSW, Vic, Tas and the ACT: +10:00
(non-DST) and +11:00 (DST).
The time zone names can also be
set, although they don’t figure into the
time display calculations.
B. There are three DST modes:
↳ Automatic – use the rules below
to determine when to switch time
zones for DST.
↳ On – force the use of the second
(DST) time zone. This can be toggled
manually.
↳ Off – force the use of the first (nonDST) time zone. This can be toggled
manually.
C. DST starts – this specifies the conditions for the start of daylight saving
siliconchip.com.au
time. You need to specify the time,
week, day of the week and month.
Look up your local time zone/DST
rules. The default is 2am on the first
Sunday in October, which suits NSW,
Vic, Tas and the ACT.
D. DST ends – this specifies the conditions for the end of daylight saving
time. You need to specify the time,
week, day of the week and month.
Look up your local time zone/DST
rules. The default is 3am on the first
Sunday in April, which suits NSW,
Vic, Tas and the ACT.
#5 Clock face master control
This lets you switch all the LEDs on
and off (eg, if they are bothering you
at night) and set the master brightness, which always has effect. If you
set the master brightness to 50%, the
LEDs will never run at more than 50%
brightness.
#6 Hands
Here you can set the RGB colours
of the hour, minute, second and sub-
second hands using a colour picker. If
you set them to dimmer colours, that
will affect their relative brightness.
If you enable “Smooth transition”
for a hand, when it moves to the next
position, it will fade over a second
rather than jumping.
You can also enable and disable
the second and sub-second hands
here, determine whether the sub-
second hand chaser runs clockwise or
anti-clockwise, and set the sub-second
hand trail length in pixels (the trail
fades behind it as it moves). Setting
the trail value to 0 disables the trail.
Screen 1:
this shows
all the Clock
settings,
including
those for WiFi and OTA
updating. The
top section
is a status
display. It’s
implemented
as static
HTML
that uses
JavaScript
to fetch the
status and
settings from
endpoints
on the
device and
triggers other
endpoints to
make settings
changes or
trigger state
changes.
#7 AM/PM LEDs
You can enable and disable these
individually and set their colours
when on.
#8 Auto-dimming
When enabled, if there’s more light
detected by the LDR, the pixels will
run brighter. Otherwise, only the master brightness control has an effect.
You can set the maximum and minimum brightnesses applied to the LEDs
for the two extremes of the LDR sensing, and the LDR values that correspond to these.
By default, the LDR values are
sensed automatically; they are the lowest and highest values seen. This can
be calibrated by placing a black insulating object over the LDR to block all
siliconchip.com.au
Australia's electronics magazine
December 2026 59
light, then shining a bright light at it.
Until you do this, the clock display
may appear to flicker as the LDR hasn’t
been calibrated. If you want to save
this calibration permanently, simply
switch off the Auto-calibrate setting
after calibration and save the settings.
The detected values will be permanent
unless you re-enable Auto-calibrate
or change them manually by editing
the fields.
connected to your Wi-Fi network.
Once you’ve done that, it should only
take a few seconds for it to get the current time from the network, and the
clock face will come alive. Assuming you’ve set the time zone (also
described in the last article), the time
should be correct and you can hang
the clock on the wall.
There are all sorts of things you can
customise, but the defaults are usable,
and many people could be happy with
#9 Testing
them. One of the great things about the
Clicking either button initiates one Wi-Fi interface is that if you decide
of the LED test modes. The first, “Test you want to change something later,
all LEDs”, lights them up white one you don’t need to touch the Clock!
at a time, starting with 12 o’clock and Just open its web page and change the
working its way clockwise around the required settings.
clock face, then the AM/PM LEDs.
If you’re using a GNSS module, you
It returns to the normal clock mode will probably want to change the time
after that. The second, “Test colours”, source to “GNSS then NTP”. That way,
lights up all LEDs red for 10s, then it will use the satellite-based time
green for 10s, then blue for 10s, then as long as it’s certain it’s accurate. If
returns to the normal clock mode.
that goes away for some reason (eg,
marginal satellite signal reception),
#10 Button functions
it will fall back to NTP, then switch
These let you select what the two back to GNSS when the signal comes
buttons, S1 (A) and S2 (B), do when good again.
pressed. You can set separate functions
You may want to adjust the ‘hand’
for a short (<1s) or long (>1s) press of colours, especially if that helps you
each. The defaults are:
easily remember which shows the
• S1 short press shuffles the ‘hand’ hour, minute and second. You may
colours
also want to change or disable the
• S1 long press toggles auto- sub-second chaser if you don’t like it;
dimming
for example, adjusting its colour to be
• S2 short press cycles the second/ more dim or shortening the trail.
sub-second hand modes
It’s a good idea to use auto-dimming
• S2 long press triggers NTP syn- so that the display isn’t too bright at
chronisation.
night. Calibrate the LDR as per the
Other possible options include tog- instructions last month or above, then
gling the AM/PM LEDs on and off, tog- adjust the maximum brightness so it’s
gling DST on/off, changing the master comfortably readable during the day
brightness, saving the settings and and the minimum brightness so it’s a
switching Wi-Fi on and off.
suitable brightness at night.
#11 Save/reset
The options are: Save will store the
current configuration in flash memory
so these same settings are used after
subsequent power-on events; Reload
saved settings discards any recent
changes and goes back to the settings
saved in flash (similar to rebooting
the unit); Restore factory defaults
loads the default settings but doesn’t
change what’s in flash unless you subsequently press Save; Reboot reboots
the unit as if its power was cycled off
and on again.
Screen 2: the VSCode project view
showing all the files needed to build
the firmware.
60
Silicon Chip
Setup and use
The instructions last month
explained how to get the Clock
Australia's electronics magazine
Waiting display colours
If the Wi-Fi network drops out, the
clock will attempt to automatically
reconnect periodically. When it can’t
display the time, or while it’s booting
up, it will show a coloured ‘waiting’
pattern:
• Red ■ – trying to connect to Wi-Fi
• Mauve/purple ■ – connected to
Wi-Fi, waiting for NTP time synchronisation
• Green ■ – Wi-Fi access point is
active (switch to internal or GNSS time
for time display)
• Blue ■ – GNSS mode selected, no
valid GNSS signal yet
• Yellow ■ – Valid GNSS signal
detected, no valid GNSS time yet
siliconchip.com.au
Firmware design
The firmware compiles to about
512kiB, barely more than a quarter
of the Pico W’s flash space. Much of
that is related to Wi-Fi and web serving, as required for the setup, remote
configuration and network time (NTP)
systems.
The program was developed using
VSCode and mostly uses libraries provided for use with the Raspberry Pi
Pico W. We initially used the WS2812
library on GitHub (https://github.com/
ForsakenNGS/Pico_WS2812) for
driving the LEDs but found several
bugs and deficiencies that we had to
address. So the library in our code is
not the same as the one on GitHub.
While we have our own GNSS code,
to keep things simple and robust, we
used the open-source minmea NMEA
parser from https://github.com/kosma/
minmea
The program is split into these main
files (see Screen 2):
• Bootloader.cpp/BootMetadata.
cpp: the code that handles booting the
latest version of the code and switching between the two flash ‘slots’ to
allow over-the-air (OTA) updating.
• ClockFace.cpp: logic for driving
the clock LEDs
• Config.cpp: storing the configuration data in RAM and flash
• dhcpserver.c & dnsserver.c:
example files supplied with
the Pico W SDK needed
for Wi-Fi access point
mode
• EEPROM.cpp:
saving and loading
from flash memory (EEPROM
emulation)
• GNSS.cpp:
helpers to feed
serial data to
the parser and
update the
clock with the
GNSS-derived
time
• The http_
server.cpp file:
works with the
Pico W’s HTTP
server to deliver
the various web
pages, update the
data on them and
react to configuration
changes in the web interface
siliconchip.com.au
• LDR.cpp: using the ADC to get
the LDR brightness, perform LDR calibration etc
• Log.cpp: code for logging messages and implement the USB serial
port debugging console
• minmea.c: the NMEA string parsing library
• NTP.cpp: wrappers and helpers
for NTP time
• OTA.cpp: the over-the-air firmware update code for writing new
firmware to flash memory
• RGBClock.cpp: the main file that
does start-up initialisation, runs the
main loop, and handles S1/S2 button presses
• Timezone.cpp: our own timezone
implementation
• WiFi.cpp: helpers for setting up
a Wi-Fi Access Point, connecting to a
network etc
• WS2812.cpp: the aforementioned
LED driving library
We’ll describe what the code does in
a little more detail later, but first let’s
look at some of the challenges we ran
into during development.
WS2812 library problems
This library is designed specifically for the Pico series, using the
Australia's electronics magazine
PIO peripheral to transmit data in the
serial format required by the WS2812B
‘NeoPixel’ LEDs, so it seemed perfect. It also looked quite simple and
straightforward. Our initial tests
showed it worked.
However, when we put the code in
a tight loop, updating the clock face
continuously, strange things started
to happen. The clock face would only
update occasionally, sometimes jumping whole seconds and being jittery,
despite the LED update routine being
called hundreds of times per second,
as confirmed by debugging over the
USB serial console.
So we had a close look at the library
code and realised that it had a serious bug! The way you update a daisy-
chained string of WS2812B LEDs is
that you transmit 24 bits of data for
each LED in the chain, then you leave
the serial line low for at least 50μs.
This is the ‘reset period’ that tells all
the LEDs in the chain to latch the data
and update their state.
The WS2812 library we used has no
provision for a reset period. It simply
feeds the data to the LEDs and then
returns. So if you call it again within
50μs, as might be the case in a tight
loop, there is no reset period and the
LEDs never update. The fact that our
LEDs updated at all was a coincidence
due to the code occasionally taking
longer!
Also, we realised the library
is not interrupt-safe because
a reset could be triggered
by an interrupt that
stalls the transmission
code for at least 50μs.
We
therefore rewrote the
library to change
a few things.
One important
change was to
add a loop at
the end of transmission to wait
until the last bit
has been sent,
then sleep for
500μs to make
sure the LEDs
reset and update
their states each
time (500μs may
seem excessive, but we
wanted it to be safe, and
the clock face still updates
at over 50Hz).
December 2026 61
We actually ended up putting the
wait/sleep at the top of the function,
since it does the same job that way,
but it means most of the actual LED
data transmission can happen simultaneously with other main loop code,
which improves code efficiency.
We also enabled the PIO FIFO
(first-in first-out queue) and switched
from software control to feeding the
PIO from RAM using DMA (direct
memory access). This way, even if
the processor is interrupted by, say,
a Wi-Fi event, the LED data transmission will complete without interruption. Some of this new code is shown
in Listing 1.
Another change we made to the
WS2812 library was to add the ability
to accumulate LED data in the array.
As initially provided, you could only
set a pixel colour. But say the hour
hand is red, the minute hand is blue
and they are at the same position. We
want that to show up as a purple LED
(red + blue). So we modified the setPixelColor() function to produce an
addPixelColor() function as well.
Time zone difficulties
The next bit of ‘fun’ I had was with
time zones. The Pico W’s C library is
compatible with the standard Unix C
library that uses a “TZ” environment
variable to set the time zone.
Once set, functions like localtime_r() can be used to convert a Unix
timestamp to the local time, which
can be used to update the clock display. The specific code in the Pico W
example is:
struct timespec ts;
get_time_utc(&ts);
struct tm tm;
pico_localtime_r(&ts.tv_sec, &tm);
// DMA completion only means the last word has been
// written to the PIO FIFO. It may not have finished
// shifting out yet.
while (!pio_sm_is_tx_fifo_empty(pio, sm))
tight_loop_contents();
// Allow the final 24-bit word to finish shifting,
// followed by the WS2812 reset/latch interval.
sleep_us(500);
dma_channel_set_read_addr(dma_chan, data, false);
dma_channel_set_trans_count(dma_chan, length, true);
dma_channel_wait_for_finish_blocking(dma_chan);
Listing 1: the modified WS2812::show() function that uses DMA instead of a
blocking loop to transfer data to the LEDs.
case WIFI_CONNECTING:
if (link == CYW43_LINK_UP) {
console_logf(“wifi: link up”);
wifi_state = WIFI_CONNECTED;
wifi_state_since_us = now;
Wi-Fi conundrums
start_mdns(&cyw43_state.netif[CYW43_ITF_STA]);
if (!httpd_started) {
console_logf(“httpd start”);
cyw43_arch_lwip_begin();
httpd_init();
cyw43_arch_lwip_end();
httpd_started = true;
}
console_logf(“ntp start”);
start_ntp();
console_logf(“Wi-Fi connected, IP=%s”, ip4addr_ntoa
(netif_ip4_addr(&cyw43_state.netif[CYW43_ITF_STA])));
}
Listing 2: some of the Wi-Fi state machine logic for bringing the network
interface up and down cleanly.
62
Silicon Chip
The first two lines get the current
UTC time from the internal real-time
clock (which is synchronised to NTP
once per hour). The last two lines then
extract the local time (date, hours, minutes, seconds etc) from that data using
the current time zone per the “TZ”
environment variable.
This all works as long as you never
change the “TZ” variable! If you do, it
will just keep using the old time zone,
no matter what you do. This particularly baffled me (and my AI assistant)
as we checked the underlying code
and it seemed correct.
The Pico’s library code caches the
time zone data after extracting it from
the TZ variable, but it seemed to have
the code to correctly update the cache
if it detected a change in the contents
of the TZ variable.
But despite verifying we were definitely updating the TZ variable, there
was nothing we could do to get it to
use any time zone specified other than
the very first one.
Obviously, there is a caching
bug somewhere in the library, but I
couldn’t figure out a way around it.
This is a non-starter in our application, since the user can change the
time zone information at any time via
the web interface. So I had to write my
own timezone functions, converting
our time zone configuration and timestamp directly into local time, rather
than relying on the library functions.
This is probably a bit more efficient
too, but annoying that I had to ‘reinvent the wheel’.
You can find the resulting functions in the Timezone.cpp file, some
of which is shown in Listing 3.
Australia's electronics magazine
While it’s impressively easy to connect to a Wi-Fi network with the Pico
W (or to create an access point), there
are quite a few steps beyond simply connecting that you need for it
to work properly, and some of them
have pitfalls.
Annoyingly, if you do things in the
wrong order, the Pico W has a tendency to ‘panic’, print an error and
stop running code.
You can’t even use the normal
reflash procedure in that case; you
have to pull out the USB cable, hold
down the BOOTSEL button, and plug
it back in. That’s annoying when the
module is mounted on the back of a
PCB.
siliconchip.com.au
After connecting to a Wi-Fi network,
we need to start the mDNS and NTP
services. mDNS allows you to access
the Clock’s web page via its name in
the URL opened with a web browser,
instead of having to figure out its IP
address (which can change) and putting that into the URL instead.
Alternatively, if creating a Wi-Fi
access point (so that you can connect to the Clock’s Wi-Fi network,
rather than the other way around),
we instead have to start the DHCP
and DNS servers (implemented in the
files dhcpserver.c and dnsserver.c,
respectively).
The DHCP server is needed to
assign the client (your computer/tablet/phone) an IP address; otherwise,
it will just disconnect from the network. In this mode, the role of DNS
is similar to that of mDNS in Network mode, allowing you to look up
and connect to the web configuration
page by name.
The catch with these is in switching
modes, which you might do, for example, during set-up. The Clock will start
in access point mode and you connect
to its network. You can then scan for
Wi-Fi networks, select an SSID and
enter a password.
At that point, it needs to ‘tear
down’ its Access Point, including
shutting down DNS and DHCP. It
can then connect to the Wi-Fi network specified, after which it has to
start the mDNS and NTP services. If
these aren’t done in the correct order
and a service is left behind that was
connected to a Wi-Fi network that’s
gone, the Pico W stack can ‘panic’
and crash the whole thing.
We also have to set the device’s host
name at various points.
So there was quite a bit of work
in developing and testing the Wi-Fi
code so that it can switch modes
back and forth without crashing the
device. Some of the code is shown
in Listing 2.
Wi-Fi library bugs
To provide a Wi-Fi signal strength
(RSSI) indication on the status screen,
when the status was queried through
the HTTP server (httpd), we initially
called the cyw43_wifi_get_rssi() function and report the result.
This mostly worked, but periodically it would fault and print the
message “[CYW43] do_ioctl(0, 127,
4): timeout”.
siliconchip.com.au
Using AI to aid development
A large portion of the code for this project was written manually, but there
were certain areas of ‘drudgery’ where I enlisted an artificial intelligence system to help.
For example, the “http_server.cpp” file has a function that needs to parse
a JSON array from the web interface and extract one of several dozen possible configuration changes, then call the appropriate function to perform the
update. That is not hard code to write, but it is a very repetitive function with
many similar sections.
I drafted some specifications regarding the various settings and what they
did, explaining the context to the AI. It produced the whole function in under
a minute. It would have probably taken me an hour to write and another hour
to test and debug it. It worked first time.
The AI is very useful because it understands all the Pico W APIs, while I
have not programmed a Pico W before. It was able to tell me all sorts of esoteric configuration changes and code snippets for doing things like enabling
NTP and the HTTP server. I could have read the documentation and figured
it out myself —eventually. What took it minutes would have taken me hours.
I wrote a lot of the actual structure of the code and critical functions, like
clock face handling, myself. In other sections of the code, for example, managing the Wi-Fi state, my role was more like a manager. I explained what was
required, provided existing code and used the AI to write helper functions,
which I then integrated into the code and tested.
Naturally, the AI-generated code has to be tested, so I went through and
checked that every function worked as expected. I would have had to do the
same had I written all the code manually. The only code that had to be significantly changed after being written was the Wi-Fi state machine code, which
was fairly complex and required some debugging. The AI helped me refactor
that until it worked well. In total, this cut the development down from what I
estimate would be a couple of weeks to just a few days.
static bool daylight_saving_active(time_t utc) {
if (settings.dst_mode == RGBClockSettings::DST_OFF)
return false;
if (settings.dst_mode == RGBClockSettings::DST_ON)
return true;
struct tm utc_tm;
if (!gmtime_r(&utc, &utc_tm))
return false;
int year = utc_tm.tm_year + 1900;
time_t start = dst_transition_utc(year, true);
time_t end = dst_transition_utc(year, false);
if (start == (time_t)-1 || end == (time_t)-1)
return false;
if (start < end) {
// Northern hemisphere:
// e.g. March -> October.
return utc >= start && utc < end;
}
else {
// Southern hemisphere:
// e.g. October -> April.
return utc >= start || utc < end;
}
}
Listing 3: one of the functions needed for our ‘roll your own’ daylight savings
logic to replace the faulty logic in the Pico W’s library.
Australia's electronics magazine
December 2026 63
The occasional RSSI query failure
wouldn’t matter except that it caused
the whole clock to stop functioning for
several seconds each time, including
updating the clock face. That made it
very annoying.
The solution I came up with was to
move the cyw43_wifi_get_rssi() function call into the main loop twice a second and store the latest RSSI value in
a variable that the HTTP server function can access. That fixed it.
Apparently, calling the cyw43_
wifi_get_rssi() function from within
the HTTP context (possibly inside an
interrupt) is not allowed, although that
isn’t clearly mentioned in the documentation.
More severe Wi-Fi problems
The next problem was subtler and
took a lot of time to debug since it
sometimes wouldn’t occur for several
days and the cause wasn’t obvious.
The clock would connect to the Wi-Fi
network and get NTP time reliably. It
would then refresh NTP time hourly
and remain up to date. It would do this
for hours or days.
Eventually, it would drop off the
Wi-Fi network; it no longer appeared
in the Wi-Fi base station’s client list.
However, the Wi-Fi system on the Pico
W believed it was still connected!
I had built a Wi-Fi state machine that
would do things like automatically
reconnect if the connection dropped. I
figured that was necessary, given that
you might have to reboot the router, or
it might occasionally freeze. (This is
something I think the Pico W library
really should handle by itself, but it
doesn’t).
However, that won’t work if the Pico
W is reporting that the network is still
connected when it isn’t.
I had difficulty reproducing this
problem reliably enough to fully discover why it happens, but I came up
with a solution nonetheless. If Wi-Fi
appears to be working but the NTP
time hasn’t been synchronised in over
two hours, that means two synchronisation events were missed, suggesting a network problem. In that case,
we force reconnection to the Wi-Fi
network.
Assuming that’s successful, it
should trigger an immediate NTP
update, and the time will be refreshed.
We continue to display the time normally during this process, and it
should seamlessly update once the
Wi-Fi connection is working again.
Over-the-air updating
The Clock supports over-the-air
(OTA) firmware updating, so new firmware can be installed without connecting the Pico W to a computer. This is
particularly useful if the Pico W is surface-mounted (so a USB cable can’t be
int main()
{
BootMetadata metadata;
boot_metadata_get(&metadata);
BootSlot permanent = (BootSlot)metadata.permanent_slot;
BootSlot trial = (BootSlot)metadata.trial_slot;
BootSlot target = permanent;
if (trial != BOOT_SLOT_NONE) {
if (boot_metadata_set(permanent, BOOT_SLOT_NONE, false))
target = trial;
}
if (!slot_valid(target)) {
BootSlot alternative = target == BOOT_SLOT_A ?
BOOT_SLOT_B : BOOT_SLOT_A;
if (slot_valid(alternative))
target = alternative;
else
while (true)
tight_loop_contents();
}
boot_slot(target);
}
Listing 4: the main bootloader logic that starts the Clock code.
64
Silicon Chip
Australia's electronics magazine
easily plugged in) or otherwise difficult to access after the Clock has been
assembled or mounted.
We considered using the code
found at https://github.com/usedbytes/
picowota to provide over-the-air
updating for the Pico W. However, it
requires a special tool to upload new
firmware, and we thought it would be
more convenient to use the existing
web interface so that all you need to
perform an update is a web browser
on a Wi-Fi-enabled device.
The Pico W has 2MiB of flash memory, while the Clock firmware only
occupies about 512kiB. We take advantage of the remaining space by storing
two copies of the application firmware
in separate areas of flash, referred to
as slots A and B, each of which is just
under 1MiB. A small bootloader determines which slot should be run when
the Pico W starts.
The 2MiB of flash on the Pico W
board is occupied by the bootloader
(32kiB, although only a fraction of that
is currently used), two copies of the
bootloader metadata (two 4kiB pages),
the two program slots (972kiB each)
and the space reserved for saved settings (64kiB, again much larger than
currently required).
Firmware update files contain two
versions of the same program: one
linked to run from slot A and the
other linked to run from slot B (as
the addresses vary). A small 16-byte
header identifies the file, specifies the
length of each image and contains a
32-bit CRC checksum for each one.
The update is uploaded through
the Clock’s normal web interface.
Because the complete update file is
over 1MiB and is much too large to
hold in the Pico W’s RAM, the HTTP
server processes it as a stream. If the
Clock is currently running the firmware in slot A, it ignores the slot-A
image and writes the slot-B image to
flash. If it is running from slot B, it
does the reverse.
A CRC is calculated as the new
image is received and compared with
the value stored in the update-file
header. The programmed flash is also
read back and checked before the
update is accepted. This ensures that
a truncated or corrupted upload cannot be selected for execution.
Once a new image has been received
and verified, the bootloader metadata is changed so that the new slot
will be tried once on the next reboot.
siliconchip.com.au
Screen 3: the Clock’s log messages
at power-up, shown in the VSCode
Serial Monitor tab.
Importantly, the previously working
firmware remains untouched.
The bootloader then starts the new
firmware (see Listing 4). If it runs
successfully, after a few seconds, it
updates the bootloader metadata to
make itself the permanent version. If
instead the new firmware crashes or
fails before it can do this, the bootloader will return to the previous
known-good slot on the following
reboot.
This also makes interrupted updates
relatively harmless. If power is lost
while slot B is being updated, for
example, slot A is still intact and
remains the selected firmware. The
bootloader is only instructed to try
the new slot after the complete image
has been received and its checksum
has been verified.
The result is a simple form of automatic rollback: updating the Clock
does not overwrite the firmware that
is currently running, and a new version only becomes permanent after it
has successfully booted.
This system is pretty safe from corrupting the firmware. However, there’s
a remote possibility that new firmware
could run long enough to make itself
the default but still be broken. In that
case, you can hold switches S1 & S2 at
power-up and the bootloader will boot
the other, hopefully still good version
of the firmware.
When the code is compiled in
VSCode, the makefile and Python
scripts produce the following files:
• RGBClock-full.uf2: this is the file
to initially program onto the Pico W
over the USB connection. It includes
the bootloader and main program.
• RGBClock-update.bin: this is the
file to upload for OTA updates. It’s
siliconchip.com.au
similar to the UF2 file, but omits the
bootloader and uses a simplified file
structure.
• RGBClock.elf/bin: the file that
will be uploaded automatically if you
click the “Run” button in VSCode. It
will work, except for OTA updates,
because it doesn’t include the bootloader.
Logging and debugging
Because it was difficult tracking
down some of the inherent problems
with the Pico W and its libraries mentioned above, I implemented a logging
and debugging system over the USB
serial interface. If you plug the Clock
into a USB port and open its virtual
serial interface in a terminal program,
each time something important happens, you will see a fairly detailed
message.
These messages are timestamped
with both the time since the clock was
powered up in fractional seconds and
with the current time on the clock (if
available) – see Screen 3.
These messages are also recorded
in an internal buffer so that you can
connect to the USB serial interface
later and run the “dmesg” command
(it needs to be terminated with an LF).
That will show you the 128 most recent
messages with timestamps.
This is very useful if, for example,
the Wi-Fi interface stops working and
you want to see why. You can also
issue numerous other commands over
this interface, including some to display the current status, display the
settings, change all settings, and trigger actions like connecting to Wi-Fi
or rebooting. Issue the “help” command to see the full list of commands
available.
Australia's electronics magazine
Pico 2 W support?
We considered testing this code on
a Pico 2 W, but since the Pico W does
everything we need, it didn’t seem
necessary.
We think it’s likely the code would
compile and run correctly on a Pico 2
W except for the bootloader and OTA
updating code. The Pico 2 W has an
official OTA update solution, so you
could remove our bootloader/OTA
mechanism and just use that.
In short: we haven’t tried it, but if
you have any sort of programming
skills, you could likely get it working. We would be interested in hearing from any reader who attempts this.
Conclusion
The Clock is a straightforward hardware design that makes it very easy to
show accurate local time via NTP or
satellite signals. It’s easy to build and
set up, and the web interface over
Wi-Fi makes customising it simple at
any time. It hides some pretty complex
firmware inside the Pico W module to
make all that possible.
Readers with programming skills
may be interested in downloading the
VSCode project, including the source
code, and customising it. The OTA
update process can be used for simpler changes, or for any future firmware updates we may release. Programming it over a USB cable would
be better for those wishing to make
significant changes.
The general code structure could
actually be useful in a range of
Wi-Fi-enabled projects; features like
Wi-Fi setup using Access Point mode,
OTA firmware updates and web configuration could be adapted for a range
SC
of different projects.
December 2026 65
|