🛠 Micro PDM et buzzer : sonomètre interactif

M5StickC Plus2 (M5Stack) — ESP32-PICO-V3-02 "stick" wearable kit (SKU K016-P2) · avance · Schéma : IMG-m5stack-stick-c-plus-2-avance-2-m5stick-c2-micro-pdm.svg — M5StickC Plus2 avec le microphone MEMS PDM SPM1423 (CLK=G0, DATA=G34) qui capte une onde sonore, jauge de niveau sur l'écran, et buzzer passif sur G2 relié en PWM.

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials