Interfacing Ball Tilt Switch Sensor with Arduino

If you want to detect any type of movement without using accelerometer then ball tilt switch might be another option for you.
These are the kind of switches that works on the movements like inclination shaking etc. These movements break or make the electrical connection. These are very popular and used in toys, robots and other projects. These switches are very small, cheap and easy to use.
The specification of these switches are as follows:

Dimensions5.5mm (0.22″) diameter & 13.5mm (0.53″) long
Maximum operating voltage (VCC)Up to 20V
Maximum operating current (Imax)30mA
Sensitivity rangeMovements of around 5 to 10 degrees
Lifetime50,000+ cycles (switches)

How Do Ball Tilt Sensors Work?

Ball tilt sensor or switch is made up of metal capsule or tube with a small ball that rolls in it. A small amount of movement or inclination allows ball to move from one end to other. There are two poles on both sides, when ball moves from one end to another it will break or make electrical connection.

The left side switch show the electrical connection. On the other hand the right side switch show the broken electrical connection.

Testing a Ball Tilt Sensor

For testing the switch put your meter in continuity test mode and connect one of the wire on one side of switch and other wire on the other side. Then look at you multimeter

This shows the switch is closed.

This shows the switch is open.

Wiring up a Ball Tilt Sensor to an Arduino

The wiring of ball tilt sensor is very easy. You have to connect one of the wire to any Arduino digital pin and other to GND. Here the point to be noted is that when you connect like this method you have to must have to use Arduino pull up function on the pin you are using.

Arduino Code – Reading a Ball Tilt Sensor

const int tiltPin = 2;		// tilt sensor pin is connected to pin 2
const int ledPin = 13;		// built-in LED is connected to pin 13

void setup() {
	pinMode(tiltPin, INPUT);		// set sensor pin as an INPUT pin
	digitalWrite(tiltPin, PULL_UP);	// turn on the built in pull-up resistor
	pinMode(ledPin, OUTPUT);		// set LED pin as an OUTPUT pin
}

void loop() {
	if (digitalRead(tiltPin)) { // check if the pin is high
		digitalWrite(ledPin, HIGH);	// turn on the LED
	}
	else { // if it isn't
		digitalWrite(ledPin, LOW);	// do the opposite
	}
}

This code is self-explanatory and has comments to understand.

What is switch bounce and How to Deal with It?

As we know that there is a ball in the sensor. It makes or breaks contact instantly. But in real world, when we re position after tilting the sensor the ball get few bounces as we throw a ball from hand to floor it gets few bounces. Because of these bounces it will make and breaks the contact which is not detectable by us but that time is notable by Arduino as it can execute many instruction in that time. Switch bounce is approximately 5o milliseconds. It can be deal programmatically. This process is called de bouncing.

const int tiltPin = 2;		// tilt sensor pin is connected to pin 2
const int ledPin = 13;		// built-in LED is connected to pin 13

int ledState = HIGH;		// the current state of the LED
int tiltState;				// the current reading from the sensor
int lastTiltState = LOW;	// the previous reading from the sensor

unsigned long time = 0;		// the last time the output pin was toggled
unsigned long debounceDelay = 50;	// the debounce time, increase if the output flickers

void setup() {
	pinMode(tiltPin, INPUT);		// Set sensor pin as an INPUT pin
	digitalWrite(tiltPin, HIGH);	// turn on the built in pull-up resistor
	pinMode(ledPin, OUTPUT);		// Set LED pin as an OUTPUT pin
}

void loop() {
	// read the state of the tilt sensor
	tiltState = digitalRead(tiltPin);

	// If the sensor really tilted?
	if (tiltState != lastTiltState) {
		// reset the debouncing timer
		time = millis();
	}

	if ((millis() - time) > debounceDelay) {
		// whatever the switch is at, its been there for a long time
		// so lets settle on it!
		ledState = tiltState;
	}
	digitalWrite(ledPin, ledState);

	// Save the last tiltState so we keep a running tally
	lastTiltState = tiltState;
}

Leave a Reply

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

Facebook
YouTube
× Contact us