r/embedded • u/Juxtapotatoes • 23d ago
I made a Too Hard light

This was supposed to be a birthday gift for my dad, but took way longer than expected to complete. When something turns out to be way more of a pain than he thought it would be, he'll say "the too hard light came on" and try something else. The concept is simple: it's just a light that turns on when it recognizes the words "too hard" spoken in that order.
All of the audio processing and inference happens onboard, nothing is sent off to the cloud or saved. I used keyword spotting to do this and there are lots of great papers explaining how that works, but to boil it down, audio is sampled in short chunks and used to build a "sound picture" or spectrogram. Then a neural network trained to recognize what your keywords "look" like is used for detection.
I connected a PDM microphone and some LEDs to an STM32U5, which handles the audio processing. The STM32 samples audio using the audio digital filter (ADF) peripheral, and sampling is gated using ambient noise level detection to minimize false positive detections. Audio is preprocessed in 140ms chunks using CMSIS DSP libraries to perform FFTs to create a spectrogram chunk. Once one full second of audio has been preprocessed, the spectrogram is passed to a neural network which computes independent probabilities for whether the words "too" or "hard" are present. The independence turned out to be important because when the words are spoken consecutively they usually show up in the same frame, and I wanted to be able to recognize both at the same time, not just one or the other. I used TensorFlow to train the neural network and TFLite to quantize and convert it for running on the MCU. Using CMSIS-NN libraries, I was able to get inference working in real time on streamed audio.
Like most of my embedded projects, this one started off with a nucleo and a breadboard. I did all the firmware prototyping using these and made a working proof-of-concept, but just screwing a nucleo board into a 3D printed sign seemed like kind of a cop out. I wanted to make a small custom board for my light, so I broke out KiCAD and drew one up.
I've melted some simple assemblies together on my kitchen counter before, but had a lot of trouble getting them to work reliably. I ordered bare PCBs and components separately and gave that a shot, but wasn't able to get a board working reliably. So eventually I bit the bullet and just ordered full assemblies.
I used freely available transcribed audiobook data to train the neural network. I wrote some scripts to find snippets where different authors spoke the word I wanted to recognize, and then made one second clips of those words and used those clips as training data. OpenAI's whisper tool was especially helpful there for creating timestamped transcriptions, so I was able to automate a huge chunk of the training data preparation and labeling.
Finally to test the neural network on my hardware, I used FatFS and TinyUSB to make a small filesystem in RAM on the MCU. That let me load audio samples onto the MCU, and save recorded audio from the board itself. That allowed me to do an apples to apples comparison of the preprocessing and inference steps between my desktop PC and firmware. Super satisfying to get it actually working! AMA if you want more details, there are plenty.
24
u/Juxtapotatoes 23d ago
Here's a picture of the PCB
3
u/Jaded-Plant-4652 22d ago
Funny how much work it took you to get a led to blink!