python如何获取传感器数据_如何使用python和raspberry pi2从串口读取传感器数据

我正试着从超声波距离传感器读取串行数据。我得到的唯一输出是一个像这样的白色正方形:

bwkF3.jpg

我有一个树莓皮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

传感器输出帧格式为:

SGHKI.png

这是我写的代码: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(接地)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值