Tilt Switch and Vibration Sensor for Arduino

  

A tilt switch acts just like a simple push button. The picture above shows an example connected to digital pins 6 and 7. The brass tube contains a small ball that moves as the sensor moves. Like a switch or push button this ball completes a circuit. Move the sensor and the ball moves breaking the circuit.

This particular sensor is ‘off’ when pointing up and ‘on’ when horizontal or pointing down. This sensor detects vibration so the even when pointing up any vibration will turn the sensor ‘on’.

As it’s just a switch any simple sketch that uses a push button can be used. The sensor has two  leads, connect one to earth and the other to a digital pin. This digital pin needs to be kept HIGH, using a resistor to +5 volts. As the sensor is activated it takes the digital pin LOW.

The code below uses two simple tricks to keep the connections to a minimum. The input is Digital pin 7, so Digital pin 6 is made an OUTPUT and this pin is then set to LOW to provide 0v or ground. So how do we get rid of the need to use a resistor to keep inn 7 HIGH? The Arduino has pullup resisistors that can be switched on, so when defining pin 7 this resistor is switched on using pinMode(inPin, INPUT_PULLUP).

This simple circuit switches the onboard LED off and on. You can also see its operation as a vibration sensor

// Tilt sensor and Vibration a Detector

int ledPin = 13;
int inPin = 7;
int earthPin = 6;
int valueTilt = 0;

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(inPin, INPUT_PULLUP);
pinMode(earthPin, OUTPUT);
digitalWrite(earthPin, LOW);

}

void loop() {
valueTilt = digitalRead(inPin); // read the state of the tilt switch
digitalWrite(ledPin, valueTilt);
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s