buzzer.ino

M5StickC Plus2 (M5Stack) — ESP32-PICO-V3-02 "stick" wearable kit (SKU K016-P2) · File : buzzer.ino · Language : Arduino · Use case : Jouer de la musique avec le buzzer passif

The stick's passive buzzer (G2) plays whatever frequency you feed it: two arrays of notes/durations and the scale comes out of the case hole. The audio feedback of an alert is one line.

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.

Wiring

No wiring; the buzzer is soldered to G2 inside the case. Volume is limited by the buzzer size — put your ear to the hole.

How it works

M5.Spk.tone(freq, ms) sets a square-wave timer on G2; the call blocks for the requested duration. The 20 ms gap between notes separates attacks. 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

A musical thermometer (frequency = temperature), or a full Morse code with two durations.

buzzer.ino

// Buzzer passif sur G2 — M5Unified gere le timer.
#include <M5Unified.h>

const uint16_t NOTES[] = {392, 392, 523, 523, 659, 659, 523,   // sol sol do do mi mi do
                          494, 494, 587, 587, 698, 698, 587};
const uint16_t DUREES[] = {220, 220, 220, 220, 220, 220, 440,
                           220, 220, 220, 220, 220, 220, 440};
const int NB = sizeof(NOTES) / sizeof(uint16_t);

void setup() {
  auto cfg = M5.config();
  M5.begin(cfg);
  M5.Spk.begin();
  M5.Display.setRotation(1);
  M5.Display.setTextSize(2);
  M5.Display.drawString("Bouton A = musique", 10, 50, 2);
}

void loop() {
  M5.update();
  if (M5.BtnA.wasPressed()) {
    for (int i = 0; i < NB; i++) {
      M5.Spk.tone(NOTES[i], DUREES[i]);
      M5.Display.drawChar(110, 90, 'o', TFT_BLACK, TFT_YELLOW, 4);
      delay(DUREES[i] + 20);
    }
  }
  delay(10);
}

← Back to the board