🛠 Bouton externe et console série interactive
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
- Arduino Nicla Sense ME board (ABX00050) and micro-USB cable
- Momentary push button (normally open contact)
- Jumper wires; the pull-up resistor is internal, so no external resistor is needed
- >
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
- Button wired pin-to-3V3 without a resistor: floating input at rest and spurious readings; prefer pin-to-ground with internal pull-up.
- Forgetting
while (!Serial);: the first lines of the program are sent before the monitor opens and seem "lost". - Voltage confusion: external pins are translated to 3.3 V by default via VDDIO_EXT; do not inject more than 3.3 V into a pin.
- To drive a real load (power LED, buzzer), direct connection to the pin is discouraged by the manual: use a MOSFET or a buffer.
Other tutorials for this board
Code examples for this board
led_rgb.inoArduinoimu_bhi260.inoArduinorotation_fusion.inoArduinopression_bmp390.inoArduinonose_electronique.inoArduinoboussole_bmm150.inoArduino