🛠 Bouton externe et console série sur le Nicla Vision
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
- Arduino Nicla Vision + micro-USB cable
- 1 push button, 1 10 kΩ resistor (the internal pull-up is sufficient in practice)
- Connecting wires; the board exposes 22 digital pins D0–D21
- >
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
- Button wired to 3.3 V without a resistor to ground: short circuit when pressed. Always button → pin → GND with pull-up, or button → 3.3 V → pin → resistor → GND.
- Reserved pin: avoid D6/D7 (internal SS), D8–D10 (external SPI bus), D20–D22 (internal SPI1 bus of the LSM6DSOXTR IMU) and D11/D12 (external I²C SDA/SCL). D0 to D5 are safe for this setup.
- Silent console: the Nicla Vision uses USB CDC; on some IDEs you need to check the port wait option or wait for
while (!Serial);. Also check the cable (data, not charge-only). - Random counting without button: pin not configured as INPUT_PULLUP, it floats.
Other tutorials for this board
Code examples for this board
led_rgb.inoArduinoimu_lsm6dsox.inoArduinotof_distance.inoArduinomicro_pdm.inoArduinocamera_capture.inoArduinowifi_scan.inoArduino