Pico 2 (RP2350) I2S INMP441 MEMS microphone real-time syncopated rhythm pattern detector and tempo (BPM) estimator.
  • C 83.3%
  • CMake 16.7%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-08-01 19:53:30 -07:00
ideas Move strategy and blueprint markdown files into ideas/ subdirectory 2026-08-01 19:50:26 -07:00
.gitignore Initial commit of noise detector visualizer and transient detector 2026-08-01 19:23:11 -07:00
CMakeLists.txt Evolve project to consume lf-inmp441 as a Conan package 2026-08-01 19:53:30 -07:00
conanfile.txt Evolve project to consume lf-inmp441 as a Conan package 2026-08-01 19:53:30 -07:00
main.c Widen transient detector range to 375Hz and lower sensitivity thresholds 2026-08-01 19:40:43 -07:00
pico_sdk_import.cmake Initial commit of noise detector visualizer and transient detector 2026-08-01 19:23:11 -07:00
README.md Update README to match the rhythm pattern matcher and BPM estimator implementation 2026-08-01 19:48:54 -07:00

Pico 2 (RP2350) I2S INMP441 Rhythm Pattern Matcher & BPM Estimator

This project implements a real-time, tempo-agnostic rhythm pattern matching system for the INMP441 I2S MEMS Microphone running on the Raspberry Pi Pico 2 (RP2350). It uses the static library driver lf-inmp441 located in the parent directory.


⚡ Features

  • Transient Detector: Isolates the low-to-mid frequency band (62.5 Hz to 375.0 Hz) and triggers on rapid energy attacks (e.g., hand claps or desk taps).
  • Tempo-Agnostic Pattern Matching: Uses a rolling 5-interval queue to detect a specific syncopated rhythm template (3:1:2:1:1 relative ratios) corresponding to a 4/4 measure of:
    • Quarter, Eighth Rest, Eighth, Quarter, Two Eighths (Hit step grid: [ x . . x x . x x ])
  • Phase Independence: Evaluates all 5 circular shifts of the template so it can lock onto the rhythm regardless of which beat in the measure it begins tracking.
  • Real-time BPM Estimation: Computes the estimated tempo in Beats Per Minute (BPM) based on the total duration of the matched measure:
    \text{BPM} = \frac{240,000}{\text{Measure Duration in ms}}
  • Onboard LED Feedback: Flashes the Pico 2 onboard green LED (GP25) briefly on each detected beat.
  • Consecutive Bar Tracking: Counts consecutive bars matched and triggers an achievement message when 16+ consecutive bars are locked in.
  • Automatic Silence Reset: Resets the bar counter to zero if no beats are detected for more than 5 seconds.

🔌 Hardware Wiring Guide

The INMP441 operates as an I2S slave. The Pico 2 acts as the I2S master, generating the bit clock (SCK) and word select clock (WS/LRCK) signals.

Important

The PIO clock generation code requires that the WS pin be connected to the GPIO pin immediately following the SCK pin (SCK pin + 1).

INMP441 Pin Pin Marking Function Pico 2 GP Pin Pico 2 Physical Pin Connection Description
VDD VDD Power Input 3V3 (OUT) Pin 36 3.3V Power Supply (1.8V - 3.6V max)
GND GND Ground GND Pin 3 (or any GND) Power Ground (0V)
SCK SCK Serial Bit Clock GP20 Pin 26 SPI/I2S Clock generated by Pico 2
WS WS Word Select (LRCK) GP21 Pin 27 Frame sync signal (Left/Right channel clock)
SD SD Serial Data Out GP22 Pin 29 Digital audio data line to Pico 2
L/R L/R Channel Select GND GND Pull to GND for Left channel mono output

🛠️ Build and Compiling Instructions

Prerequisites

Ensure you have the Raspberry Pi Pico SDK installed and the PICO_SDK_PATH environment variable set. For example:

export PICO_SDK_PATH=/home/bethany/code/pico-sdk

Compiling the Code

  1. Navigate to the noise-detector directory:
    cd noise-detector
    
  2. Create a build directory and run CMake (specifying pico2 as the target board):
    mkdir build
    cd build
    cmake -DPICO_BOARD=pico2 ..
    
  3. Compile the project:
    make -j$(nproc)
    

This will produce the compiled firmware files in the build/ directory, including noise_detector.uf2.


⚡ Flashing the Firmware

  1. Unplug your Pico 2 from the computer.
  2. Hold down the white BOOTSEL button on the board.
  3. While holding the button, plug the Pico 2 back into your computer's USB port.
  4. Release the BOOTSEL button. The Pico 2 will mount as a USB mass storage device named RP2350.
  5. Drag and drop the noise_detector.uf2 file into the mounted directory.
  6. The board will flash, reboot automatically, and start running the visualizer.

🖥️ Monitoring the Output

You can monitor the output from a terminal over USB CDC Serial. On Linux, you can run:

minicom -b 115200 -o -D /dev/ttyACM0

Output Example

When you perform the syncopated rhythm (e.g. by tapping your fingers on the desk), the console will log the intervals and matches:

Beat transient! Interval: 704 ms

=======================================================
>>> [MATCH] Bar #24 Detected! (Est. Tempo: 90.4 BPM)
    Intervals: [704, 320, 352, 928, 352] ms
=======================================================

!!! [ACHIEVEMENT] Target met: 16+ consecutive bars locked! !!!

If you stop tapping or break the rhythm, the system resets:

[SILENCE] Rhythm broken. Resetting counter.