Component guide · Beginner · 3 min read
Vibration, Shock and Tilt Sensors (SW-420, SW-520D)
By Geekobot Learning Team · Updated 24 Oct 2026
On this page
Vibration and tilt sensors are simple switches that close or open when shaken or tilted. The SW-420 module uses a spring contact and a comparator with a sensitivity pot. The SW-520D tilt switch has a metal ball that rolls to connect two pins. Piezo discs generate a voltage when flexed or knocked. They are cheap and easy, but for real angles or vibration levels use an accelerometer.
Key specs at a glance
- SW-420 module
- Spring switch, LM393 comparator, digital output, sensitivity pot
- SW-520D
- Ball tilt switch: closed when upright, opens when tilted past roughly 45°
- Piezo modules
- Analog or digital output from a piezo film or disc
- Supply
- 3.3 to 5 V for modules
- Output
- Short pulses, so use interrupts or fast polling
- Better option
- MPU6050 or ADXL345 accelerometer for angle or vibration level
Types and how they work
a spring around a pin touches it briefly when shaken. The module's comparator turns those contacts into a digital signal, and the potentiometer sets how much shaking counts.
a metal ball connects two contacts when upright and rolls away when tilted. It bounces, so debounce it.
a piezo element produces a voltage when it is flexed or knocked. Great for knock detection and drum pads; add a 1 MΩ resistor across it to bleed the charge, and a zener or the module's circuit to limit spikes.
Wiring and code
Connect VCC, GND and DO to a digital pin. Vibration pulses can be only a few milliseconds long, so use an interrupt or poll quickly and count pulses over a window.
const byte PIN = 2; // interrupt-capable pin
volatile unsigned int hits = 0;
void onHit() { hits++; }
void setup() {
Serial.begin(9600);
pinMode(PIN, INPUT);
attachInterrupt(digitalPinToInterrupt(PIN), onHit, CHANGE);
}
void loop() {
delay(1000);
noInterrupts(); unsigned int n = hits; hits = 0; interrupts();
Serial.println(n); // higher = more vibration
if (n > 20) Serial.println("Shaking!");
}When to use an accelerometer instead
Switch sensors answer "did it move?" They cannot tell you the tilt angle, the direction or how strong the vibration is. For a self-balancing robot, a spirit level, machine health monitoring or fall detection, use an MPU6050 or similar accelerometer, which gives real numbers on three axes.
Common mistakes to avoid
- Polling slowly and missing short vibration pulses.
- Not debouncing a ball tilt switch.
- Connecting a bare piezo straight to an input without a bleed resistor or clamp.
- Expecting an angle reading from a tilt switch.
- Setting the SW-420 pot so sensitive it triggers constantly.
Frequently asked questions
How do I adjust the SW-420 sensitivity?
Turn the blue potentiometer until the LED stays off at rest and flickers when tapped.
Is a tilt switch the same as an accelerometer?
No. A tilt switch is on or off; an accelerometer measures angle and acceleration.
Can I detect a knock on a door?
Yes. A piezo sensor taped to the door with an analog reading and a threshold works well.
Why does my vibration sensor trigger randomly?
It is too sensitive or picking up desk vibration. Turn down the pot and mount it firmly.
Do tilt switches contain mercury?
Old ones did. Modern SW-520D types use a metal ball and are mercury-free.
Parts for this guide
Terms in this guide
Keep learning
Sources
- SW-420 and SW-520D switch datasheets
- TI LM393 datasheet
- TE Connectivity piezo film sensor technical manual
Written by the Geekobot team from manufacturer datasheets. Always check the datasheet for the exact part and variant you buy.




