🛠 Bouton avec pull-up interne
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
- 1 × classic Arduino Nano (ATmega328P)
- 1 × push button
- Male-female jumper wires; no external resistor needed thanks to the internal pull-up (~20-50 kΩ)
- The board's built-in LED (pin 13)
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
- Expecting HIGH when the button is pressed: with INPUT_PULLUP, it is LOW that signals a press; wiring the button to 5 V would require a pull-down resistor.
- Button wired pin→5 V without a resistor: short circuit 5 V/ground when pressed, causing the board to enter protection mode (reset).
- Mechanical bouncing: without the 50 ms delay, a single press triggers multiple counts.
- Floating pin: forgetting INPUT_PULLUP leaves the pin susceptible to noise, turning the LED on by itself.
Other tutorials for this board
Code examples for this board
buzzer-melodie.inoArduinoservo-sweep.inoArduinoultrason-hcsr04.inoArduinolcd-i2c.inoArduinoeeprom-contre.inoArduinosommeil-watchdog.inoArduino