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

closed as unclear what you're asking by Jacobm001, Steve Robillard Apr 25 '16 at 23:48

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    Welcome to Raspberry Pi! Please take the tour and visit the helpcenter to see how things work here. Pay attention to this and please give your question a clear title. – Ghanima Apr 24 '16 at 16:36
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()
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

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.

  • 1
    The raspberrypi.org site is kind of unintuitive these days. It's quite difficult to find specific pieces of technical information without encountering burping jelly babies and flappy astronauts. Can you point to a specific tutorial that might be helpful? – goobering Apr 24 '16 at 23:34

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