🛠 Bouton USER et console série USB-C
Objectives
Read the USER button on the Nano ESP32 using an internal pull-up resistor (INPUT_PULLUP) and display its state along with a press counter on the native USB-C serial console. This is the foundation of any human-machine interaction on the board.
Hardware
- 1 × Arduino Nano ESP32 + USB-C cable
- The onboard USER button (the BOOT button is used to enter bootloader mode, not for sketches)
- >
Complete code
const uint8_t BTN = 2; // D2, cf. exemple GPIO officiel du Nano ESP32
uint32_t appuis = 0;
uint32_t derniereMs = 0;
void setup {
Serial.begin(115200); // série native USB-C
pinMode(BTN, INPUT_PULLUP); // pull-up interne : appui = LOW
Serial.println("Appuyer sur USER...");
}
void loop {
bool appuye = (digitalRead(BTN) == LOW);
uint32_t now = millis;
if (appuye && now - derniereMs > 200) { // anti-rebond logiciel 200 ms
derniereMs = now;
appuis++;
Serial.printf("Appui n°%lu\n", (unsigned long)appuis);
}
delay(10);
}
Expected verification
Each press of the USER button prints "Press #1", "Press #2", etc. in the serial monitor at 115200 baud, without duplicates thanks to software debouncing.
Common errors
- INPUT without pull-up: the input floats and the console displays phantom presses.
- Forgetting debouncing: a single physical press generates multiple lines.
- Confusing BOOT and USER: BOOT (with reset) is used for the DFU bootloader, not for testing.
- Monitor closed or wrong baud rate: nothing is displayed even though the code is running.
Other tutorials for this board
Code examples for this board
connexion-wifi.inoArduinopoint-acces.inoArduinoble-capteur.inoArduinoneoled-rgb.inoArduinoadc-12bits.inoArduinopreferences.inoArduino