Example Code for Raspberry Pi – Digital Vibration Sensor
Last revision 2026/01/20
The article provides a detailed tutorial on using a digital vibration sensor with Raspberry Pi, including example code and practical applications such as step counting and crash warning systems.
Introduction
What's the simplest way to check vibration with electric devices? Use a vibration switch to turn on and off the circuit through vibration to generate a signal.
Despite a simple structure, you can make full use of this vibration sensor with creative thinking, like step counting, Crash warning light and so on. As long as you have ideas, the usage of simple components will change endlessly. As long as you have ideas, the usage of simple components can be varied an infinite number of times.

This is a very simple pedometer that can count the number of steps you take.
Wiring Diagram
- Connect the digital LED light-emitting module to the pin 12 on extension board, and connect the digital vibration sensor to the pin 8.

Wiring Diagram
- Open Thonny Python IDE to copy the following program into it
import RPi.GPIO as GPIO
import time
import atexit
LED=12
Vibration=8
atexit.register(GPIO.cleanup)
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED,GPIO.OUT)
GPIO.setup(Vibration,GPIO.IN)
while True:
if GPIO.input(Vibration):
GPIO.output(LED,GPIO.HIGH)
else :
GPIO.output(LED,GPIO.LOW)
time.sleep(0.1)

- Click ‘Run’, you can observe that when the sensor detects vibration, the LED will go out.
Was this article helpful?
