bouton-led.ino

LilyPad Arduino · File : bouton-led.ino · Language : Arduino · Use case : Allumer une LED quand un bouton est pressé, avec anti-rebond

A button wired to GND lights the LED while pressed. INPUT_PULLUP enables the internal resistor, and the 30 ms window filters the contact's mechanical bounce.

Hardware required

A LilyPad Arduino (ATmega168V/328V, 8 MHz, 3.3 V) with its sew tabs, programmed via an FTDI cable (breakout) and powered by 3.7 V (LiPo) or 5 V on V-REG. 1 push button.

Wiring

One button leg on pin 2, the other to GND; LED (with 330 Ω resistor) on pin 13 if external.

How it works

A reading counts only after 30 ms of stability; pressing pulls the pin to ground (LOW with pull-up) and lights the LED. In the Arduino IDE, select « LilyPad Arduino » and the FTDI port, then Upload. Conductive thread adds a few ohms of resistance: keep runs short and insulate crossings.

Going further

Turn it into a toggle by flipping a state on each edge, or wire a second button on pin 3.

bouton-led.ino

// Bouton sur la broche 2 (vers GND) + LED sur 13, anti-rebond logiciel.
const int BOUTON = 2;
const int LED = 13;
const unsigned long ANTI_REBOND = 30; // ms

bool dernier = HIGH, stable = HIGH;
unsigned long bord = 0;

void setup() {
  pinMode(BOUTON, INPUT_PULLUP); // resistance interne : rien d'externe
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  bool lu = digitalRead(BOUTON);
  unsigned long t = millis();
  if (lu != dernier) bord = t;
  if (t - bord >= ANTI_REBOND && lu != stable) {
    stable = lu;
    digitalWrite(LED, stable == LOW ? HIGH : LOW);
    Serial.println(stable == LOW ? F("Appui") : F("Lache"));
  }
  dernier = lu;
}

← Back to the board