
Building the firmware for a battery-powered CO detector with a standards-modeled alarm curve
Challenge
A consumer hardware team needed firmware for a compact, battery-powered carbon monoxide detector with a full-color display, one that didn't just sound an alarm at a single fixed PPM threshold but responded the way professional CO alarms do - faster at dangerous concentrations, with more tolerance at borderline ones - while also functioning as a small USB-C power bank, running for a long time on a single charge, and staying reliable enough to trust unattended as a safety device.
Solution
We built the firmware on an STM32G0, reading live CO concentration from a UART-connected gas sensor module and driving a color ST7789 LCD through the UGUI graphics library, with a custom TTF-to-bitmap font pipeline and a converted bitmap icon set for the on-screen gauge, numbers, and status icons. Rather than one alarm threshold, we implemented a four-tier concentration/time alarm curve matching the response-time behavior used by industry-standard CO alarms: sustained readings of 30-70 ppm sound the alarm within 8 hours, 70-150 ppm within 1 hour, 150-400 ppm within 11 minutes, and 400+ ppm within 5 minutes, with the on-screen reading itself shifting from white to amber to red as concentration climbs, giving an immediate visual cue ahead of the timed audible alarm. Instead of burning power on a software countdown, each tier's delay is scheduled directly on the STM32's hardware RTC alarm, so the MCU can stay idle and the alarm still fires exactly on time; if the reading changes tier, or drops out of an alarm range entirely, before the RTC alarm fires, the pending alarm is cancelled and rescheduled for the new tier automatically. For power, we drove a Monolithic Power Systems MP2722 USB-C battery charger IC over I2C, building a state machine that distinguishes normal charging, a completed charge, and the device acting as a power source for another device over the same port - in that last mode, the firmware watches for a voltage dip under load and backs the charge current down automatically rather than letting the battery sag. Temperature and humidity readings come from an AHT20 sensor alongside the CO reading, and the independent watchdog is refreshed throughout the main loop and every display update so the device recovers on its own from any lockup instead of needing a manual reset.
Results
The result is a CO detector that behaves like the professional alarms its owners already trust: it alarms on the schedule real safety standards define, sooner at dangerous concentrations and with headroom at borderline ones, scheduled entirely on hardware so the MCU isn't burning battery life running a software countdown. Its USB-C power path can charge the device or, in reverse, safely power another device off its own battery without overloading it, and the combination of independent-watchdog recovery and RTC-backed alarm state gives the product the kind of unattended reliability a safety device needs to have.
Kick-start your project
Get in touch with our team to discuss your business needs and see how we can help with a project like this one.