🛠 Lire un bouton avec résistance de pull-up interne
Objectives
Learn how to read a digital input on the Nano Every using a push-button, utilizing the internal pull-up resistor (INPUT_PULLUP) instead of an external resistor. The board features 22 digital I/O pins (0 to 21) operating at 5 V logic.
Hardware
- 1 × Arduino Nano Every
- 1 × push-button
- 1 × jumper wire (pin 2 → button, button → GND)
- Micro-USB cable
Complete Code
// Bouton sur broche 2, logique inversée grâce au pull-up interne
const uint8_t BTN = 2;
bool etatPrecedent = HIGH;
void setup {
pinMode(LED_BUILTIN, OUTPUT); // broche 13
pinMode(BTN, INPUT_PULLUP); // pull-up interne : HIGH au repos
Serial.begin(9600);
}
void loop {
bool etat = digitalRead(BTN); // LOW = pressé
if (etat != etatPrecedent) {
delay(30); // anti-rebond logiciel grossier
etat = digitalRead(BTN);
Serial.println(etat == LOW ? "Bouton presse" : "Bouton lache");
etatPrecedent = etat;
}
digitalWrite(LED_BUILTIN, etat == LOW ? HIGH : LOW);
}
Expected Verification
Connect the button between pin 2 and GND: at rest, digitalRead returns HIGH (internal pull-up) and LED 13 is off; when pressed, the reading changes to LOW, the LED turns on, and the serial monitor (9600 baud) displays the event. No external resistor is required.
Common Errors
- Wiring the button between the pin and 5 V: with
INPUT_PULLUP, the logic is inverted and you short-circuit the pull-up; keep the connection pin → GND. - Forgetting
INPUT_PULLUP: the pin floats, causing readings to change randomly. - Confusing it with the classic Nano: the pinout is identical, but the Every uses an ATmega4809 — make sure to select the correct board in the IDE.
- Raw mechanical bouncing: without debouncing, a single press generates multiple lines; the
delay(30)in this tutorial is a minimal educational approach, prefermillisfor real projects. - Note: the PWM pins on this board are only 3, 5, 6, 9, and 10 — pin 2 is a simple digital I/O, which is sufficient here.
Other tutorials for this board
Code examples for this board
multi-serie.inoArduinoadc-filtre.inoArduinointerrupt-bouton.inoArduinonvm-consignes.inoArduinosommeil-every.inoArduinowatchdog-secours.inoArduino