0

I bought an SW-420 vibration sensor.

So, I want to make a sensor for touch and vibration detection. This is my first Raspberry Pi project.

I was googling for open source Python code but couldn't find any.

Could you help me if you don't mind?

SW-420 vibration sensor

goobering
  • 10,750
  • 4
  • 40
  • 64
Jin
  • 1
  • 1
  • 1
  • 2

3 Answers3

2

If you don't have much experience with Python, or the Raspberry Pi generally, the easiest code I've seen uses the gpiozero library. There are installation instructions on the linked homepage.

The recipes page there has a simple motion detector example that should work with your vibration sensor. Whatever you do, don't use their wiring diagram as it connects the sensor to 5V. The 5V output from the sensor is likely to damage your Pi. You need to connect your sensor to 3.3V, GND, and GPIO4 to get a 3.3V output:

from gpiozero import MotionSensor, LED 
from signal import pause

pir = MotionSensor(4) led = LED(16)

pir.when_motion = led.on 
pir.when_no_motion = led.off

pause()
goobering
  • 10,750
  • 4
  • 40
  • 64
1

The datasheet of the sensor says ir has a digital output , so i'm assuming you need to detect digital output frecuency , I made a quick class on python that detects more than 1000 interrupts on a second , it turns on an LED (conected to another pin of raspberry pi ) you can modify it to change the frecuency you need

it should be something like this

#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
import math as m 

class Sw40(object):
    """docstring for Senal"""
    def __init__(self, pin , led):
        self.led = led
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(self.led,GPIO.OUT)
        self.pin = pin
        GPIO.setup(self.pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

        GPIO.add_event_detect(self.pin, GPIO.RISING, callback=self.callback, bouncetime=1)
        self.count = 0 

    def callback(self , pin):
        self.count += 1

    def LedOn(self):
        GPIO.output(self.led , 1)

    def LedOff(self):
        GPIO.output(self.led , 0)








def main():
    sensor = Sw40(21,20) 
    try:
        while True:

            time.sleep(1)
            if sensor.count >=1000:
                sensor.LedOn()
            else:
                sensor.LedOff()
            sensor.count = 0        



    except KeyboardInterrupt:
        GPIO.cleanup()



if __name__ == '__main__':
    main()

running this code as copy-paste , the led will turn on if there is more than 1000 interrupts on a second , you just need to instantiate the class with the pins you connect the LED and the sensor to your raspberry , on the example (main function) The signal should be connected to Broadcom pin #21 , and the LED to Broadcom (BCM) pin #20

hope it helps

Luis Borbolla
  • 190
  • 2
  • 11
1

Nothing fancy needed here ( digital out means the output is ether high or low ). You just need the Pi to detect when the sensor's output goes high. This can be done using a Python script. I suggest you go to https://www.raspberrypi.org/ to learn how to do this. It isn't hard even for someone without programing experience.

Garnett Haines
  • 315
  • 1
  • 2
  • 4