🛠 Lire un bouton avec une résistance de pull-up interne

Arduino Micro · debutant · Schéma : IMG-micro-debutant-2-micro-bouton-pull-up.svg — Bouton à connexion branché entre la broche 2 et GND sur une Arduino Micro, avec la résistance de pull-up interne symbolisée sur l'entrée

Objectives

Read a push button on the Arduino Micro using the internal pull-up resistor (20 to 50 kΩ), without external components, and understand active-low logic.

Materials

Complete code

// Bouton sur broche 2 vers GND, pull-up interne actif.
// Logique active-low : lecture LOW quand le bouton est enfonce.
const uint8_t BTN = 2;   // les broches 0,1,2,3,7 portent aussi une interruption externe
bool etatLed = false;

void setup {
  pinMode(BTN, INPUT_PULLUP);     // pull-up interne 20-50 kOhm
  pinMode(LED_BUILTIN, OUTPUT);   // broche 13
  Serial.begin(9600);             // Serial = port USB CDC
}

void loop {
  if (digitalRead(BTN) == LOW) {  // bouton enfonce
    delay(50);                    // anti-rebond grossier
    if (digitalRead(BTN) == LOW) {
      etatLed = !etatLed;
      digitalWrite(LED_BUILTIN, etatLed);
      Serial.println(etatLed ? "LED ON" : "LED OFF");
      while (digitalRead(BTN) == LOW) {}  // attendre le relachement
    }
  }
}

Expected verification

Each press toggles LED 13 and displays LED ON / LED OFF in the Serial Monitor (Micro's serial port). No visible bouncing: one press results in a single line.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials