I am trying to read serial data from an ultrasonic distance sensor. The only output I get is a white square like this one:
I have a raspberry pi 2 and a ME007-ULS v1 ultrasonic sensor from ebay, I got this from the manual:
When the triggering pin “2.Trigger” is in falling edge and the low level keeps in 0.1 to 10ms, which will trigger the controller to work one time and then the output pin “3.TX/PWM” will output a frame 3.3V TTL level serial data
and the output frame format of the sensor is:
This is the code I've written:
import RPi.GPIO as GPIO
import time
from serial import Serial
#GPIO mode
GPIO.setmode(GPIO.BCM)
#assign GPIO pins
GPIO_TRIGGER = 18
#direction of GPIO-Pins (IN / OUT)
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
def uss_funct():
ser = Serial('/dev/ttyAMA0', baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=3)
# set trigger HIGH, sensor is waiting for falling edge
GPIO.output(GPIO_TRIGGER, True)
# set trigger LOW after 10ms -> Falling Edge
time.sleep(0.01000)
GPIO.output(GPIO_TRIGGER, False)
# set trigger back HIGH after 2ms, as LOW is supposed to be between 0.1-10ms
time.sleep(0.00200)
GPIO.output(GPIO_TRIGGER, True)
#read from rx
test_output = ser.read()
ser.close()
#clean up GPIO pins
GPIO.cleanup()
print (test_output)
if __name__ == '__main__':
uss_funct()
I think I got the wiring right but just in case - this is how I wired the sensor:
The ultrasonic sensor has 5 Pins:
- 3.3-12V input (connected to 3.3v output)
- trigger (connected to GPIO 18)
- TX Output (connected to GPIO 10)
- Digital Output (NOT connected)
- GND (connected to GND)