This lesson introduces the tilt sensor component. The tilt sensor is a great way to integrate some motion sensing into a project without having to buy a much more expensive component such as an accelerometer.
While an accelerometer has a range of input information to work with, the tilt sensor communicates two states: HIGH and LOW. This makes the tilt sensor similar in function to a pushbutton.
Here’s an image of the tilt sensor from Sparkfun Electronics:
The tilt sensor has a tiny metal ball inside the cylinder, and when the ball falls into position, the legs become connected. This is similar to how the pushbutton legs are connected once the button is pushed.
You can detect changes in orientation by reading the state of the sensor. Let’s work through a sample project!
For this basic project, you’re going to be controlling a series of LEDs by changing the orientation of the breadboard. This project example introduces the functionality of the tilt sensor as well as some new code!
The steps for this project are split between the circuit setup and the sketch itself. Since this sketch introduces new commands, there will also be a section explaining each new command.
Refer to this fritzing diagram for the circuit setup:
Note: The digital input connection for the tilt sensor should be placed on the breadboard in between the 10k resistor and the tilt sensor leg.
Here is a basic sketch for the tilt sensor. This will make the LEDs change state as the tilt sensor changes state. There are some new concepts used in this code, and there will be some ‘going further’ challenges as well.
Parts of this sketch are based on the sketch used in the Adafruit tilt sensor lesson.
int sensorPin = 2; // pin where tilt sensor connects
int ledPinOne = 4; // make an LED connection var for each LED
int ledPinTwo = 5;
int ledPinThree = 6;
int ledPinFour = 7;
int switchState = 0; // current state of the tilt sensor
int LEDstate = HIGH; // variable to store the state of the LEDs
int tiltValue; // variable to store the value read from the pin
int previous = LOW; // variable to store the previous state of the pin
unsigned long time = 0; // tracks how long since the last change of state
unsigned long debounce = 50; // sets an initial value for the debounce variable.
// debounce is used to check twice (in this case within 50 ms) to make sure the sensor has triggered
void setup() {
pinMode(sensorPin, INPUT); // set the sensor for input
digitalWrite(sensorPin, HIGH); // initialize the tilt sensor to be on
pinMode(ledPinOne, OUTPUT); // set the LEDs for output
pinMode(ledPinTwo, OUTPUT);
pinMode(ledPinThree, OUTPUT);
pinMode(ledPinFour, OUTPUT);
}
void loop() {
tiltValue = digitalRead(sensorPin);
if (tiltValue != previous) {
time = millis();
}
if ((millis() - time) > debounce) {
switchState = tiltValue;
if (switchState == HIGH)
LEDstate = LOW;
else
LEDstate = HIGH;
}
digitalWrite(ledPinOne, LEDstate);
delay(100);
digitalWrite(ledPinTwo, LEDstate);
delay(100);
digitalWrite(ledPinTwo, LEDstate);
delay(100);
digitalWrite(ledPinThree, LEDstate);
delay(100);
digitalWrite(ledPinFour, LEDstate);
delay(100);
previous = tiltValue;
}
Code language: JavaScript (javascript)
Much of the code is familiar, but let’s break down the overall gist of the sketch:
There are several global variables that are being set. Most of these are familiar. One that is new is the LEDstate
variable, which is either going to be HIGH
or LOW
. This is used for toggling the state of the LEDs later in the sketch.
The previous
variable is used to track the last state of the tilt sensor.
The code inside the void setup()
is pretty familiar – you’re just assigning the pins their associated modes.
The core of the sketch is inside the void loop()
. The first step is to read the value of the tilt sensor.
Once a change is detected, the value of millis()
is saved inside a variable called time
. There is then a check to see if the tilt sensor has been triggering for longer than the debounce
value and if it has, then the state changes.
Next, there is some conditional logic to determine the state of the LEDs based on the state of the tilt sensor. This is similar to the logic used in the pushbutton project.
The LEDs are then set to the appropriate state by using the LEDstate
variable.
At the end of the loop, the current state is stored in the previous
variable.
unsigned long
is a type of variable that is meant to store long numbers. Since the time and millis()
values can become quite long, this type is used.millis()
is a command that returns the number of seconds that the current Arduino program has been running.debounce
is the variable name, and debouncing is a conceptAfter you understand the concepts used in this project, you should experiment with changing the values of the LEDs.
Remember, the tilt sensor is similar in functionality to a pushbutton. Any of the challenges you did with buttons can apply to this as well!
Try to come up with some inventive ways to incorporate a tilt sensor into your experiments!