接线步骤
在开始之前,确保你已经准备好了以下材料:
接下来,按照以下步骤进行接线:
树莓派引脚 | 激光传感器引脚 |
---|
GPIO11 (物理编码) | SIG (信号) |
GND | GND (-) |
5V | Vcc (电源) |
编程步骤
完成接线后,需要编写代码来控制激光发射器。以下是示例代码:
import RPi.GPIO as GPIO
import time
laserpin = 11
GPIO.setmode(GPIO.BOARD)
GPIO.setup(laserpin, GPIO.OUT)
GPIO.output(laserpin, GPIO.LOW)
try:
while True:
GPIO.output(laserpin, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(laserpin, GPIO.LOW)
time.sleep(0.5)
except KeyboardInterrupt:
GPIO.output(laserpin, GPIO.LOW)
GPIO.cleanup()