Summer Sale: 5% OFF Over $49, Code: Elephant5; 10% OFF Over $99, Code:Elephant10



Arduino UNO R3/MEGA 2560 R3 Project 13: Tilt Switch

Posted by Fiona Su on

Introduction:

Tilt switch controlling the LED ON and OFF.

Hardware Required:

  • Ball switch*1
  • Control board *1
  • Led *1
  • 220Ω resistor*1
  • 10KΩ resistor*1
  • USB cable *1
  • Breadboard jumper wire *5

Video Tutorial

Connection

Connection for REV4:
Connect the ball tilt switch, LED and resistors to control board. Connect the LED to digital pin 8, ball switch to analog pin 5.

thumb

 Connection for 2560 R3:

thumb

Experiment Principle:
When one end of the switch is below horizontal position, the switch is on. The voltage of the analog port is about 5V (1023 in binary). The LED will be on. When the other end of the switch is below horizontal position, the switch is off. The voltage of the analog port is about 0V (0 in binary). The LED will be off.
In the program, we determine whether the switch is on or off according to the voltage value of the analog port, whether it's above 2.5V (512 in binary) or not.

Sample Code:

void setup() 
{ 
  pinMode(8,OUTPUT);// set digital pin 8 as “output” 
} 
void loop() 
{ 
int i;// define variable i 
while(1) 
{ 
  i=analogRead(5);// read the voltage value of analog pin 5 
  if(i>512)// if larger that 512(2.5V) 
  { 
    digitalWrite(8,LOW);// turn on LED 
  } 
  else// otherwise 
  { 
    digitalWrite(8,HIGH);// turn off LED 
  } 
 } 
}

Result

Hold the breadboard with your hand. Tilt it to a certain extent, the LED will be on. If there is no tilt, the LED will be off.
The principle of this experiment can be applied to relay control.
Experiment is completed.Thank you!

thumb 

All Tutorial

Buy 

0 comments

Leave a comment

Please note, comments must be approved before they are published