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 Pro Mini (ATmega328P, 5 V/16 MHz or 3.3 V/8 MHz variants) wired to a USB-serial adapter (FTDI) or an ICSP programmer. 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
Select « Arduino Pro Mini » in the IDE (5 V/16 MHz or 3.3 V/8 MHz per your model), ATmega328P processor, the right port, then Upload. The adapter must be wired DTR-DTR, RX-TX, TX-RX, VCC-VCC, GND-GND.
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;
}