🛠 Bouton avec pull-up interne
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
- 1 × Arduino Pro Mini (ATmega328P, one of the two models)
- 1 × push button
- Male-to-female wires; no external resistor needed thanks to the internal pull-up (~20-50 kΩ)
- The board's built-in LED (pin 13)
- FTDI/CP2102 adapter on the 6-pin connector for uploading and viewing the serial monitor
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
- Expecting HIGH when the button is pressed: with INPUT_PULLUP, it is LOW that signals a press; wiring the button to VCC would require a pull-down resistor.
- Button wired pin→VCC without a resistor: short circuit when pressed, causing the board to restart (the linear regulator cuts out).
- Mechanical bouncing: without the 50 ms delay, a single press triggers multiple counts.
- Floating pin: forgetting INPUT_PULLUP leaves the pin picking up noise and the LED blinking on its own; on the 3.3 V model, the logic is identical but at the 3.3 V level.
Other tutorials for this board
Code examples for this board
clignotante.inoArduinobouton-poussoir.inoArduinopwm-fade.inoArduinocapteur-lumiere-ldr.inoArduinoservo-balayage.inoArduinoruban-neopixel.inoArduino