[Arduino] Tilt Sensor

 Hi,

time ago my brother give to me a set of sunfounder sensors. In this box, among different "i-don't-know-what-they-do" sensors there is a sensor with a small glass in with inside there is a small mercury ball.

Looking on google seems that this sensor is a "Tilt Switch" that should recognize when is upside-down.

Ok, do something with it....

Today we will do a VERY VERY VERY VERY usefull project using this tilt switch sensor that when is upside down it switch on a led.... (mhhh.... very very usefull)


What we need:


The connection is this:

  • Tilt Sensor GND --> Arduino GND
  • Tilt Sensor VCC --> Arduino 5V
  • Tilt Sensor Output PIN --> Arduino Digital Pin 2
  • LED GND --> 220 ohm resistor --> Arduino GND
  • LED Positive PIN --> Arduino Digital Pin 9

And this is the schema using fritzing:




this is the code:


const int tiltPin = 2;
const int LED =  9;      


int tiltState = 0;         
 
void setup() 
{
  pinMode(LED, OUTPUT);
  pinMode(tiltPin, INPUT);
}
 
void loop() 
{
  // read tilt switch value:
  tiltState = digitalRead(tiltPin);
  
  if (tiltState == HIGH) 
  {
    // turn LED on:
    digitalWrite(LED, HIGH);
  } 
  else 
  {
    // turn led off:
    digitalWrite(LED, LOW);
  }
}


and this is what we have done:




   
very very very usefull....

Commenti