bouton-led.ino
A button between GPIO4 and GND, pulled up internally: every clean press is counted and printed. The 40 ms debounce filter kills the mechanical bounce that would otherwise double-count.
Hardware required
An ESP32-C3 board (RISC-V 160 MHz, Wi-Fi + BLE 5) such as a DevKitM-1 or SuperMini, powered over USB (native on the C3). 1 push button (one leg GPIO4, other GND).
Wiring
Button on GPIO4 to GND; the INPUT_PULLUP resistor is internal. Avoid GPIO6-11 (flash) and GPIO34-39 (input-only, no pull-up).
How it works
The sketch stores the last read level and the timestamp of the last change; an edge only counts once the level holds stable for 40 ms. INPUT_PULLUP inverts logic: pressed = LOW. In the Arduino IDE (esp32 package), select « ESP32C3 Dev Module », enable « USB CDC On Boot » if needed, then Upload. GPIO9 is often the BOOT button; keep GPIO8 free at boot.
Going further
Double-click (measure the interval between presses), or deep-sleep wake on that same GPIO via esp_sleep_enable_ext0_wakeup.
bouton-led.ino
// Bouton sur GPIO4 (vers GND, résistance interne) + compteur anti-rebond.
const int BOUTON = 4;
const int LED = 8;
const unsigned long ANTI_REBOND = 40; // ms
bool dernier = HIGH, stable = HIGH;
unsigned long bord = 0;
int compteur = 0;
void setup() {
pinMode(BOUTON, INPUT_PULLUP);
pinMode(LED, OUTPUT);
Serial.begin(115200);
}
void loop() {
bool lu = digitalRead(BOUTON);
unsigned long t = millis();
if (lu != dernier) bord = t;
if (t - bord >= ANTI_REBOND && lu != stable) {
stable = lu;
if (stable == LOW) { // front d'appui (logique inversée)
compteur++;
digitalWrite(LED, HIGH);
Serial.printf("Appui n°%d\n", compteur);
} else {
digitalWrite(LED, LOW);
}
}
dernier = lu;
}