🛠 Bouton avec pull-up interne

Arduino Pro Mini · debutant · Schéma : IMG-pro-mini-debutant-2-promini-bouton-pullup.svg — bouton poussoir câblé entre la broche 2 et la masse sur Arduino Pro Mini, pull-up interne activé, LED builtin témoin

Objectives

Learn how to read a digital input using the internal pull-up resistor (INPUT_PULLUP, 20-50 kΩ on the ATmega328P): 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. Pin 2 is a good choice: it is also the external interrupt INT0, useful for waking up the board from deep sleep in advanced tutorials.

Hardware

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(57600);             // meme debit que le bootloader d'usine
}

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 !"));            // F : la chaine reste en flash
  }
}

Expected verification

Upon upload, the LED is off and the serial monitor (57600 bauds) displays "press!" on each firm press of the button, alternating the LED state. With no button pressed, digitalRead(BTN) returns HIGH thanks to the internal pull-up: the circuit remains stable at rest. Note the use of F around strings: with 32 KB of flash, 2 KB of which is taken by the bootloader, every byte counts on the Pro Mini.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials