🛠 Bouton et console USB

Arduino MKR NB 1500 · debutant · Schéma : IMG-mkr-nb-1500-debutant-2-mkrnb-bouton-console.svg — Bouton poussoir branché entre la broche D2 et la masse GND d'une MKR NB 1500, câble USB vers un PC affichant la console série

Objectives

Read a push button and display presses on the USB serial console. Understand that the MKR NB 1500 has native USB (no FTDI chip): Serial is the USB CDC port, and the GPIO logic is at 3.3 V.

Hardware

Complete code

// Bouton sur D2 + console USB — MKR NB 1500
#include <MKRGSM.h>
GSM gsm;                       // sert uniquement a eteindre le modem

const uint8_t BTN = D2;        // D2 accepte aussi l'entree analogique (AIN18)

void setup {
  Serial.begin(9600);
  while (!Serial);             // USB natif : attendre l'ouverture du port CDC
  pinMode(BTN, INPUT_PULLUP);

  // Couper le modem pour eviter qu'il cherche un reseau en pure perte
  gsm.begin;
  gsm.shutdown;
}

void loop {
  static uint8_t last = HIGH;
  uint8_t now = digitalRead(BTN);
  if (now != last) {
    Serial.println(now == LOW ? "Bouton appuye" : "Bouton relache");
    last = now;
  }
  delay(20);                   // petit anti-rebond logiciel
}

Expected verification

Open the serial console (9600 baud) after uploading: "Button pressed" / "Button released" are displayed on each press. No additional components are required thanks to the internal pull-up resistor.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials