1
votes

I am trying to read serial data from an ultrasonic distance sensor. The only output I get is a white square like this one:

output example

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:

output frame

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:

  1. 3.3-12V input (connected to 3.3v output)
  2. trigger (connected to GPIO 18)
  3. TX Output (connected to GPIO 10)
  4. Digital Output (NOT connected)
  5. GND (connected to GND)
2

2 Answers

0
votes

I was able to solve my problem and get rid of the white square by creating an empty list and then append whatever output ser.read() gives:

data_output = []

def uss_function():

(...)

#read from rx
data_output.append(ser.read(6))
ser.close()

This gives me an output like this: ['\x00\xff\x01V\x00\xcc'] It's still not exactly what I'm supposed to get as a reading but it's getting closer. I assume that it has something to do with erroneous readings from the serial port and I will update this answer once I figured it all out. I assume that's the topic of a new question though.

0
votes

By default Serial.read() only reads one byte. You need to read 6:

# read from rx
test_output = ser.read(size=6)

Then to see what you have try:

print(repr(test_output))