gebos1
Posts: 13
Joined: Wed Feb 20, 2019 9:02 am

Measuring strength of vibration on SW-420 shock sensor using Python/Raspberry Pi.

Fri Mar 29, 2019 5:23 pm

I am trying to measure the strength and show values produced from the SW-420 shock sensor. I have found how to do so using the Arduino code that can be seen in the link below:

http://makersindia.org/t77-2/

Also in that link is a code to detect movement with the shock sensor using python, however it doesn't show values. It's just triggered when the output is high. I want to make it trigger at a certain threshold value but in order to do so I need to figure out how to produce those values in python instead of Arduino. Does anyone know how to do this in Python?

User avatar
B.Goode
Posts: 8252
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Measuring strength of vibration on SW-420 shock sensor using Python/Raspberry Pi.

Fri Mar 29, 2019 6:05 pm

I put the text of your Subject/Topic into an Internet search and it found this previous discussion in these forums:
viewtopic.php?f=37&t=199921


But the description from your linked product information looks as though what you seek is not possible with this packaging of the sensor:
The Vibration module based on the vibration sensor SW-420 and Comparator LM393 to detect if there is any vibration that beyond the threshold. The threshold can be adjusted by the on-board potentiometer. When this no vibration, this module output logic LOW the signal indicate LED light,And vice versa.
It seems you only get a digital report of High or Low, not a readout of the underlying analogue values detected.

gebos1
Posts: 13
Joined: Wed Feb 20, 2019 9:02 am

Re: Measuring strength of vibration on SW-420 shock sensor using Python/Raspberry Pi.

Fri Mar 29, 2019 6:16 pm

Yea I've been searching the web for a little while now and appears that it isn't possible to produce values on the Raspberry Pi like you can for the Arduino but instead just high and low like you said. I was just hoping this wasn't the case and someone knew how to do it. :(

User avatar
B.Goode
Posts: 8252
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Measuring strength of vibration on SW-420 shock sensor using Python/Raspberry Pi.

Fri Mar 29, 2019 6:54 pm

gebos1 wrote:
Fri Mar 29, 2019 6:16 pm
Yea I've been searching the web for a little while now and appears that it isn't possible to produce values on the Raspberry Pi like you can for the Arduino but instead just high and low like you said. I was just hoping this wasn't the case and someone knew how to do it. :(

I don't think the Arduino code displays the analogue values as you appear to think.

See the documentation for the Arduino pulseIn function on which it relies: https://www.arduino.cc/reference/en/lan ... o/pulsein/

I think it is measuring the length of time between two vibrations that trigger the preset threshold, not the frequency of the vibrations themselves.

MarkTF
Posts: 297
Joined: Tue Mar 03, 2015 4:59 pm

Re: Measuring strength of vibration on SW-420 shock sensor using Python/Raspberry Pi.

Fri Mar 29, 2019 8:09 pm

gebos1 wrote:
Fri Mar 29, 2019 6:16 pm
Yea I've been searching the web for a little while now and appears that it isn't possible to produce values on the Raspberry Pi like you can for the Arduino but instead just high and low like you said. I was just hoping this wasn't the case and someone knew how to do it. :(
The second sketch in your Arduino link doesn't measure the amplitude of the vibration event, it measures the duration of a vibration event exceeding the threshold for the sensor to detect vibration at all. The Arduino "pulsein()" function waits for a change in the binary input (Low to High in this instance) and returns the duration that the detected pulse was active (time for it to go back High to Low after going High).

One might be able to do the same thing on RPi with the Python pigpio library by writing a pulsein() function that monitors a pin for those level transition and returns the elapsed time, but it will likely not do it with the resolution of the Arduino which may or may not be a problem in this instance.

Code: Select all

# Untested code adapted from https://tutorials-raspberrypi.com/raspberry-pi-ultrasonic-sensor-hc-sr04/

#Libraries
import RPi.GPIO as GPIO
import time
 
#GPIO Mode (BOARD / BCM)
GPIO.setmode(GPIO.BCM)
 
#set GPIO Pins
GPIO_ECHO = 24
 
#set GPIO direction (IN / OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)

def pulsein():
    # save StartTime
    while GPIO.input(GPIO_ECHO) == 0:
        StartTime = time.time()
 
    # save time of arrival
    while GPIO.input(GPIO_ECHO) == 1:
        StopTime = time.time()
 
    # time difference between start and arrival
    TimeElapsed = StopTime - StartTime
 
    return TimeElapsed

gebos1
Posts: 13
Joined: Wed Feb 20, 2019 9:02 am

Re: Measuring strength of vibration on SW-420 shock sensor using Python/Raspberry Pi.

Sat Mar 30, 2019 7:20 pm

MarkTF wrote:
Fri Mar 29, 2019 8:09 pm
gebos1 wrote:
Fri Mar 29, 2019 6:16 pm
Yea I've been searching the web for a little while now and appears that it isn't possible to produce values on the Raspberry Pi like you can for the Arduino but instead just high and low like you said. I was just hoping this wasn't the case and someone knew how to do it. :(
The second sketch in your Arduino link doesn't measure the amplitude of the vibration event, it measures the duration of a vibration event exceeding the threshold for the sensor to detect vibration at all. The Arduino "pulsein()" function waits for a change in the binary input (Low to High in this instance) and returns the duration that the detected pulse was active (time for it to go back High to Low after going High).

One might be able to do the same thing on RPi with the Python pigpio library by writing a pulsein() function that monitors a pin for those level transition and returns the elapsed time, but it will likely not do it with the resolution of the Arduino which may or may not be a problem in this instance.

Code: Select all

# Untested code adapted from https://tutorials-raspberrypi.com/raspberry-pi-ultrasonic-sensor-hc-sr04/

#Libraries
import RPi.GPIO as GPIO
import time
 
#GPIO Mode (BOARD / BCM)
GPIO.setmode(GPIO.BCM)
 
#set GPIO Pins
GPIO_ECHO = 24
 
#set GPIO direction (IN / OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)

def pulsein():
    # save StartTime
    while GPIO.input(GPIO_ECHO) == 0:
        StartTime = time.time()
 
    # save time of arrival
    while GPIO.input(GPIO_ECHO) == 1:
        StopTime = time.time()
 
    # time difference between start and arrival
    TimeElapsed = StopTime - StartTime
 
    return TimeElapsed
Ok thank you, I'll take a look at the code see if I can alter it and get it to function how I want it to for my shock sensor.

gebos1
Posts: 13
Joined: Wed Feb 20, 2019 9:02 am

Re: Measuring strength of vibration on SW-420 shock sensor using Python/Raspberry Pi.

Sat Mar 30, 2019 7:21 pm

B.Goode wrote:
Fri Mar 29, 2019 6:54 pm
gebos1 wrote:
Fri Mar 29, 2019 6:16 pm
Yea I've been searching the web for a little while now and appears that it isn't possible to produce values on the Raspberry Pi like you can for the Arduino but instead just high and low like you said. I was just hoping this wasn't the case and someone knew how to do it. :(

I don't think the Arduino code displays the analogue values as you appear to think.

See the documentation for the Arduino pulseIn function on which it relies: https://www.arduino.cc/reference/en/lan ... o/pulsein/

I think it is measuring the length of time between two vibrations that trigger the preset threshold, not the frequency of the vibrations themselves.
Thanks, I believe you are correct.

Return to “General discussion”