🛠 Réagir aux boutons A et B

M5StickC Plus2 (M5Stack) — ESP32-PICO-V3-02 "stick" wearable kit (SKU K016-P2) · debutant · Schéma : IMG-m5stack-stick-c-plus-2-debutant-2-m5stick-c2-boutons-a-b.svg — Vue de profil de la M5StickC Plus2 avec les boutons A (face, G37), B (flanc, G39) et C (alimentation, G35) annotés et un compteur affiché à l'écran.

Objectives

Read the three buttons on the M5StickC Plus2: A = G37 (front), B = G39 (side), C = G35 (also the power button/wake source). Count presses, reset with B, and understand why C is a button to handle with caution.

Hardware

Complete code

// Boutons A/B/C — broches INPUT ONLY G37/G39/G35 (jamais en sortie !)
#include <M5Unified.h>

uint32_t compteur = 0;

void setup {
  auto cfg = M5.config;
  M5.begin(cfg);
  M5.Display.setRotation(1);
  M5.Display.fillScreen(TFT_BLACK);
  // M5Unified configure les broches en entrée avec pull-up interne
  // (en écriture directe : pinMode(37, INPUT_PULLUP), etc.)
}

void loop {
  M5.update;
  if (M5.BtnA.wasPressed) {          // bouton A = G37
    compteur++;
    M5.Speaker.tone(2093, 80);         // bip court : buzzer passif sur G2 (PWM)
  }
  if (M5.BtnB.wasPressed) {          // bouton B = G39
    compteur = 0;
    M5.Speaker.tone(440, 80);
  }
  if (M5.BtnC.wasPressed) {          // C = G35 = aussi la touche power !
    M5.Display.fillScreen(TFT_BLACK);  // appui long > 6 s = extinction (sans USB)
  }
  M5.Display.setCursor(20, 50);
  M5.Display.setTextSize(4);
  M5.Display.setTextColor(TFT_YELLOW, TFT_BLACK);
  M5.Display.printf("%-6lu", (unsigned long)compteur);
  vTaskDelay(10 / portTICK_PERIOD_MS);
}

Expected verification

Each press on A increments the large yellow counter and makes the passive buzzer (G2, PWM tone output only) "beep". B resets the counter to zero. A short press on C clears the screen; holding it for more than 6 seconds without USB completely turns off the device.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials