bouton-poussoir.ino
A button wired to pin 2 lights the LED on 13 on each press. Software debouncing (30 ms) filters the mechanical contact bounce. The pull-up resistor is the microcontroller internal one (INPUT_PULLUP).
Hardware
An Arduino Uno R3 (ATmega328P, 5 V, 16 MHz) powered over USB or a 7-12 V barrel jack. 1 push button (one leg to pin 2, the other to GND).
Wiring
Button between pin 2 and GND. No external resistor: INPUT_PULLUP enables the internal 20-50 kΩ resistor.
How it works
The reading is only accepted once stable for 30 ms. A press pulls the pin to ground (LOW with PULLUP) and lights the LED; releasing turns it off.
Running it
In the Arduino IDE, select the « Arduino Uno » board and the matching serial port, then click Upload. Open the serial monitor at 9600 baud to follow the messages.
Variants
For a latching toggle button, flip ledAllumee on each edge. For active-high wiring, add a 10 kΩ resistor to GND and test HIGH.
bouton-poussoir.ino
// Bouton poussoir avec anti-rebond logiciel (machine à états).
const int BOUTON = 2; // broche du bouton (vers GND, résistance interne)
const int LED = 13;
bool etatPrecedent = HIGH;
bool etatStable = HIGH;
bool ledAllumee = false;
unsigned long dernierBord = 0;
const unsigned long ANTI_REBOND = 30; // ms
void setup() {
pinMode(BOUTON, INPUT_PULLUP); // pas de resistance externe : on utilise celle du MCU
pinMode(LED, OUTPUT);
Serial.begin(9600);
}
void loop() {
bool lecture = digitalRead(BOUTON);
unsigned long maintenant = millis();
if (lecture != etatPrecedent) {
dernierBord = maintenant; // le signal bouge : on remet le chrono a zero
}
if (maintenant - dernierBord >= ANTI_REBOND && lecture != etatStable) {
etatStable = lecture;
if (etatStable == LOW && !ledAllumee) { // front d'appui (LOW en PULLUP)
ledAllumee = true;
Serial.println(F("Appui !"));
} else if (etatStable == HIGH && ledAllumee) {
ledAllumee = false;
}
digitalWrite(LED, ledAllumee ? HIGH : LOW);
}
etatPrecedent = lecture;
}