bouton-led.ino

GD32F103 (GigaDevice) — chip + Blue-Pill-compatible boards · File : bouton-led.ino · Language : Arduino · Use case : Allumer la LED sur appui bouton avec anti-rebond

The most common button circuit, adapted to the STM32 core's Arduino pins: INPUT_PULLUP works on PA0 and the 30 ms filter cleans the bounce. The LED tracks the press live on the monitor.

Hardware required

A GD32F103C8T6 development board (Cortex-M3 at 108 MHz, Blue Pill pin-to-pin compatible), powered at 3.3 V/5 V, with an ST-Link or serial adapter. 1 push button (PA0 to GND).

Wiring

Button between PA0 and GND; internal pull-up enabled. Every pin is 3.3 V max — never 5 V on PA0.

How it works

Continuous sampling + validation after 30 ms stability is the standard debounce pattern; digitalWrite(LED, stable) exploits active-low without a conditional. With Arduino: install the « STM32F1 » core (Roger Clark, board manager) and pick the « GD32F103C » board; the user LED is PC13 (active LOW). In C: GD32F10x firmware library + ST-Link.

Going further

Press counter with flash backup (EEPROM/flash sketch), or double-click for two actions.

bouton-led.ino

// Bouton sur PA0 (vers GND) + LED PC13, anti-rebond 30 ms.
const int BOUTON = PA0;
const int LED = PC13;
const unsigned long ANTI_REBOND = 30;

bool dernier = HIGH, stable = HIGH;
unsigned long bord = 0;

void setup() {
  pinMode(BOUTON, INPUT_PULLUP);
  pinMode(LED, OUTPUT);
  Serial.begin(115200);
}

void loop() {
  bool lu = digitalRead(BOUTON);
  unsigned long t = millis();
  if (lu != dernier) bord = t;
  if (t - bord >= ANTI_REBOND && lu != stable) {
    stable = lu;
    digitalWrite(LED, stable);        // actif-bas : LOW = allume
    Serial.println(stable ? "relache" : "APPUIE");
  }
  dernier = lu;
}

← Back to the board