🛠 Bouton USER et console série USB-C

Arduino Nano ESP32 · debutant · Schéma : IMG-nano-esp32-debutant-2-nanoesp32-bouton-user-console.svg — Doigt appuyant sur le bouton USER, câble USB-C vers un PC affichant la console série.

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials