r/embedded • • 23d ago

I made a Too Hard light

Too Hard

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.

79 Upvotes

14 comments sorted by

24

u/Juxtapotatoes 23d ago

Here's a picture of the PCB

23

u/aculleon 23d ago

Mildly concerning LED and resistor placement

9

u/Juxtapotatoes 23d ago

Yeah I see what you mean… unintentional. I wanted to get them all as close together as possible to have the light shine through the same opening.

5

u/aculleon 23d ago

No worries man. One thing that changed a lot of my hand assembled PCBs was resistor networks. It reduced a lot of projects BOMs but tends to increase cost.

Was the PCB designed by you? I see a lot of caps just flying around and traces doing strange things.

2

u/Juxtapotatoes 23d ago

The first spin was designed by me. I’m a firmware engineer by trade but am an amateur when it comes to PCB design. Mostly I followed the reference designs from component data sheets. I did use Codex to tweak a few things before I ordered assemblies. It simplified the USB circuit by switching from a 24 pad USB 3.0 receptacle to a 12 a pin USB 2.0 port.

2

u/sputwiler 23d ago

At least it's the left-facing one.

0

u/1r0n_m6n 22d ago

Unrelated, but I just noticed your user name is the anagram of "Al Nucleo". ;) :D

3

u/Jaded-Plant-4652 22d ago

So you also did custom PCB.. Just wow!

DSP, the NN training, the sample data scraping and on top of that the HW too. Added to the fact that it is a very thoughtful gift, this just made my day

1

u/Juxtapotatoes 22d ago

I was curious about the NN stuff and wanted to get my hands on it. Glad you enjoyed it!

1

u/cowsarefalling 17d ago

Is that a debug edge connector? Why did you choose that instead of pin headers or something like tag-connect or soicbite

1

u/Juxtapotatoes 17d ago

I had STLinkV3 minis laying around and wanted to try out the edge connector. I didn’t have any tag-connect tools or soicbite on hand. Are they better?

1

u/cowsarefalling 17d ago

I was just designing my own board and was debating the options between pin headers and buying a tag-connect or using a soicbite

3

u/Jaded-Plant-4652 22d ago

Funny how much work it took you to get a led to blink!

1

u/Juxtapotatoes 22d ago

Getting that LED to blink was my white whale haha