interrupteur-tilt.ino

LilyPad Arduino · File : interrupteur-tilt.ino · Language : Arduino · Use case : Détecter l'inclinaison avec un switch à bille cousu

The ball switch closes depending on orientation: perfect for a necklace that reacts when flipped, or a vest that detects a fall. The ball bounces — hence the double read and short delay.

Hardware required

A LilyPad Arduino (ATmega168V/328V, 8 MHz, 3.3 V) with its sew tabs, programmed via an FTDI cable (breakout) and powered by 3.7 V (LiPo) or 5 V on V-REG. 1 ball switch (SW-200D) + conductive thread.

Wiring

One leg to pin 3, the other to GND; the pull-up is internal.

How it works

Each stable change toggles the LED and prints it. When sewing, space the tabs to avoid shorts through the thread. In the Arduino IDE, select « LilyPad Arduino » and the FTDI port, then Upload. Conductive thread adds a few ohms of resistance: keep runs short and insulate crossings.

Going further

Count toggles per minute to estimate activity, or combine with an I2C accelerometer.

interrupteur-tilt.ino

// Switch a bille (tilt) sur la broche 3, LED sur 13.
const int TILT = 3;
const int LED = 13;

bool etat = false;
bool precedent = HIGH;

void setup() {
  pinMode(TILT, INPUT_PULLUP);
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  bool lu = digitalRead(TILT);
  if (lu != precedent && digitalRead(TILT) == lu) { // petit anti-rebond
    etat = !etat;
    digitalWrite(LED, etat ? HIGH : LOW);
    Serial.println(etat ? F("Incline") : F("A plat"));
  }
  precedent = lu;
  delay(10);
}

← Back to the board