
Tilt Sensor Interfacing with Raspberry Pi – Regent Electronics
Introduction
Tilt sensors play a crucial role in detecting angular movement and orientation changes. In this article, we will explore the working principles of tilt sensors and their interfacing with a Raspberry Pi. Whether you’re building automation projects or want to understand tilt detection better, this guide will help you get started.
Understanding the Tilt Sensor

A tilt sensor is a simple electronic device that detects inclination or orientation changes. It produces an electrical signal that changes based on angular movement. These sensors contain a rolling ball enclosed in a conductive material, which establishes or breaks an electrical connection depending on the sensor’s tilt angle.
Working Principle of Tilt Sensors

- The tilt sensor consists of a rolling ball inside a cylindrical enclosure.
- When positioned horizontally, the rolling ball completes an electrical circuit.
- When tilted, the rolling ball moves, breaking the electrical circuit.
- This change in electrical state is detected and used for further applications.
By now, you have an understanding of the basic working principles of a tilt sensor. Next, we will explore how to interface it with a Raspberry Pi.
Components Required
To interface the tilt sensor with a Raspberry Pi, you will need the following components:
- Tilt Switch Module x1
- Generic LED x1
- Breadboard x1
- Raspberry Pi x1
- Jumper Wires
Interfacing the Tilt Sensor with Raspberry Pi

Since the tilt sensor functions as a switch, interfacing it with a Raspberry Pi follows a simple circuit connection.
Wiring Instructions
- Connect the VCC pin of the tilt sensor to the 5V pin of the Raspberry Pi.
- Connect the GND pin of the tilt sensor to the GND pin of the Raspberry Pi.
- Connect the Signal pin of the tilt sensor to GPIO 27 on the Raspberry Pi.
- Connect an LED to GPIO 17 for visual indication.
With the wiring complete, let’s move on to coding.
Python Code for Tilt Sensor Interfacing
The following Python script enables the Raspberry Pi to detect angular movement using the tilt sensor and turn on an LED when a tilt is detected.
import RPi.GPIO as GPIO
import time
# Setup GPIO mode
GPIO.setmode(GPIO.BCM)
tilt_pin = 27 # GPIO pin connected to the tilt sensor
led_pin = 17 # GPIO pin connected to the LED
# Setup GPIO pins
GPIO.setup(tilt_pin, GPIO.IN)
GPIO.setup(led_pin, GPIO.OUT)
print(“Tilt Sensor Test – Press Ctrl+C to stop”)
def on_tilt_detected(channel):
print(“Tilt detected!”)
GPIO.output(led_pin, 1)
time.sleep(1)
GPIO.output(led_pin, 0)
# Detect tilt event
GPIO.add_event_detect(tilt_pin, GPIO.FALLING, callback=on_tilt_detected, bouncetime=100)
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
GPIO.cleanup()
Code Explanation
- Importing Libraries: The script imports the RPi.GPIO and time modules.
- Setting GPIO Mode: The GPIO pins are set to BCM mode.
- Pin Initialization: The tilt sensor and LED pins are defined and configured.
- Defining Callback Function: A function on_tilt_detected() is created to execute when a tilt event occurs.
- Event Detection: The GPIO.add_event_detect() method continuously monitors the tilt sensor and triggers the callback function when tilt is detected.
- Main Loop: The script runs in a loop, waiting for an event, and cleans up GPIO settings when stopped.
Applications of Tilt Sensors
Tilt sensors have numerous practical applications, including:
- Security Systems – Detect unauthorized movement.
- Smartphones & Wearables – Screen rotation and gesture recognition.
- Robotics – Balance control in robots.
- Industrial Equipment – Machine orientation monitoring.
- Gaming Controllers – Motion-sensitive gaming controls.
Conclusion
In this guide, we explored the working principle of a tilt sensor, interfaced it with a Raspberry Pi, and implemented a simple Python script for tilt detection. Whether for automation, security, or robotics, tilt sensors offer a wide range of possibilities.
If you have any questions or suggestions, feel free to share them in the comments below. Stay tuned for more tutorials from Regent Electronics!
Happy Learning! 🚀