Learn to code

ARDUINO & CODES

Previous Post
Homepage
Next Post
Driving a brushed DC Motor with Arduino

Arduino Sensor: Detecting movement with a tilt sensor

in Arduino 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 tilt the ball rolls away and the contact is broken. When the bos 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 infomation to the Arduino, either a HIGH or LOW signal.

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

To determine if something is shaken, you wil need to check how its  been since the state of the tilt senor 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 an HIGH or a LOW signal.

Parts you will need

Arduino Uno
BreadBoard
Jumper Wires
2 x 220-ohm resistor
LED
Tilt Sensor
LED

BreadBoard Layout

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

The Code

/*
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

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
*/
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 ()

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.

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

 

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
 }
 else{ //if it isn't
 digitalWrite(firstLEDPin, LOW); //do the opposite
 digitalWrite(secondLEDPin, HIGH);
 }

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 determine the condition of the shaketime. We set the shake 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 seeting the right pinmodes we need to know wheter 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 occuring.

Secondly, we determnine 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);
 }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu