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

Arduino Nano Every · debutant · Schéma : IMG-nano-every-debutant-2-nanoevery-bouton-pullup.svg — Bouton à bouton-poussoir câblé entre la broche 2 et GND sur une Nano Every, avec le symbole de la résistance de pull-up interne dessinée dans la puce

Objectives

Learn how to read a digital input on the Nano Every using a push-button, utilizing the internal pull-up resistor (INPUT_PULLUP) instead of an external resistor. The board features 22 digital I/O pins (0 to 21) operating at 5 V logic.

Hardware

Complete Code

// Bouton sur broche 2, logique inversée grâce au pull-up interne
const uint8_t BTN = 2;
bool etatPrecedent = HIGH;

void setup {
  pinMode(LED_BUILTIN, OUTPUT);   // broche 13
  pinMode(BTN, INPUT_PULLUP);     // pull-up interne : HIGH au repos
  Serial.begin(9600);
}

void loop {
  bool etat = digitalRead(BTN);   // LOW = pressé
  if (etat != etatPrecedent) {
    delay(30);                    // anti-rebond logiciel grossier
    etat = digitalRead(BTN);
    Serial.println(etat == LOW ? "Bouton presse" : "Bouton lache");
    etatPrecedent = etat;
  }
  digitalWrite(LED_BUILTIN, etat == LOW ? HIGH : LOW);
}

Expected Verification

Connect the button between pin 2 and GND: at rest, digitalRead returns HIGH (internal pull-up) and LED 13 is off; when pressed, the reading changes to LOW, the LED turns on, and the serial monitor (9600 baud) displays the event. No external resistor is required.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials