本文实例为大家分享了python树莓派红外反射传感器的程序,供大家参考,具体内容如下
1、工具
rpi3,微雪arpi600,infrared reflective sensor
2、基本原理
infrared reflective sensor 输出数字和模拟信号
模拟信号通过arpi600上的ad转换芯片tlc1543进行转换
tlc1543通信使用rpi的gpio口,模拟spi,进行模拟信号输入端口选择,和输出读取
程序通过读取dout判断与障碍物接近和远离
在接近障碍物时读取ad转换数值
3、连接实物
4、python程序(根据arpi600提供程序修改)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
#!/usr/bin/python
# -*- coding:utf-8 -*-
import rpi.gpio as gpio
import time
clock = 16
address = 20
dataout = 21
dout = 17
def adc_read(channel):
value = 0 ;
for i in range ( 0 , 4 ):
if ((channel >> ( 3 - i)) & 0x01 ):
gpio.output(address,gpio.high)
else :
gpio.output(address,gpio.low)
gpio.output(clock,gpio.high)
gpio.output(clock,gpio.low)
for i in range ( 0 , 6 ):
gpio.output(clock,gpio.high)
gpio.output(clock,gpio.low)
time.sleep( 0.001 )
for i in range ( 0 , 10 ):
gpio.output(clock,gpio.high)
value << = 1
if (gpio. input (dataout)):
value | = 0x01
gpio.output(clock,gpio.low)
return value
gpio.setmode(gpio.bcm)
gpio.setwarnings(false)
gpio.setup(clock,gpio.out)
gpio.setup(address,gpio.out)
gpio.setup(dataout,gpio. in ,gpio.pud_up)
gpio.setup(dout,gpio. in )
lastdata = 2
try :
while true:
data = gpio. input (dout)
if lastdata and (lastdata = = 1 ):
continue
if data = = 0 :
print 'near the obstacles'
print 'ad: %d ' % adc_read( 6 )
if data = = 1 :
print 'far the obstacles'
lastdata = data
time.sleep( 0.1 )
except :
gpio.cleanup()
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/bspbspace/article/details/51661289