Loading... Please wait...

Tilt / Ball Switch

Tilt / Ball Switch

This example shows how to read the state of a tilt switch and turn the LED on pin 13 on or off according to the sate of the switch..

Hardware Required

  • Arduino Board
  • You can connect a separate LED or just use the in-built one on pin 13.
  • 10K resistor

Circuit

Connect the tilt switch as shown to digital pin 2.

tilt-switch-tilt-switch-1.jpg

 

Code


/** * Turn on an LED connected to LEDPIN only when * the value readed on INPIN is HIGH */ #define LEDPIN 13 #define INPIN 2 void setup() { pinMode(LEDPIN, OUTPUT); pinMode(INPIN, INPUT); } void loop() { if(digitalRead(INPIN) == HIGH){ digitalWrite(LEDPIN, HIGH); // turns the LED on } else { digitalWrite(LEDPIN, LOW); // turns the LED off } delay(10); // debounce switch (wait for a stable read) }
 

What's News

newsletter

Copyright 2017 oddWires. All Rights Reserved.
 Sitemap | oddWires