🛠 Bouton avec pull-up interne

Arduino Nano (Classic, ATmega328P) · debutant · Schéma : IMG-nano-classic-debutant-2-nanoc-bouton-pullup.svg — bouton poussoir câblé entre la broche 2 et la masse, pull-up interne activé, LED builtin témoin

Objectives

Learn how to read a digital input using the internal pull-up resistor (INPUT_PULLUP): the button connects the pin to ground, so the state is INVERTED (LOW = pressed). We turn on the built-in LED (pin 13) when the button is pressed, using a simple software debounce.

Materials

Complete code

// Bouton sur broche 2, pull-up interne : LOW = appuyé
const uint8_t BTN = 2;
const uint8_t LED = LED_BUILTIN;   // broche 13
uint32_t dernierAppui = 0;

void setup {
  pinMode(BTN, INPUT_PULLUP);      // pull-up interne activé
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
}

void loop {
  bool appuye = (digitalRead(BTN) == LOW);   // logique inversée
  if (appuye && millis - dernierAppui > 50) {
    dernierAppui = millis;                 // anti-rebond 50 ms
    digitalWrite(LED, !digitalRead(LED));
    Serial.println(F("appui !"));
  }
}

Expected verification

Upon upload, the LED is off and the serial monitor (9600 baud) displays "press!" on each distinct button press, toggling the LED state. With no button pressed, digitalRead(BTN) returns HIGH thanks to the internal pull-up: the circuit remains stable at rest.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials