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/
import RPi.GPIO as GPIO
#GPIO Mode (BOARD / BCM)
#set GPIO Pins
GPIO_ECHO = 24
#set GPIO direction (IN / OUT)
# 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