🛠 Allumer une LED avec un bouton poussoir
Objectives
Read a digital input and control an output: the button/LED pair, the foundation of any interaction. You will discover internal pull-up resistors (INPUT_PULLUP), the active-low configuration, and the phenomenon of mechanical bounce.
Materials
- 1 Arduino UNO R3 + USB-B cable
- 1 breadboard, connecting wires
- 1 push button
- 1 LED + 1 330 Ω resistor (to limit current below the 20 mA per pin ceiling)
- Wiring: button between pin 2 and GND (no external resistor needed, the internal pull-up does the job); LED + 330 Ω between pin 13 and GND.
Complete code
// Bouton sur pin 2 (vers GND), LED sur pin 13.
const uint8_t BTN = 2;
const uint8_t LED = 13;
void setup {
pinMode(BTN, INPUT_PULLUP); // pull-up interne : repos = HIGH, appuyé = LOW
pinMode(LED, OUTPUT);
Serial.begin(9600);
}
void loop {
bool appuye = (digitalRead(BTN) == LOW); // logique inversée (actif-bas)
digitalWrite(LED, appuye ? HIGH : LOW);
Serial.println(appuye ? "appuye" : "repos");
delay(10);
}
Expected verification
LED off at rest, on when pressed. The Serial Monitor (9600 baud) displays the state continuously. If you remove the wire from pin 2 without the pull-up, the input floats and the display becomes random — this is precisely the behavior that INPUT_PULLUP corrects.
Common errors
- Forgetting INPUT_PULLUP and wiring the button to +5 V without a pull-down resistor: floating input, erratic readings.
- Inverting the logic: with this setup, pressed == LOW. Many beginners test for == HIGH and think the button is broken.
- Forgetting the LED resistor: the LED burns out or the pin saturates (max 20 mA).
- Mechanical bounce causes the reading to jitter: invisible here, critical as soon as you count presses — see the multitasking millis tutorial with 30 ms debounce.
- Wrong serial port: the UNO resets every time the port is opened (the bootloader intercepts the first bytes); the first characters may be missing.
Other tutorials for this board
- Faire clignoter la LED intégrée de l'UNO R3
- Faire varier la luminosité avec un potentiomètre (ADC + PWM)
- Thermomètre de lumière : LDR, seuils et hystérésis
- Télémétrie série robuste vers le Traceur série
- Multitâche non bloquant : heartbeat + bouton avec debounce
- Data logger sur EEPROM : persistance sans usure
Code examples for this board
clignotante.inoArduinobouton-poussoir.inoArduinopwm-fade.inoArduinocapteur-lumiere-ldr.inoArduinoservo-balayage.inoArduinoruban-neopixel.inoArduino