🛠 Bouton poussoir et console USB

Arduino MKR GSM 1400 · debutant · Schéma : IMG-mkr-gsm-1400-debutant-2-mkrgsm-bouton-console.svg — Carte MKR GSM 1400 sur breadboard avec un bouton poussoir entre la broche D2 et la masse, câble micro-USB relié à un ordinateur affichant la console série.

Objectives

Wire a push button to a digital pin on the MKR GSM 1400 (3.3 V logic), read it using an internal pull-up resistor (INPUT_PULLUP), debounce the signal, and display each press on the USB console. This is a fundamental skill for any local interface: pins D0–D7 are digital, including 8 external interrupt-capable inputs.

Hardware

Complete Code

// Bouton sur D2 (vers GND) + console USB
const uint8_t BTN = 2;
volatile uint32_t appuis = 0;
uint32_t dernierMs = 0;

void onAppui {
  uint32_t maintenant = millis;
  if (maintenant - dernierMs > 50) {  // anti-rebond 50 ms
    appuis++;
    dernierMs = maintenant;
  }
}

void setup {
  pinMode(BTN, INPUT_PULLUP);          // logique active a l'etat BAS
  attachInterrupt(digitalPinToInterrupt(BTN), onAppui, FALLING);
  Serial.begin(9600);
  while (!Serial) {}                   // attend l'ouverture du moniteur USB
  Serial.println("MKR GSM 1400 - test bouton D2");
}

void loop {
  static uint32_t dernierAffiche = 0xFFFFFFFF;
  noInterrupts;
  uint32_t n = appuis;
  interrupts;
  if (n != dernierAffiche) {
    dernierAffiche = n;
    Serial.print("Appuis : ");
    Serial.println(n);
  }
  delay(10);
}

Expected Verification

Each single press prints a line "Presses: N" with a counter that increases by 1; a firm press must never create duplicates. The while (!Serial) statement shows that the MKR GSM 1400 has native USB CDC: the program waits for the serial monitor to open.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials