🛠 Bouton poussoir et console série
Objectives
Read a digital input with a push button, handle software debouncing, and communicate with the serial console via the SAMD21 native USB (Full-Speed, VID 0x2341, application PID 0x0053).
Hardware
- 1 Arduino MKR WAN 1300
- 1 push button
- 1 10 kΩ resistor (or internal pull-up)
- Micro-USB cable
Complete code
// Logique 3,3 V, max ~7 mA par broche (famille MKR).
const uint8_t BTN = 2;
volatile uint32_t appuis = 0;
void onBouton { appuis++; }
void setup {
Serial.begin(115200);
while (!Serial) {} // attend l'ouverture du moniteur
pinMode(BTN, INPUT_PULLUP); // bouton vers GND
attachInterrupt(digitalPinToInterrupt(BTN), onBouton, FALLING);
}
void loop {
static uint32_t dernier = 0;
if (millis - dernier > 500) {
dernier = millis;
Serial.print("etat=");
Serial.print(digitalRead(BTN) ? "relache" : "APPUIE");
Serial.print(" appuis=");
Serial.println(appuis);
}
delay(5);
}
Expected verification
Each press increments the counter; the state changes on the displayed line twice per second. while (!Serial) prevents losing the first lines.
Common errors
- Button connected to 3.3 V without a resistor: short circuit or floating state; prefer
INPUT_PULLUPto GND. - Bouncing: multiple increments per press, add debouncing (
millis) or a 100 nF capacitor. - Forgetting
while (!Serial)on native USB: first lines are lost.
Other tutorials for this board
Code examples for this board
emission-p2p.inoArduinoreception-p2p.inoArduinoreglage-sp.inoArduinometeo-lora.inoArduinotrame-crc.inoArduinorelais-radio.inoArduino