toughc5_gpio_button_buzzer.ino

M5Stack ToughC5 Weatherproof IoT Dev Kit (ESP32-C5) · File : toughc5_gpio_button_buzzer.ino · Language : C++ (Arduino) · Use case : Contrôler le buzzer passif via un bouton GPIO externe avec anti-rebond matériel.

Basic physical interaction example using an external button and the integrated buzzer.

Uses the M5ToughC5 library. Wire the button between the chosen GPIO pin (here G18) and GND. The buzzer is connected to GPIO2. Compile with the 'M5ToughC5' board in the Arduino IDE.

toughc5_gpio_button_buzzer.ino

#include <M5ToughC5.h>

// Broche GPIO pour le bouton (ex: G0 ou une broche libre des connecteurs HY2.0)
// Note: Vérifier le schéma pour les broches libres non utilisées par l'écran/RTC
const int BUTTON_PIN = 18; 
const int BUZZER_PIN = 2; // Buzzer passif connecté à GPIO2

void setup() {
    M5.begin();
    Serial.begin(115200);
    
    // Configuration du bouton en entrée avec pull-up interne
    pinMode(BUTTON_PIN, INPUT_PULLUP);
    
    // Configuration du buzzer en sortie
    pinMode(BUZZER_PIN, OUTPUT);
    digitalWrite(BUZZER_PIN, LOW);
    
    Serial.println("M5ToughC5 GPIO & Buzzer Demo");
}

void loop() {
    // Lecture du bouton (LOW = pressé à cause du pull-up)
    if (digitalRead(BUTTON_PIN) == LOW) {
        // Générer un son simple (fréquence ~2kHz)
        tone(BUZZER_PIN, 2000, 100);
        delay(150); // Petit délai pour l'anti-rebond logiciel simple
        Serial.println("Button Pressed - Beep!");
    } else {
        noTone(BUZZER_PIN);
    }
    delay(10);
}

← Back to the board