🛠 Bouton poussoir et console série

Arduino MKR WAN 1310 · debutant · Schéma : IMG-mkr-wan-1310-debutant-2-mkrwan1310-bouton-console-serie.svg — Bouton poussoir câblé entre la broche 1 et GND sur une MKR WAN 1310, moniteur série à l'écran.

Objectives

Read a push button on a digital pin of the MKR WAN 1310, count the presses, and display them on the serial console via the SAMD21's native USB. Reminders: 8 digital pins at 3.3 V, 8 external interrupts (0, 1, 4, 5, 6, 7, 8, 16/A1, 17/A2), 7 mA max per pin.

Hardware

Complete code

// Bouton sur broche 1 + console série — MKR WAN 1310
const int BUTTON = 1;   // broche compatible EINT pour un réveil future
const int LED    = 6;   // LED_BUILTIN
unsigned long appuis = 0;
unsigned long dernierAppui = 0;

void setup {
  pinMode(BUTTON, INPUT_PULLUP);
  pinMode(LED, OUTPUT);
  Serial.begin(115200);
  while (!Serial);
  Serial.println("Appuyez sur le bouton (broche 1 -> GND)");
}

void loop {
  if (digitalRead(BUTTON) == LOW) {          // actif bas : tirage interne
    unsigned long maintenant = millis;
    if (maintenant - dernierAppui > 50) {    // anti-rebond logiciel 50 ms
      appuis++;
      dernierAppui = maintenant;
      Serial.print("Appui n°");
      Serial.println(appuis);
      digitalWrite(LED, HIGH);
    } else {
      digitalWrite(LED, LOW);
    }
  }
}

Expected verification

Each press displays "Press n°X" in the serial monitor (115200 baud) and turns on D6. The counter does not increase by several counts with a single press thanks to the debouncing.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials