sonometre.ino

M5Stack Core (ESP32) — Basic / Gray family (K001 / K002) · File : sonometre.ino · Language : Arduino · Use case : Mesurer le niveau sonore avec le micro interne

The case's MEMS mic (GPIO34) streams 8-bit samples: the root-mean-square turns noise into a 0-100% level shown as a bar. The backup sound meter is three math functions away.

Hardware required

An M5Stack Core Basic/Gray (ESP32 in an enclosure with a 320×240 TFT, A/B/C buttons, speaker, microSD slot, 110 mAh battery). None (mic in the case); a sound source to test.

Wiring

No wiring. The mic sits behind the lid holes: don't block them.

How it works

Mic.readPixels samples ~8 kHz on the internal PDM bus; RMS (sum of squares / N, square root) is the signal energy measure. The 0-64 → 0-100% mapping is ear-calibrated. In the Arduino IDE (esp32 package), board « M5Stack-Core-ESP32 »; install the « M5Stack » (or M5Unified) library from the Library Manager, then Upload.

Going further

Trigger the buzzer above a threshold (level warning), or log peaks to the SD card.

sonometre.ino

// Microphone PDM interne : RMS -> barre de niveau + clignotement.
#include <M5Stack.h>

const int N = 2048;
uint8_t tampon[N];
unsigned long precedent = 0;

void setup() {
  M5.begin();
  M5.Mic.begin();
  M5.Lcd.setTextSize(2);
}

void loop() {
  if (millis() - precedent < 100) return;
  precedent = millis();

  M5.Mic.readPixels(tampon, N);          // 2048 echantillons 8 bits
  long somme = 0;
  for (int i = 0; i < N; i++) {
    long e = tampon[i] - 128;            // recentre autour de zero
    somme += e * e;
  }
  float rms = sqrtf((float)somme / N);
  int niveau = constrain(map((int)rms, 0, 64, 0, 100), 0, 100);

  M5.Lcd.fillRect(10, 60, 300, 40, TFT_BLACK);
  M5.Lcd.fillRect(10, 60, 3 * niveau, 40,
                  niveau > 80 ? TFT_RED : TFT_GREEN);
  M5.Lcd.setCursor(10, 110);
  M5.Lcd.printf("RMS %.1f -> %d %%\n", rms, niveau);
}

← Back to the board