© Royalty Free/Corbis
Exercise 1
Soft Circuit
Exercise 3— A More Complicated switch
A switch that alternates connections, like this one, diagrammed here

is suprisingly complex and 10 times more expensive than a little tactile button.
To get your button to act this way, use a trick called a
pull-down resistor:
The pull-down resistor here is the 10K resistor.
When the switch is held down, the 100Ω resistor is
connected directly to 5V. When the switch is released,
the 100Ω resistor is connected to the 10K resistor
which pulls it down to ground.
Think of it this way:
When you press the button and connect the 100Ω resistor to 5V,
the button has a very small resistance (less than 1 Ω),
so it provides a strong pull to 5V.
The 10KΩ resistor is also connecting the 100Ω
resistor to ground, but since the 10KΩ resistor has 10000 times
more resistance than the button, its a very weak pull
to ground and can't compete.
The strong 5V connection overpowers the weak ground
connection and the input pin reads HIGH.
However, when the switch is disconnected, there is no longer a strong pull to 5V. In fact, its let go completely. But there is still weak pull to ground. Despite being a weak connection, it's better than nothing and so the resistor pulls the input pin to LOW.
Build this circuit. It should be very reliable now! If its not working, make sure you have the right resistor values and that the parts are connected up properly.

Wiring up a normally closed switch
You can also use the switch to connect the input to ground, and use a resistor as a pull-up resistor.

Note that the strong and weak connections have nothing to do with whether the switch is configured as a pull-up or pull down. The strength of the connection comes from the fact that the button is very low resistance when held down and that the resistor is much much more resistive to current flow than the button.

Tilt Sensors
A tilt sensor is a sensor that can detect if a object is tilting to one
side or another. The cheapest kind of tilt sensor can also be used
as a measurement for movement. The drawback of this kind of tilt
sensor is that they work as a pushbutton so they can't be used to
tell which direction an object is tilting or how much, only that the
object is tilting.
Inside the tilt sensor there is a small metal ball inside a metal casing.
Once the ball touches the sides of the metal casing it will complete
a circuit and you can read it.
Testing your tilt
Put your multimeter into continuity-test mode and touch the probes to the two leads. Then tilt to determine the angle at which the switch opens and closes
When pointing down, the switch is Open Loop (no continuity)
When pointing up, the switch is closed (low resistance / continuous)
Connecting to your sensor
/* Better Debouncer
*
* This debouncing circuit is more rugged, and will work with tilt switches!
*
* http://www.ladyada.net/learn/sensor/tilt.html
*/
int inPin = 2; // the number of the input pin
int outPin = 13; // the number of the output pin
int LEDstate = HIGH; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 50; // the debounce time, increase if the output flickers
void setup()
{
pinMode(inPin, INPUT);
digitalWrite(inPin, HIGH); // turn on the built in pull-up resistor
pinMode(outPin, OUTPUT);
}
void loop()
{
int switchstate;
reading = digitalRead(inPin);
// If the switch changed, due to bounce or pressing...
if (reading != previous) {
// reset the debouncing timer
time = millis();
}
if ((millis() - time) > debounce) {
// whatever the switch is at, its been there for a long time
// so lets settle on it!
switchstate = reading;
// Now invert the output on the pin13 LED
if (switchstate == HIGH)
LEDstate = LOW;
else
LEDstate = HIGH;
}
digitalWrite(outPin, LEDstate);
// Save the last reading so we keep a running tally
previous = reading;
}
Materials
- One 1k Ω resistor
- One tilt sensor
- Non-conductive fabric
- Conductive thread
- Two wires
Start by attaching the tilt sensor and a 1kΩ resistor to a piece of fabric. Sew a few loops over the tilt sensor to keep in place with some
normal thread. Stitch three wires to the fabric using conductive
thread and make the following connections as shown in the below
illustration:
Here is the outline for the code. Complete and upload to your board:
/*declare pin for tilt sensor */
/* declare pin for on board led */
/* declare variable to store the status of the tilt
sensor */
void setup(){
/* declare tilt pin as INPUT and led pin as an OUTPUT */
}
void loop(){
/* read tilt pin and store the value */
/* set the state of the led to the state of the tilt*/
}
Now when you tilt the fabric the LED next to pin 13 on the Arduino board should light up. You can use this type of tilt sensor to
detect movement. It's a matter of making a estimate of how many
hits you get from the tilt sensor in a certain amount of time. Let's
say that the tilt sensor goes from on to off 2 times every second,
then its safe to assume that the garment the tilt sensor is placed on,
is moving.