🛠 Lire un bouton avec une résistance de pull-up interne
Objectives
Read a push button on the Arduino Micro using the internal pull-up resistor (20 to 50 kΩ), without external components, and understand active-low logic.
Materials
- 1 Arduino Micro
- 1 push button
- 1 wire between pin 2 and one leg of the button, the other leg to GND
- Micro-USB cable
Complete code
// Bouton sur broche 2 vers GND, pull-up interne actif.
// Logique active-low : lecture LOW quand le bouton est enfonce.
const uint8_t BTN = 2; // les broches 0,1,2,3,7 portent aussi une interruption externe
bool etatLed = false;
void setup {
pinMode(BTN, INPUT_PULLUP); // pull-up interne 20-50 kOhm
pinMode(LED_BUILTIN, OUTPUT); // broche 13
Serial.begin(9600); // Serial = port USB CDC
}
void loop {
if (digitalRead(BTN) == LOW) { // bouton enfonce
delay(50); // anti-rebond grossier
if (digitalRead(BTN) == LOW) {
etatLed = !etatLed;
digitalWrite(LED_BUILTIN, etatLed);
Serial.println(etatLed ? "LED ON" : "LED OFF");
while (digitalRead(BTN) == LOW) {} // attendre le relachement
}
}
}
Expected verification
Each press toggles LED 13 and displays LED ON / LED OFF in the Serial Monitor (Micro's serial port). No visible bouncing: one press results in a single line.
Common errors
- Forgetting INPUT_PULLUP: the input floats and the Monitor displays random changes.
- Wiring the button to 5 V instead of GND without an external resistor: short circuit when the pull-up is active.
- Expecting HIGH when the button is pressed: with a pull-up, pressing gives LOW (active-low).
- Using Serial1 by reflex: on the Micro, Serial1 is the UART TTL on pins 0/1, invisible in the Serial Monitor.
Other tutorials for this board
- Faire clignoter la LED embarquée (et comprendre les LED TX/RX)
- Lire un capteur analogique sur le convertisseur 10 bits
- Transformer le Micro en clavier USB (HID) avec bouton de sécurité
- Pont Serial vers Serial1 : passerelle USB vers TTL
- Piloter un servo moteur avec le PWM et un potentiomètre
- Sortir des sentiers : registres AVR et interruption externe
Code examples for this board
clignotante.inoArduinobouton-led.inoArduinoclavier-auto-tape.inoArduinosouris-cercle.inoArduinotelecommande-media.inoArduinobouton-clic-souris.inoArduino