sonometre.ino

M5StickC Plus2 (M5Stack) — ESP32-PICO-V3-02 "stick" wearable kit (SKU K016-P2) · File : sonometre.ino · Language : Arduino · Use case : Mesurer le niveau ambiant avec le micro du stick

The Plus2's PDM mic samples at 16 kHz: RMS and peak fall out of one loop and show as a colored bar. The red threshold at 85% matches a loud conversation at one meter.

Hardware required

An M5StickC Plus2 (ESP32-PICO-V3-02, 135×240 display, A/B/C buttons, passive buzzer on G2, IR + red LED on G19, MPU6886 IMU, microphone, 200 mAh battery). None; a sound source to test.

Wiring

No wiring — MEMS mic inside the case, behind its hole.

How it works

Mic.record fills a signed 16-bit buffer; the sum of squares gives RMS, the max gives peak. The /100 mapping is an ear setting, not a dB standard. In the Arduino IDE (esp32 package), board « M5StickCPlus2 »; « M5StickCPlus2 » or M5Unified library. On the PMIC-less v2, hold button C for >2 s to power on; HOLD (G4) latches the power.

Going further

Audio warning (buzzer above threshold), or a day-long noise logger timestamping peaks.

sonometre.ino

// Micro SPM1423 (PDM) via M5Unified : niveau 0-100 en barre.
#include <M5Unified.h>

const int N = 1024;
int16_t tampon[N];

void setup() {
  auto cfg = M5.config();
  M5.Mic.setSampleRate(16000);
  M5.begin(cfg);
  M5.Display.setRotation(1);
}

void loop() {
  M5.Mic.record(tampon, N, 16000);      // 64 ms de 16 bits
  long long somme = 0;
  int16_t crete = 0;
  for (int i = 0; i < N; i++) {
    somme += (long)tampon[i] * tampon[i];
    crete = max(crete, (int16_t)abs(tampon[i]));
  }
  float rms = sqrtf((float)somme / N);
  int niveau = constrain((int)(rms / 100), 0, 100);

  M5.Display.fillRect(0, 0, 240, 135, TFT_BLACK);
  M5.Display.fillRect(4, 100, 232 * niveau / 100, 24,
                      niveau > 85 ? TFT_RED : TFT_GREEN);
  M5.Display.setCursor(4, 20);
  M5.Display.printf("RMS %6.0f  crete %d\n", rms, crete);
  M5.Display.printf("niveau %d %%\n", niveau);
  delay(100);
}

← Back to the board