🛠 Bouton de contact cousu et pull-up interne

LilyPad Arduino · debutant · Schéma : IMG-lilypad-debutant-2-lilypad-bouton-pullup.svg — Deux boutons-pression reliés par fil conducteur formant un contact entre la pastille 2 et le moins, LED sur la pastille 9.

Objectives

Sew a contact sensor (two snap buttons connected by a conductive thread) that acts as a push button, and turn on an LED when the two snaps are connected. This introduces digital inputs, the internal pull-up resistor (20 to 50 kΩ), and software debouncing adapted to conductive thread.

Materials

Complete Code

// Bouton cousu : contact fermé -> LED allumée (pull-up interne)
const uint8_t BOUTON = 2;  // contact cousu entre cette pastille et « − »
const uint8_t LED    = 9;  // LED LilyPad sur pastille PWM
const unsigned long ANTI_REBOND = 50; // ms : le fil conducteur « crépite »

bool etatStable = HIGH, derniereLecture = HIGH;
unsigned long dernierChangement = 0;

void setup {
  pinMode(BOUTON, INPUT_PULLUP); // pull-up interne : pas de résistance externe
  pinMode(LED, OUTPUT);
}

void loop {
  bool brut = digitalRead(BOUTON);
  unsigned long maintenant = millis;
  if (brut != derniereLecture) {          // tout changement relance le timer
    derniereLecture = brut;
    dernierChangement = maintenant;
  } else if (brut != etatStable && maintenant - dernierChangement >= ANTI_REBOND) {
    etatStable = brut;                    // contact fermé = LOW (relié à la masse)
  }
  digitalWrite(LED, (etatStable == LOW) ? HIGH : LOW);
}

Expected Verification

The LED is off at rest; when the two snap buttons are brought close to each other (closed contact), the LED turns on without any external resistor. When separating the snaps, it turns off cleanly, without flickering thanks to the 50 ms debounce.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials