我用树莓派2B+ 控制继电器,指令是用python编写的,代码及连线照片如下。目前测试功能时候,指令执行到GPIO.setup(18, GPIO.OUT)时灯亮,退出程序时灯灭,但时期间我输入1或0都无法控制灯亮灭- import RPi.GPIO as GPIO
- import time
- def contorl(sign):
- if sign == '1':
- GPIO.output(18, GPIO.HIGH)
- elif sign == '0':
- GPIO.output(18, GPIO.LOW)
- if __name__ == '__main__':
- GPIO.setmode(GPIO.BCM)
- GPIO.setup(18, GPIO.OUT)
- while True:
- sign = input('请输入指令,开灯为1,关灯为0,退出为exit :')
- if sign != 'exit':
- contorl(sign)
- else:
- break
- GPIO.cleanup()
复制代码
|