🛠 Allumer une LED avec un bouton poussoir

Arduino UNO R3 · debutant · Schéma : IMG-uno-r3-debutant-2-bouton-poussoir-led.svg — UNO R3 sur breadboard : bouton entre la pin 2 et GND, LED + 330 Ω entre la pin 13 et GND.

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials