How to Use Tilt Switch Sensor with Arduino

Tilt sensors are simple yet powerful components used in a variety of projects for detecting orientation, vibration, or sudden movement. In this blog, we’ll explore how to use a tilt switch sensor with Arduino, from wiring and coding to real-life applications like fall detection, theft alarms, and orientation-based controls.

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.

How to Use Tilt Switch Sensor with Arduino

What is a Tilt Switch Sensor?

A tilt switch sensor is a type of orientation sensor that changes its output based on its angle of inclination. When the sensor is tilted beyond a certain threshold, it completes or breaks a circuit, triggering an electrical response.

The most common tilt sensors include a metal ball or mercury droplet inside a cylinder. These sensors act like switches: they are either ON or OFF depending on their position.

How Tilt Switch Sensors Works?

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.

Key Features of Tilt Switch Sensor

  • Simple ON/OFF output
  • Easy to interface with Arduino or any microcontroller
  • Small, inexpensive, and reliable
  • Useful for detecting changes in position or vibration

Components Required for using Tilt Switch Sensor with Arduino:

ComponentQuantity
Arduino Uno (or Nano)1
Tilt Switch Sensor1
10K Resistor (optional)1
LED (optional)1
Jumper WiresAs needed
Breadboard1

Tilt Switch Sensor Pinout (KY-017)

If you’re using a KY-017 tilt switch module, it usually has 3 pins:

Pin NameDescription
VCC5V Power Supply
GNDGround
D0Digital Output Pin

If you’re using a bare tilt switch (no module), connect it between GND and a digital pin, using a pull-up resistor.

Circuit Diagram of Tilt Sensor with 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.

Circuit Diagram of Tilt Sensor with Arduino

Arduino Code for Tilt Switch 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;
}

Troubleshooting of Tilt Switch Sensor:

  • If no change is detected, rotate the sensor slowly to find the tilt threshold.
  • Avoid using in high-vibration environments if false triggers are an issue.
  • If you need more precise angular data, consider gyroscope/accelerometer modules (like MPU6050) instead.

Conclusion

Using a tilt switch sensor with Arduino is a quick and efficient way to detect orientation changes or sudden movement. Whether you’re building a basic clap-triggered toy or a fall alert device, this sensor is the perfect starting point for motion-detection projects.

Need Help in Setup of Tilt Switch Sensor with Arduino?

If you need any Help or Assistance for Setup of Tilt Switch Sensor with Arduino, with Modifications or Customization then you can contact us through WhatsApp. We can deliver you this Project in the Following Ways.

Leave a Reply

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

Facebook
YouTube