🛠 Bouton et console série sur la GIGA R1 WiFi
Objectives
Read a numeric input, debounce it properly, and communicate with the native serial port of the GIGA R1 WiFi (available as soon as it is connected to the USB-C USB0 port, without a USB-to-serial converter). You will also learn the golden rule of pins: 3.3 V logic, never a 5 V signal, and INPUT_PULLUP to avoid wiring to VCC.
Hardware
- Arduino GIGA R1 WiFi board + USB-C cable
- 1 push button between pin 6 and GND (no external resistor needed: we use the internal pull-up)
- Optional: 1 LED + 330 Ω on pin 5 to visually confirm the press
Complete code
// GIGA R1 WiFi — bouton + console série avec anti-rebond
const int BTN_PIN = 6; // bouton vers GND, pull-up interne
const int LED_PIN = 5; // LED + 330 ohms vers GND
volatile unsigned long appuis = 0;
int etatPrecedent = HIGH;
unsigned long dernierChangement = 0;
const unsigned long ANTI_REBOND = 30; // ms
void setup {
Serial.begin(115200); // port série natif sur USB-C (USB0)
while (!Serial) {} // attend l'ouverture du moniteur (USB natif)
pinMode(BTN_PIN, INPUT_PULLUP); // 3.3 V logic — jamais de signal 5 V
pinMode(LED_PIN, OUTPUT);
Serial.println("GIGA R1 WiFi prete : appuyez sur le bouton broche 6");
}
void loop {
int etat = digitalRead(BTN_PIN);
unsigned long maintenant = millis;
if (etat != etatPrecedent && maintenant - dernierChangement > ANTI_REBOND) {
dernierChangement = maintenant;
etatPrecedent = etat;
if (etat == LOW) { // bouton actif a l'etat LOW (pull-up)
appuis++;
digitalWrite(LED_PIN, HIGH);
Serial.print("Appui n#"); Serial.print(appuis);
Serial.print(" — uptime "); Serial.print(maintenant / 1000); Serial.println(" s");
} else {
digitalWrite(LED_PIN, LOW);
}
}
}
Expected verification
Open the serial monitor at 115200 baud: the welcome message is displayed. Each press prints a line like “Press n#1 — uptime 12 s” etc., without spurious duplicates (the 30 ms debounce does its job). The indicator LED lights up during the press.
Common errors
- The serial monitor remains empty: with native USB, the
while (!Serial)waits for the monitor to open; if you run the sketch without the monitor, it runs in a waiting state — this is intended. Reconnect or open the monitor. - Button wired to 3.3 V without pull-down: this also works, but you then need
INPUT_PULLDOWNor an external resistor; the pull-up + GND schematic is the simplest. - Presses counted multiple times: debounce missing or too short; increase
ANTI_REBONDto 50 ms. - Inconsistent speed: the native serial port ignores the actual baud rate, but keep 115200 on both sides to avoid surprises with other tools.
Other tutorials for this board
Code examples for this board
clignotante.inoArduinobouton.inoArduinorespiration.inoArduinoanalogique.inoArduinodac-onde.inoArduinooled-i2c.inoArduino