I'm working on a project that uses SW-420 sensors to detect vibrations. I was originally following this tutorial and my code is pasted below. I used a previous post to determine that I should be using the 3.3V pin instead of 5V, so I swapped that. In every tutorial I've seen, both lights on the sensor have worked, but regardless of what GPIO pin I use, the DO-LED doesn't work. (Not sure if that's relevant.)

If I run the code, it will tell me that a vibration has been detected only if I remove and replace the DO cable (using GPIO 27/pin 13) from the pi--but the sensor doesn't register taps/shakes/hard knocks.

Does anybody know what I'm doing wrong? Pic of offending Pi unit


import RPi.GPIO as GPIO
import time

channel = 27

GPIO.setup(channel, GPIO.IN)

def callback(channel):
   if GPIO.input(channel):
      print("Collision Detected!")
      print("Collision Detected!")

GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300)
GPIO.add_event_callback(channel, callback)

while True:

1 Answer 1


I guess the pot needs to be adjusted to set the vibration sensitivity you require.

The pot is the square blue component with the cross head screwdriver slots. Typically they have many complete turns (perhaps 10 or more). Turn the pot until you get the desired output on DO.

  • I have about five sensors, and none of them have more than one complete rotation on the pot. I tried it with two different sensors, and neither of them registered vibrations. Do you think the code is okay?
    – ratassets
    Mar 24, 2019 at 16:56
  • The code as posted won't compile. If that is just a formatting problem the code is probably okay as it does work if I format it correctly. I haven't any of that sensor type so didn't know the range of rotation.
    – joan
    Mar 24, 2019 at 17:40
  • It turns out two of the sensors were knockoffs--the authentic ones work just fine. Thanks for your help!
    – ratassets
    Mar 24, 2019 at 19:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.