Overview
In this lesson, we will learn how to use the tilt switch and change the state of an LED by changing the angle of tilt switch.
Components
- 1 * Arduino UNO
- 1 * USB Cable
- 1 * Tilt Switch
- 1 * LED
- 1 * 220Ω Resistor
- 1 * Breadboard
- Several jumper wires
Principle
The tilt switch is also called the ball switch. When the switch is tilted in the appropriate direction, the contacts will be connected, tilting the switch the opposite direction causes the metallic ball to move away from that set of contacts, thus breaking that circuit.
Procedures
Step 1: Build the circuit
Step 2: Program
/*********************************************************** File name: 24_tiltSwitch.ino Description: Tilt switches to control the LED light on or off Website: www.adeept.com E-mail: support@adeept.com Author: Tom Date: 2015/05/02 ***********************************************************/ int ledpin=11; //definition digital 11 pins as pin to control the LED int tiltSwitchpin=7; //Set the digital 7 to tilt switch interface int val; //Define variable val void setup() { pinMode(ledpin,OUTPUT); //Define small lights interface for the output interface pinMode(tiltSwitchpin,INPUT_PULLUP);//define the tilt switch interface for input interface } void loop() { val=digitalRead(tiltSwitchpin);//Read the number seven level value is assigned to val if(val==LOW) //Detect tilt switch is disconnected, the tilt switch when small lights go out { digitalWrite(ledpin,LOW);} //Output low, LED OFF else //Detection of tilt switch is conduction, tilt the little lights up when the switch conduction { digitalWrite(ledpin,HIGH);} //Output high, LED ON }
Step 3: Compile the program and upload to Arduino UNO board
Now, when you lean the breadboard at a certain angle, you will see the state of LED is changed.
Summary
In this lesson, we have learned the principle and application of the tilt switch. Tilt switch is a very simple electronic component, but simple device can often make something interesting.