一、介绍
??使用红外跟踪传感器TCRT5000循迹模块。TCRT5000是蓝色的LED传感器的黑色部分用于接收,内部电阻器的电阻随红外光而变化。
二、组件
★Raspberry Pi 3主板*1
★树莓派电源*1
★40P软排线*1
★跟踪传感器模块*1
★面包板*1
★跳线若干
三、实验原理
??跟踪的检测原理是红外发射管向路面发射光,红外线遇到白线等浅色地面反射,接收管接收反射光,施密触发器整形后输出低电平;当红外线遇到黑线等深色地面时,接收管未接收反射光,施密触发器整形后输出高电平。
四、实验步骤
?? 连接电路。
树莓派 | T型转接板 | 循迹传感器 |
---|---|---|
GPIO0 | G17 | SIG |
5V | 5V | VCC |
GND | GND | GND |
树莓派 | T型转接板 | 双色LED灯 |
---|---|---|
GPIO1 | G18 | R |
- | - | G |
GND | GND | GND |
?? 我使用数字输出DO口,所以控制程序很简单。运行程序,不断循环打印检测信息。当检测到白色物品时打印’White line is detected’,且LED红灯亮;检测到黑色物品时,打印…Black line is detected’,LED灯熄灭。
#!/usr/bin/env python import RPi.GPIO as GPIO TrackPin = 11 LedPin = 12 def setup(): GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location GPIO.setup(LedPin, GPIO.OUT) # Set LedPin's mode is output GPIO.setup(TrackPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.output(LedPin, GPIO.LOW) # Set LedPin LOW to off led def loop(): while True: if GPIO.input(TrackPin) == GPIO.LOW: print 'White line is detected' GPIO.output(LedPin, GPIO.HIGH) # led on
else:
print '...Black line is detected'
GPIO.output(LedPin, GPIO.LOW) # led off
def destroy():
GPIO.output(LedPin, GPIO.LOW) # led off
GPIO.cleanup() # Release resource
if __name__ == '__main__': # Program start from here
setup()
try:
loop()
except KeyboardInterrupt:
# When 'Ctrl+C' is pressed, the child program destroy() will be executed.
destroy()
实验结果示例: