bouton-led.ino

ESP32-S3 · File : bouton-led.ino · Language : Arduino · Use case : Compter les appuis sur un bouton avec anti-rebond

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-S3 board (dual-core Xtensa with vector instructions, Wi-Fi + BLE 5, native USB) such as a DevKitC-1, XIAO or T-Display-S3, powered over USB. 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 « ESP32S3 Dev Module », enable « USB CDC On Boot », then Upload. Many boards put a WS2812 on GPIO48.

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 = 48;
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;
}

← Back to the board