🛠 Bouton externe et console série interactive

Arduino Nicla Sense ME · debutant · Schéma : IMG-nicla-sense-me-debutant-2-niclasense-bouton-console.svg — Nicla Sense ME avec un bouton poussoir externe câblé entre la broche numérique 4 et la masse, résistance de pull-up interne symbolisée, câble USB vers l'ordinateur affichant la console

Objectives

Learn how to read a digital input and communicate with the serial console on the Nicla Sense ME. Two specific aspects of this board: first, the digital I/Os are "low power" pins (the manual recommends using a MOSFET or a buffer to drive loads, which is not an issue for a simple button); second, the USB serial port is provided by the SAMD11 bridge chip, which explains the while (!Serial); statement that waits for the monitor to open. The external logic level is set by VDDIO_EXT, programmable from 1.8 to 3.3 V (3.3 V by default on external pins).

Hardware

Complete code

const uint8_t BROCHE_BOUTON = 4;   // broche numérique disponible (0-9)

void setup {
  Serial.begin(115200);
  while (!Serial);                 // attend l'ouverture du moniteur série
  pinMode(BROCHE_BOUTON, INPUT_PULLUP);
  Serial.println("Console Nicla Sense ME prete");
}

void loop {
  static bool etatPrecedent = HIGH;
  bool etat = digitalRead(BROCHE_BOUTON);
  if (etat != etatPrecedent) {
    delay(30);                     // anti-rebond grossier
    Serial.println(etat == LOW ? "Bouton appuye" : "Bouton relache");
    etatPrecedent = etat;
  }
  if (Serial.available) {
    char c = Serial.read;
    Serial.print("Recu : ");
    Serial.println(c);
  }
}

Expected verification

When opening the serial monitor (115200 baud), the welcome message is displayed. Each press of the button prints "Button pressed", releasing it prints "Button released"; a letter typed in the console is echoed back. A button wired pin-to-ground gives LOW at rest, the classic inverted logic with INPUT_PULLUP.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials