🛠 Bouton utilisateur et console série USB
Objectives
Read the state of the USER button on the Feather nRF52840 and display it on the USB serial console. The nRF52840 has a native USB controller: the Serial object in the Arduino core is a CDC-TinyUSB implementation, so there is no external serial chip. You will also learn about software debouncing (millis) and non-blocking delays.
Hardware
- 1× Adafruit Feather nRF52840 Express
- 1× micro-USB data cable
- Arduino IDE with the Adafruit nRF52 Boards core
Complete Code
// nrf52840-bouton-console-uart.ino
const uint8_t BUTTON_PIN = 9; // bouton USER (alias D9)
// >
// sa broche exacte (D9 vient d'exemples CircuitPython) ; vérifier au multimètre
// ou avec le schéma officiel si rien ne se passe.
uint8_t lastState = HIGH;
uint32_t lastDebounce = 0;
uint32_t pressCount = 0;
void setup {
Serial.begin(115200); // console USB CDC native
pinMode(BUTTON_PIN, INPUT_PULLUP); // le bouton USER relie la broche à GND
Serial.println("Console Feather nRF52840 prete");
}
void loop {
uint8_t state = digitalRead(BUTTON_PIN);
uint32_t now = millis;
if (state != lastState && (now - lastDebounce) > 30) {
lastDebounce = now;
lastState = state;
if (state == LOW) {
pressCount++;
Serial.print("Appui n°");
Serial.println(pressCount);
}
}
if (Serial.available) { // echo de la console vers la carte
char c = Serial.read;
Serial.print("Recu: ");
Serial.println(c);
}
}
Expected Verification
Open the serial monitor at 115200 baud: each press of the USER button displays "Press n°N" without spurious bounces (no duplicates within 1 ms). Any character typed into the console is echoed back.
Common Errors
INPUTwithout pull-up: the pin floats and the console fills with false presses — useINPUT_PULLUP.- Debouncing with
delay(30): you miss subsequent presses; compare timestamps usingmillisas shown above. - Incorrect USER pin: if nothing reacts, test D9 and neighboring pins, as the exact reference is not confirmed in the documentation.
- Monitor closed during upload: with native CDC, close the monitor if the port is busy.
Other tutorials for this board
Code examples for this board
neopixel_pulsation.pyPythonled_bouton.pyPythonentrees_num.pyPythonanalogique_lumiere.pyPythonservo_pwm.pyPythonscanner_i2c.pyPython