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?
Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. It only takes a minute to sign up.Sign up to join this community
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()
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
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.