Public post

8.4 Detecting movement with a tilt sensor

· 8. Sensors

In this tutorial, you will learn how to measure if something is tilted or shaken. Tilt sensors are designed for this.

The most common tilt sensor is a ball bearing in a box with contacts at one end. When the box is tilted the ball rolls away and the contact is broken. When the box tilted to roll the other way the contact is restored since the ball touches the contacts. In this way, the tilt sensor uses the ball as a switch that is turned on or off depending on its inclination. The tilt sensor will give digital information to the Arduino, either a HIGH or LOW signal.

Tilt sensors are sensitive sensors and detect movement of around 5 to 10 degrees.

To determine if something is shaken, you will need to check how its been since the state of the tilt sensor is changed. If it has not changed for a time you consider significant, the object of the tilt sensor is not shaking.

This way, the tilt sensor acts like a switch that is turned on or off depending on its inclination. So, it will give digital information to the Arduino, either a HIGH or a LOW signal.

Materials you will need

Component Number
Arduino Uno Rev3 1x
Jumper Wires 8x
Breadboard 400 points 1x
220Ω resistor 2x
LEDS 2x
Tilt Sensor 1x

Breadboard Layout

Circuit Diagram – Rember to attach the LEDs correctly with their anode and cathode side.

Code tilting

//tilt sketch
//a tilt sensor attached to pin 2,
//lights one of the LEDs connected to pins 11 and 12
//depending on which way the sensor is tilted

const int tiltSensorPin = 2; //pin the tilt sensor is connected to
const int firstLEDPin = 11; //pin for one LED
const int secondLEDPin = 12; //pin for the other
void setup()
{
 pinMode (tiltSensorPin, INPUT); //the code will read this pin
 digitalWrite (tiltSensorPin, HIGH); // and use a pull-up resistor
 pinMode (firstLEDPin, OUTPUT); //the code will control this pin
 pinMode (secondLEDPin, OUTPUT); //and this one
}
void loop()
{
 if (digitalRead(tiltSensorPin)){ //check if the pin is high
 digitalWrite(firstLEDPin, HIGH); //if it is high turn on firstLED
 digitalWrite(secondLEDPin, LOW); //and turn off secondLED
 }
 else{ //if it isn't
 digitalWrite(firstLEDPin, LOW); //do the opposite
 digitalWrite(secondLEDPin, HIGH);
 }
}

Code Explanation tilting

Defining the variables

Since we want to control 2 LEDs and detect information from the tilt sensor we will need three global variables.

//tilt sketch a tilt sensor attached to pin 2, 
//lights one of the LEDs connected to pins 11 and 12 
//depending on which way the sensor is tilted

//pin the tilt sensor is connected to const int
const int tiltSensorPin = 2; 
//pin for one LED
firstLEDPin = 11; 
//pin for the other
const int secondLEDPin = 12; 

Void Setup ()

You will need to set the pins of the LEDS to an output mode and set the pin of the Tilt Sensor to an input pin. Furthermore, you will use the internal pull-up resistors of the Board.

//the code will read this pin
pinMode (tiltSensorPin, INPUT); 
// and use a pull-up resistor  
digitalWrite (tiltSensorPin, HIGH);
//the code will control this pin
pinMode (firstLEDPin, OUTPUT);
//and this one
pinMode (secondLEDPin, OUTPUT); 
Turning the LEDs on or off

The beginning of the sketch is quite easy just like the void loop() part of this sketch. The loop part turns an LED on when the tilt sensor is turned one way while the other LED is turned off. If you tilt the sensor the other way the opposite will LED will turn on.

if (digitalRead(tiltSensorPin)){ //check if the pin is high
 digitalWrite(firstLEDPin, HIGH); //if it is high turn on firstLED
 digitalWrite(secondLEDPin, LOW); //and turn off secondLED
 }

Code Shaking

// shaken sketch
// tilt sensor connected to pin 2
// led connected to pin 13
 
const int tiltSensorPin = 2;
const int ledPin = 13;
int tiltSensorPreviousValue = 0;
int tiltSensorCurrentValue = 0;
long lastTimeMoved = 0;
int shakeTime=50;
void setup()
{
 pinMode (tiltSensorPin, INPUT);
 digitalWrite (tiltSensorPin, HIGH);
 pinMode (ledPin, OUTPUT);
}
void loop()
{
 tiltSensorCurrentValue=digitalRead(tiltSensorPin);
 if (tiltSensorPreviousValue != tiltSensorCurrentValue){
 lastTimeMoved = millis();
 tiltSensorPreviousValue = tiltSensorCurrentValue;
 }
 if (millis() - lastTimeMoved < shakeTime){
 digitalWrite(ledPin, HIGH);
 }
 else{
 digitalWrite(ledPin, LOW);
 }
}

Code Explanation Shaking

Defining the variables

In the shaking sketch, we will use one LED that turns on when the object/tilt sensor is shaken.  For the LED and the Tilt sensor, a variable is being made. Furthermore, we create variables to hold time to determine the condition of the shaketime. We set the shaking time at 50 milliseconds.

const int tiltSensorPin = 2;
const int ledPin = 13;
int tiltSensorPreviousValue = 0;
int tiltSensorCurrentValue = 0;
long lastTimeMoved = 0;
int shakeTime=50;
Determine if the sensor is shaking.

The real magic happens in the void loop() part of this sketch. After defining variables and setting the right pinmodes we need to know whether the sensor is shaking or not?

Firstly, we read the sensorvalue by reading the til sensor pin. Next, we determine if the current sensorvalue is different then the previous sensorvalue. If that is true, the first condition is met that determines if there is shaking occurring.

Secondly, we determine if the time between the HIGH and LOW state of the sensor is low enough to be called shaking. If that condition is met the LED will be turned on and if not the LED will be receiving a LOW signal.

void loop()
{
 tiltSensorCurrentValue=digitalRead(tiltSensorPin);
 if (tiltSensorPreviousValue != tiltSensorCurrentValue){
 lastTimeMoved = millis();
 tiltSensorPreviousValue = tiltSensorCurrentValue;
 }
 if (millis() - lastTimeMoved < shakeTime){
 digitalWrite(ledPin, HIGH);
 }
 else{
 digitalWrite(ledPin, LOW);
 }
}

Become a member

You just read a free post but there are 2 member-only posts that you don't currently have access to.

Subscribe for $14.99 monthly or $99.99 yearly.

Become a member

Comments

Comments are for members only.
Please become a member or sign in to join the conversation!

You've successfully subscribed to ArduinoPlatform
Welcome! You are now a ArduinoPlatform subscriber.
Welcome back! You've successfully signed in.
Success! You are now a paying member and have access to all content.
Success! Your billing info is updated.
Billing info update failed.