Introduction
The tilt switch used here is a ball one with a metal ball inside. It is used to detect small angle of inclination.
Components
– 1 * SunFounder Uno board
– 1 * USB data cable
– 1 * Tilt switch
– Several jumper wires
Experimental Principle
The principle is very simple. When the switch is tilted in a certain angle, the ball inside rolls down and touches the two contacts connected to the pins outside, thus triggering circuits. Otherwise the ball will stay away from the contacts, thus breaking the circuits.
Experimental Procedures
Step 1: Build the circuit
The schematic diagram
Step 2: Program (Please refer to the example code in LEARN -> Get Tutorial on our website)
Step 3: Compile the code
Step 4: Upload the sketch to the SunFounder Uno board
Now, tilt the switch, and the LED attached to pin 13 on SunFounder Uno board will light up.
Code
/*****************************************/ const int ledPin = 13;//the led attach tovoid setup() { pinMode(ledPin,OUTPUT);//initialize the ledPin as an output pinMode(2,INPUT); digitalWrite(2, HIGH); } /******************************************/ void loop() { int digitalVal = digitalRead(2); if(HIGH == digitalVal) { digitalWrite(ledPin,LOW);//turn the led off } else { digitalWrite(ledPin,HIGH);//turn the led on } } /**********************************************/ |
Video