🛠 Micro PDM et buzzer : sonomètre interactif
Objectives
Use the SPM1423 MEMS PDM microphone (CLK=G0, DATA=G34) to measure sound level (peak-to-peak over a sample window) and control the passive buzzer (G2, PWM tone only — no audio playback): a sound meter that beeps when you shout. G0 also serves as a boot strap pin: do not connect anything to it.
Hardware
- 1× M5StickC Plus2 (v2)
- USB-C cable for uploading
- Arduino IDE / PlatformIO, M5Unified (built-in PDM support)
Complete code
// Sonometre PDM — micro SPM1423 (CLK=G0, DATA=G34) + buzzer passif G2
#include <M5Unified.h>
const int N_ECH = 1024;
int16_t echantillons[N_ECH];
float niveau = 0;
void setup {
auto cfg = M5.config;
M5.begin(cfg);
// Configuration explicite du micro PDM interne
auto mic = M5.Mic.config;
mic.pin_clock = 0; // CLK du SPM1423 = G0
mic.pin_data = 34; // DATA = G34
mic.is_stereo = false;
mic.sample_rate = 16000; // 16 kHz
M5.Mic.begin;
M5.Display.setRotation(1);
M5.Display.setTextColor(TFT_YELLOW, TFT_BLACK);
}
void loop {
M5.update;
M5.Mic.readRaw(echantillons, N_ECH); // 1024 echantillons 16 bits
int32_t mini = 32767, maxi = -32768;
for (int i = 0; i < N_ECH; i++) {
if (echantillons[i] < mini) mini = echantillons[i];
if (echantillons[i] > maxi) maxi = echantillons[i];
}
niveau = (float)(maxi - mini) / 655.36f; // 0..100 %
// Jauge horizontale 240 px
M5.Display.fillRect(0, 60, 240, 20, TFT_BLACK);
int w = (int)(niveau * 2.4f);
M5.Display.fillRect(0, 60, w, 20, niveau > 70 ? TFT_RED : TFT_GREEN);
M5.Display.setCursor(0, 20);
M5.Display.printf("Niveau: %3.0f %%", niveau);
if (niveau > 70.0f) M5.Speaker.tone(1760, 120); // buzzer G2 (PWM)
vTaskDelay(60 / portTICK_PERIOD_MS);
}
Expected verification
In silence, the gauge stays near 0; clapping or whistling makes it rise, and above 70% it turns red with a buzzer beep. >
Common errors
- Thinking you can play music through the speaker: the Plus2 only has a passive buzzer (PWM tone on G2) — no audio DAC, no wave playback.
- Loading G2 at startup: it is an ESP32 strap pin, an external load can block booting.
- Wrong PDM pins: CLK=G0 / DATA=G34 (official SPM1423 pins); swapping them results in a flat or noisy signal.
- Sampling too slowly by reading
Mic.readsample by sample: usereadRawin blocks. - Expecting a decibel level: here it is a normalized peak-to-peak ratio, to be calibrated against a reference source.
- G0 is also the flash mode strap pin: a circuit connected to it can prevent uploading.
Other tutorials for this board
Code examples for this board
hello-stick.inoArduinoboutons.inoArduinobuzzer.inoArduinoinclinometre.inoArduinohorloge.inoArduinoir-tv.inoArduino