🛠 Bouton poussoir et console série

Arduino MKR WAN 1300 · debutant · Schéma : IMG-mkr-wan-1300-debutant-2-mkrwan-bouton-console.svg — bouton poussoir câblé entre la broche 2 et la masse sur breadboard

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials