4-Directional Tilt Sensor
Last updated: 8 October 2012
Prepared by Marco De Mutiis [marcodemutiis@gmail.com]
URL: http://www.parallax.com/Store/Sensors/AccelerationTilt/tabid/172/CategoryID/47/List/0/SortField/0/Level/a/ProductID/722/Default.aspx
Cost per unit: $9.99USD (approx. 80 HKD)


General Information

About Tilt Sensors

A tilt sensor can measure the tilting in often two axes of a reference plane in two axes. In contrast, a full motion would use at least three axes and often additional sensors.
(excerpted from a wiki article on tilt sensors, accessed 8 October 2012).

The 4-Directional Tilt Sensor is a perfect accelerometer replacement in those applications which may not need precise angular feedback.
This type of sensor is commonly used in many smart phones and digital cameras as a cheaper means to adjust the screen display when the device is rotated.
(excerpted from parallax product description, accessed 8 October 2012).

Theory of Operation

Internally, the 4-Directional Tilt Sensor includes one IR LED, two phototransistors, and a small ball that moves when the sensor is rotated. Depending on the orientation of the sensor, the two phototransistors will output high or low signals depending whether light is detected or not. New locations are triggered when the sensor is rotated roughly 30° toward the new position.
The chart below depicts the cylinder location when rotating the sensor, and the output states of each phototransistor during revolution.

Items of Note:

- When the sensor is lying flat and only acted on by g-forces, the cylinder will be in Location C.
- Upon startup and when lying flat, the sensor remains in the last location before shutdown. Once movement is detected, the sensor will respond normally.

Features & Specifications:

  • § Small 6-pin DIP packaging with breadboard-friendly 0.1" pin spacing
  • § Easy communication with any microcontroller
  • § Positional feedback in four directions

Getting it to work!

PIN1 of the Tilt Sensor connected to Any digital input pin (example uses 7)
PIN6 of the Tilt Sensor connected to Any digital input pin (example uses 6)
PIN2 OR PIN5 of the Tilt Sensor connected to 3.3 or 5 V
PIN3 OR PIN4 of the Tilt Sensor connected to GROUND



Example Code

The following example sends data coming through the 2 AnalogIn inputs, packed in an integer array, to the Serial Port.
In Processing the position of the breadboard on which the Tilt Sensor sits on is visualized. Everytime we change direction the processing sketch will generate a sine wave of a different frequency.

Arduino Code
the Tilt Sensor is connected to analog pin 6 and 7
const int TiltX = 6;
const int TiltY = 7;

variable to store the values read from the two sensor pins
int sensorReadingX;
int sensorReadingY;

packing the data in an integer array that can give me the 4 different positions
int pos[][2] = '1', '2'}, {'3', '4';

void setup() {
use the serial port

void loop() {
read the sensor data and store it in the variable sensorReadingX and Y
sensorReadingX = digitalRead(TiltX);
sensorReadingY = digitalRead(TiltY);

write the position to the serial port
Serial.write(pos[sensorReadingX] [sensorReadingY]);
delay to avoid overloading the serial port buffer

Processing Code
import ddf.minim.*;
import ddf.minim.signals.*;
import processing.serial.*;

Serial myPort; Create object from Serial class
Minim minim;
AudioOutput out;
SineWave sine1;

int val = 0; Data received from the serial port
float amp1 = 0.8;
float sr = 44100;

void setup()
size(512, 512, P2D);
I know that the first port in the serial list on my mac
is always my FTDI adaptor, so I open Serial.list()[0].
On Windows machines, this generally opens COM1.
Open whatever port is the one you're using.
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
minim library settings to create a line out and a sine wave
minim = new Minim(this);
out = minim.getLineOut(Minim.STEREO, 2048);
sine1 = new SineWave(val, amp1, sr);

void draw()
val = ((direction-47)*130);

if ( myPort.available() > 0) { // If data is available,
direction = myPort.read();
if (direction == 50) {
rect(50, 50, 400, 200);

if (direction == 49) {
rect(250, 50, 200, 400);

if (direction == 51) {
rect(50, 250, 400, 200);

if (direction == 52) {
rect(50, 50, 200, 400);