🛠 Bouton poussoir et console USB
Objectives
Wire a push button to a digital pin on the MKR GSM 1400 (3.3 V logic), read it using an internal pull-up resistor (INPUT_PULLUP), debounce the signal, and display each press on the USB console. This is a fundamental skill for any local interface: pins D0–D7 are digital, including 8 external interrupt-capable inputs.
Hardware
- Arduino MKR GSM 1400 board
- 1 push button
- 1 breadboard and jumper wires
- USB power supply (the radio module remains off, so USB is sufficient for this setup)
Complete Code
// Bouton sur D2 (vers GND) + console USB
const uint8_t BTN = 2;
volatile uint32_t appuis = 0;
uint32_t dernierMs = 0;
void onAppui {
uint32_t maintenant = millis;
if (maintenant - dernierMs > 50) { // anti-rebond 50 ms
appuis++;
dernierMs = maintenant;
}
}
void setup {
pinMode(BTN, INPUT_PULLUP); // logique active a l'etat BAS
attachInterrupt(digitalPinToInterrupt(BTN), onAppui, FALLING);
Serial.begin(9600);
while (!Serial) {} // attend l'ouverture du moniteur USB
Serial.println("MKR GSM 1400 - test bouton D2");
}
void loop {
static uint32_t dernierAffiche = 0xFFFFFFFF;
noInterrupts;
uint32_t n = appuis;
interrupts;
if (n != dernierAffiche) {
dernierAffiche = n;
Serial.print("Appuis : ");
Serial.println(n);
}
delay(10);
}
Expected Verification
Each single press prints a line "Presses: N" with a counter that increases by 1; a firm press must never create duplicates. The while (!Serial) statement shows that the MKR GSM 1400 has native USB CDC: the program waits for the serial monitor to open.
Common Errors
- Button wired to 3.3 V instead of GND while the code uses
INPUT_PULLUP: the counter increments continuously; wire it to ground or switch toINPUTwith an external pull-up resistor to 3.3 V. - Forgetting that the logic level is 3.3 V: never inject 5 V into a GPIO pin on the board.
- Using a pin that does not support interrupts: only some pins accept
attachInterrupt(8 external interrupt pins on the MKR); D2 is one of them. - Not including
while (!Serial): the first lines are lost if the monitor opens after startup. - Bouncing: without the 50 ms window, a single press may count 3 to 5 events.
Other tutorials for this board
Code examples for this board
gsm-connexion.inoArduinoenvoi-sms.inoArduinohttp-get.inoArduinohttp-post-capteur.inoArduinoheure-gsm.inoArduinoappels.inoArduino