Example Code for Raspberry Pi – Digital Steel Ball Inclination Sensor
Last revision 2026/01/20
The article explains how to integrate a digital steel ball inclination sensor with Raspberry Pi, detailing the operating principle, hardware setup, and programming instructions for creating interactive projects.
Introduction
This is a digital module based on a steel ball switch. It utilizes the characteristics of the steel ball to roll it towards the bottom through gravity, thereby closing or opening the switch. So it can also be used as a simple tilt sensor.
This module can also be used in combination with the Raspberry Pi sensor expansion board. In this case, it can be used to make very interesting interactive works, which is safer than using a mercury switch.
Operating Principle

It utilizes the characteristics of the steel ball to roll it towards the bottom through gravity, thereby closing or opening the switch.
Hardware Preparation
- Gravity: 37 Pcs Sensor Set
- Raspberry Pi 4 Model B
- IO Expansion HAT for Raspberry Pi 4B/3B+
- 8GB + SanDisk Class10 SD/MicroSD Memory Card
- 5V@3A USB Power Supply
- 8.9 IPS Touch Display
Wiring Diagram
- Connect the Raspberry Pi correctly to devices such as the screen, power, keyboard and mouse.

- Connect this sensor to pin 8 on the expansion board. For convenience, connect a led switch to pin 12.


Sample Code
- Open Thonny Python IDE to copy the following program into it

import RPi.GPIO as GPIO
import time
LED = 12
dip_key = 8
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED,GPIO.OUT)
GPIO.setup(dip_key,GPIO.IN)
while True:
if GPIO.input(dip_key):
GPIO.output(LED,GPIO.HIGH)
else:
GPIO.output(LED,GPIO.LOW)
time.sleep(0.1)



Was this article helpful?
