🛠 Bouton externe et console série sur le Nicla Vision

Arduino Nicla Vision · debutant · Schéma : IMG-nicla-vision-debutant-2-niclavision-bouton-console.svg — bouton poussoir avec résistance de pull-up câblé entre la broche D0 et la masse, Nicla Vision alimenté par micro-USB

Objectives

Read an external push button and display presses in a serial console, to master digital I/O and the USB CDC port of the Nicla Vision.

Hardware

Complete code

// Nicla Vision : 22 broches numeriques D0-D21, logique 3.3 V
const uint8_t BUTTON = D0;
uint32_t appuis = 0;
uint32_t dernierMs = 0;

void setup {
  pinMode(BUTTON, INPUT_PULLUP);   // appui = LOW
  Serial.begin(115200);            // USB CDC (VID 0x2341 / PID 0x025F)
}

void loop {
  static bool etatStable = HIGH;
  bool lecture = digitalRead(BUTTON);
  if (lecture != etatStable && millis - dernierMs > 30) {
    etatStable = lecture;
    dernierMs = millis;
    if (etatStable == LOW) {
      appuis++;
      Serial.print(F("appui n"));
      Serial.println(appuis);
    }
  }
}

Expected verification

Open the serial monitor at 115200 bauds: each press prints "press nN". The button is wired between D0 and GND; without an external pull-up, the internal ~10 kΩ resistor keeps D0 high at rest. The 30 ms debounce scrolling prevents double counting.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials