I've managed to connect 2 SW420 vibration sensors to my rpi3 and can identify a vibration event on callback.

However, I'd like to get the measured value (voltage change?) from the sensors, to see which sensor had MORE vibration.

Is this possible? I've been browsing documentation, but haven't seen anything yet that shows me how to get the raw measured value.


import RPi.GPIO as GPIO
import time

def monitor_callback(channel):
    measurement = GPIO.input(channel)
    if measurement:
        print('{} {}: {}'.format(channel, measurement, time.perf_counter()))

class VibrationMonitor:

    def __init__(self, *channels):
        self.channels = channels

        # setup GPIO
        for channel in self.channels:
            GPIO.setup(channel, GPIO.IN)
            GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300)
            GPIO.add_event_callback(channel, monitor_callback)

    def monitor(self):
        while True:

if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('-c', '--channels',
                        help='Raspberry Pi GPIO BCM channel numbers assigned')
    args = parser.parse_args()

    monitor = VibrationMonitor(*args.channels)
  • 1
    In the future, please consider adding a link/URL for hardware devices that aren't "native" RPi hardware. Not everyone will know what an SW420 is, or how to get details.
    – Seamus
    Jul 28 '18 at 16:59

There are two things that prevent this from working.

  1. The SW420 modules I have seen all include a comparator. This converts a voltage signal to a binary (high/low) signal. Without access to the raw signal, you can't get anything more than two values.
  2. The Raspberry Pi doesn't have any analog input pins.

To get around #1, you need to find a different vibration sensor that To be able read an analog signal requires an A-to-D converter chip. There are many available to the pi. The ADS1015 or ADS1115 seem to be popular chips.

To get around #2, you need to get a different vibration sensor such as the LM393.

  • @NomadMaker: Your last sentence implies the LM393 is a vibration sensor, but it's not - it's a dual comparator. In fact, it's the comparator used on the SW420.
    – Seamus
    Jul 28 '18 at 17:05
  • You are absolutely correct. They came up when I was Googling "vibration sensor" and I didn't look closely enough. Doh!
    – NomadMaker
    Jul 28 '18 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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