lispython 2019-01-21
本文实例为大家分享了python树莓派红外反射传感器的程序,供大家参考,具体内容如下
1、工具
rpi3,微雪ARPI600,Infrared Reflective Sensor
2、基本原理
Infrared Reflective Sensor 输出数字和模拟信号
模拟信号通过ARPI600上的AD转换芯片TLC1543进行转换
TLC1543通信使用rpi的GPIO口,模拟SPI,进行模拟信号输入端口选择,和输出读取
程序通过读取DOUT判断与障碍物接近和远离
在接近障碍物时读取AD转换数值
3、连接实物
4、python程序(根据ARPI600提供程序修改)
#!/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()