gpio_button_led_control.ino

PaperDIY Assembly Kit · File : gpio_button_led_control.ino · Language : C++ (Arduino) · Use case : Contrôler une LED via un bouton poussoir en utilisant les GPIOs libres de la carte.

Simple digital I/O management example with software debouncing.

Use available GPIOs on the kit connectors (e.g., GPIO4, GPIO5). Connect a button between the selected GPIO and GND, and an LED with resistor to GND. Flash via USB-C. Open Serial Monitor at 115200 bauds to see states.

gpio_button_led_control.ino

#include <Arduino.h>

// Définition des broches GPIO pour l'Assembly Kit
// Note : Vérifiez le schéma spécifique de votre version d'assemblage
const int BUTTON_PIN = 4;
const int LED_PIN = 5;

bool buttonState = false;
bool lastButtonState = false;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;

void setup() {
  Serial.begin(115200);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);
  Serial.println("PaperDIY GPIO Test Started");
}

void loop() {
  bool reading = digitalRead(BUTTON_PIN);

  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {
      buttonState = reading;
      if (buttonState == LOW) { // Bouton pressé (Pull-up)
        digitalWrite(LED_PIN, HIGH);
        Serial.println("Button Pressed - LED ON");
      } else {
        digitalWrite(LED_PIN, LOW);
        Serial.println("Button Released - LED OFF");
      }
    }
  }
  lastButtonState = reading;
}

← Back to the board