我正试着从超声波距离传感器读取串行数据。我得到的唯一输出是一个像这样的白色正方形:
我有一个树莓皮2和一个ME007-ULS v1超声波传感器从易趣,我得到的手册: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
传感器输出帧格式为:
这是我写的代码: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()
我想我的接线是正确的,但以防万一-我就是这样连接传感器的:
超声波传感器有5个针脚:3.3-12V输入(接3.3v输出)
触发器(连接到GPIO 18)
TX输出(连接到GPIO 10)
数字输出(未连接)
GND(接地)