🛠 Bouton poussoir et console série
Objectives
Read a push button on a digital pin of the MKR WAN 1310, count the presses, and display them on the serial console via the SAMD21's native USB. Reminders: 8 digital pins at 3.3 V, 8 external interrupts (0, 1, 4, 5, 6, 7, 8, 16/A1, 17/A2), 7 mA max per pin.
Hardware
- 1 MKR WAN 1310 board
- 1 push button
- Wires: button between pin 1 and GND (the pull-up resistor is internal)
Complete code
// Bouton sur broche 1 + console série — MKR WAN 1310
const int BUTTON = 1; // broche compatible EINT pour un réveil future
const int LED = 6; // LED_BUILTIN
unsigned long appuis = 0;
unsigned long dernierAppui = 0;
void setup {
pinMode(BUTTON, INPUT_PULLUP);
pinMode(LED, OUTPUT);
Serial.begin(115200);
while (!Serial);
Serial.println("Appuyez sur le bouton (broche 1 -> GND)");
}
void loop {
if (digitalRead(BUTTON) == LOW) { // actif bas : tirage interne
unsigned long maintenant = millis;
if (maintenant - dernierAppui > 50) { // anti-rebond logiciel 50 ms
appuis++;
dernierAppui = maintenant;
Serial.print("Appui n°");
Serial.println(appuis);
digitalWrite(LED, HIGH);
} else {
digitalWrite(LED, LOW);
}
}
}
Expected verification
Each press displays "Press n°X" in the serial monitor (115200 baud) and turns on D6. The counter does not increase by several counts with a single press thanks to the debouncing.
Common errors
- Wiring the button to 3.3 V while the code uses
INPUT_PULLUP(active low): the signal then remains permanently low. - Forgetting the debouncing and seeing the counter jump by 3 to 10 with each press.
- Expecting the ON LED to light up when running on battery: it follows the 5 V USB/VIN, not the Li-Po battery charged by the BQ24195L.
- Not selecting the MKR WAN 1310 board in the IDE: the serial port disappears during upload (1200 baud reset), this is normal.
Other tutorials for this board
Code examples for this board
join-otaa.inoArduinouplink-confirme.inoArduinodownlink.inoArduinoactivation-abp.inoArduinosuivi-adr.inoArduinoclasse-c.inoArduino