🛠 Bouton de contact cousu et pull-up interne
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
- 1 LilyPad SimpleSnap with 3.7 V battery
- 2 snap buttons + conductive thread (the sewn "button")
- 1 LilyPad LED
- FTDI adapter for uploading
- >
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
- Forgetting INPUT_PULLUP: the input floats, and the LED blinks randomly.
- Sewing the contact to "+" instead of "−": the logic is inverted (closed contact = HIGH).
- Missing debounce: the conductive thread frays and crackles, causing the LED to flicker.
- Confusing HIGH/LOW: when the contact is closed, digitalRead returns LOW (ground), not HIGH.
Other tutorials for this board
Code examples for this board
clignotante.inoArduinobouton-led.inoArduinopwm-respiration.inoArduinoluxmetre-couture.inoArduinointerrupteur-tilt.inoArduinoservo-balayage.inoArduino