Host-side audio processing and embedded inference on the RP2040.
An educational TinyML-style prototype that classifies short audio windows into baby cry, doorbell, smoke alarm, and background/other. A host computer captures audio and extracts 33 handcrafted features; a Raspberry Pi Pico runs logistic regression, softmax, and a hysteresis state machine, then drives its LED and reports results over USB serial.
Demo video · Hardware and build guide · Results and limitations · MIT License
Research and learning prototype, not a certified alarm, baby monitor, or safety device. Never rely on it for emergency detection. The microphone and feature extraction run on the host computer, not on the Pico.
flowchart LR
subgraph Host[Host PC / Python]
M[Microphone] --> F[Audio preprocessing and feature extraction]
F --> V[33 feature values]
end
V -->|USB CDC / CSV| N
subgraph Pico[RP2040 / C++]
N[Z-score normalization] --> L[Logistic regression and softmax]
L --> S[Per-class hysteresis FSM]
S --> LED[Onboard LED]
S --> R[Serial logs]
end
The offline training pipeline prepares labeled audio, extracts features, trains the classifier, and exports its coefficients and normalization parameters as a C++ header. The committed header allows inference without retraining.
| Component | Implementation |
|---|---|
| Audio input | 16 kHz mono; live 1.5 s window, 0.5 s input callback blocks |
| Preprocessing | Peak normalization, pre-emphasis, 25 ms frames, 10 ms frame hop, Hann window |
| Spectral features | 12 Goertzel bands with pooled mean and standard deviation (24 features) |
| Additional features | RMS, spectral centroid, rolloff and zero-crossing rate (mean/std); mean flatness (9 features) |
| Model | Four-class logistic regression with balanced class weights |
| Embedded inference | Z-score normalization, linear logits and stable softmax |
| Decision logic | Per-class thresholds and hysteresis; LED for active non-background classes |
| Communication | USB CDC, newline-delimited feature CSV, PING and RESET commands |
Use Python 3.11 or 3.12. Clone the repository and run the commands from its root.
git clone https://github.com/Ertugrulmutlu/Edge-AI-Sound-Classifier-on-Raspberry-Pi-Pico.git
cd Edge-AI-Sound-Classifier-on-Raspberry-Pi-Pico
python -m venv .venvActivate .venv with source .venv/bin/activate on Linux/macOS, or .\.venv\Scripts\Activate.ps1 in Windows PowerShell.
python -m pip install -r requirements.txt
python -m unittest discover -s tests -vThe tests use synthetic audio and model metadata; they do not need a microphone, dataset, or connected Pico. Linux systems may need PortAudio installed for the sounddevice import (e.g. libportaudio2 on Debian/Ubuntu). The legacy requirement.txt is retained as a historical environment snapshot.
Use the original Raspberry Pi Pico (RP2040, non-W), Pico SDK, CMake, and a compatible ARM toolchain. See deployment.md for SDK configuration, flashing, and serial details.
cmake -S . -B build -DPICO_BOARD=pico
cmake --build buildWith the SDK configured, the target produces build/edge_sound_classifier.uf2. Flash it using BOOTSEL and connect the Pico over USB. Set COM_PORT in Python_side/simulation_tool/simulation.py to the actual USB serial port (default COM3).
python Python_side/simulation_tool/simulation.pyThe host sends 33 comma-separated floating-point values per line; the Pico reports class probabilities, active FSM flags and the highest-probability class. Press Ctrl+C to stop the host. A microphone and USB serial access are required for live operation. Pico W uses a different onboard LED interface and is not supported unchanged by this firmware.
The raw dataset and extracted feature CSV are not included. Provide audio that you have permission to use, with independent recording sources for each class. The preprocessing script expects the following directory names (including their original spelling/case):
dataset/raw/
├── door_bel/
├── fire_alarm/
├── baby_cry/
└── Negativ/
Run from the root:
python Python_side/dataset_tool/bulk_cut_data.py
python Python_side/dataset_tool/feature_extraction.py
python Python_side/model_train/train_ml.pyThe scripts write prepared clips under dataset/prep/, features to features/featuresv1.csv, and new parameters to firmware/model_params.hpp. Use a clean prepared-data directory for a fresh run, as preprocessing writes newly named files. WAV inputs are the simplest option; other formats depend on available audio decoders.
Before deploying a new model, verify its metrics, label order, and 33-feature layout. Copy the generated header into cpp_side/ only if you intend to replace the committed model, then rebuild the firmware:
Copy-Item firmware/model_params.hpp cpp_side/model_params.hppTraining prefers a source-aware split within each class when at least two distinct sources are available per class; otherwise it falls back to a stratified snippet-level split. Normalization is fitted on training data only. Source identifiers are inferred from filenames and should be checked for correctness and cross-class overlap.
Historical evaluation reported in the original project README; the source dataset is unavailable here, so these results have not been independently reproduced. Recalculated from the recorded confusion matrix: 86.54% accuracy, 0.864 weighted F1, 312 test snippets.
| Class | Precision | Recall | F1 | Support |
|---|---|---|---|---|
| baby | 0.82 | 0.69 | 0.75 | 72 |
| doorbell | 0.79 | 0.90 | 0.84 | 72 |
| other | 0.86 | 0.88 | 0.87 | 96 |
| smoke_alarm | 1.00 | 0.99 | 0.99 | 72 |
Confusion matrix (rows = actual, columns = predicted):
| Actual / predicted | baby | doorbell | other | smoke_alarm |
|---|---|---|---|---|
| baby | 50 | 12 | 10 | 0 |
| doorbell | 4 | 65 | 3 | 0 |
| other | 7 | 5 | 84 | 0 |
| smoke_alarm | 0 | 0 | 1 | 71 |
The original evaluation used a stratified fallback split due to limited recording-source diversity. Overlapping windows from the same recordings may occur in train and test, so the numbers are not evidence of performance on unseen recording sources or real-world emergencies. The weakest recall was for baby cries. No measured firmware latency, RAM usage, energy consumption, or certified reliability results are provided.
The live host and firmware are prototype implementations: the host may transmit overlapping or repeated windows, and the FSM accumulates above-threshold messages while retaining state inside the hysteresis band. Three qualifying messages are not necessarily three independent observations. Training clips can also be longer than the live window. See the deployment guide for additional caveats.
.
├── Python_side/
│ ├── dataset_tool/
│ │ ├── bulk_cut_data.py
│ │ └── feature_extraction.py
│ ├── model_train/train_ml.py
│ └── simulation_tool/simulation.py
├── cpp_side/
│ ├── main.cpp
│ └── model_params.hpp
├── docs/deployment.md
├── tests/test_host.py
├── CMakeLists.txt
├── requirements.txt
├── requirement.txt
└── LICENSE
- Video: Pico live demo
- Part 1 — Data gathering and preprocessing
- Part 2 — Feature extraction and training
- Part 3 — Pico firmware and live demo
Built by Ertuğrul Mutlu. The existing MIT license applies to the repository; audio datasets may have separate rights.