bouton-led.ino

ESP8266 (ESP-12E module) · File : bouton-led.ino · Language : Arduino · Use case : Compter des appuis bouton avec anti-rebond

A button on D3 with internal pull-up, cleanly counted by the 40 ms filter. On NodeMCU GPIO0 is also the boot pin: holding it at power-on would drop the board into flash mode.

Hardware required

An ESP-12E module (ESP8266EX, 2.4 GHz Wi-Fi) mounted on a NodeMCU / Wemos D1 mini board, powered over micro-USB. 1 push button (D3 to GND).

Wiring

Button between D3 and GND; internal pull-up enabled. Boot-safe alternative: D1/D2 (GPIO4/5). Avoid GPIO15 (TX) and GPIO16.

How it works

The level is sampled continuously; an edge only counts after 40 ms of stability. INPUT_PULLUP inverts logic: pressed = LOW, hence the == LOW test. In the Arduino IDE, install the « esp8266 » package, select « NodeMCU 1.0 (ESP-12E Module) » then Upload (hold GPIO0 LOW if flashing fails). The A0 ADC reads 0-1 V (NodeMCU divider: 0-3.3 V).

Going further

Two buttons for a menu (up/down/OK), or deep-sleep wake on that same pin.

bouton-led.ino

// Bouton sur D3 (GPIO0, vers GND) + compteur anti-rebond 40 ms.
const int BOUTON = 0;    // D3 — attention : GPIO0 = broche de boot !
const int LED = 2;       // D4
const unsigned long ANTI_REBOND = 40;

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) {
      compteur++;
      digitalWrite(LED, LOW);                    // LED activee (actif-bas)
      Serial.printf("Appui n°%d\n", compteur);
    } else {
      digitalWrite(LED, HIGH);
    }
  }
  dernier = lu;
}

← Back to the board