Basic Tilt Sensor Tutorial
by NateCreates01 in Circuits > Arduino
163 Views, 1 Favorites, 0 Comments
Basic Tilt Sensor Tutorial
What is a tilt sensor?
A tilt sensor is a small sensor that detects orientation or inclination. It is a simple sensor that can be used in many different things.
In this short tutorial, you will learn the basic setup for a tilt sensor, alongside a controller board.
Supplies
1x Tilt Sensor
1x Breadboard
3x Jumper Wires
1x Controller Board
1x USB Cable
Setting Up Your Sensor W/ Microcontroller & Breadboard
Now we will setup up our sensor with our microcontroller and breadboard.
You will need all the supplies from the list at the beginning.
Step 1: Connect a jumper wire into the GND port on the microcontroller, and the other end into the 5th column, row 1, on the positive side of the breadboard (See sketch above for reference)
Step 2: Connect a jumper wire into port 2 of your microcontroller, and the other end into column 4, row 2, of the positive side of the breadboard (see sketch above for reference)
Step 3: Connect a jumper wire into the 5v slot of the microcontroller, and the other end into column 4 on the negative side of the breadboard (see sketch above for reference)
Step 4: Place the Tilt sensor into columns 4 and 5, row 1, on the positive side of the breadboard, right above the jumper wires (see sketch above for reference)
Step 5: Connect your USB to the microcontroller and power source (Laptop used as a power source in this example)
Uploading the Code
/* Better Debouncer - This debouncing circuit is more rugged, and will work with tilt switches! */
/* http://www.ladyada.net/learn/sensor/tilt.html */
int inPin = 2; // the number of the input pin
int outPin = 13; // the number of the output pin
int LEDstate = HIGH; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin
// the follow variables are long's because the time, measured in milliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 50; // the debounce time, increase if the output flickers
void setup()
{ pinMode(inPin, INPUT);
digitalWrite(inPin, HIGH); // turn on the built in pull-up resistor
pinMode(outPin, OUTPUT);
}
void loop()
{
int switchstate;
reading = digitalRead(inPin);
// If the switch changed, due to bounce or pressing...
if (reading != previous) {
// reset the debouncing timer
time = millis();
}
if ((millis() - time) > debounce) {
// whatever the switch is at, its been there for a long time
// so lets settle on it!
switchstate = reading;
// Now invert the output on the pin13 LED
if (switchstate == HIGH)
LEDstate = LOW;
else
LEDstate = HIGH;
}
digitalWrite(outPin, LEDstate);
// Save the last reading so we keep a running tally previous = reading;
}
Sensor Is Setup
Once you have done the previous steps, your setup should look something like the photo above.
Next:
You will test the sensor to see if this setup is working properly.
Focus your attention on the built-in LED on the microcontroller, next to the port labeled 13. It should be glowing orange.
As you tilt the sensor, this LED should turn on and off.
Watch the following video to see the sensor being tested with this simple setup.
Short Sensor Function Check Video
If all the steps were followed, the sensor should function as it did in the above video.