查看 gpio如果 无此命令,可以安装 wiringpi.
root@raspberrypi:/setup# gpio redall
-bash: gpio: command not found
apt install wiringpi
root@raspberrypi:/setup# gpio readall
如上面图片所示,把 引脚40 的BCM为21
gpio readall #列出所有针角
gpio mode 29 out #设置[以writePi编号为29]的GPIO(即GPIO29口) 口为输出模式
gpio read 29 #获取当前GPIO29口的电平(0或1)
gpio write 29 1 #设置当前GPIO29口的电平为1(1为高电平电源开启,)
gpio write 29 0 #设置当前GPIO29口的电平为0(0为低电平电源关闭)
python3 脚本 ,5秒开,5秒关
----------------------------
#!/usr/bin/python
#encoding:utf-8
import RPi.GPIO
import time
time_out = 5
RELAY = 21
RPi.GPIO.setmode(RPi.GPIO.BCM)
RPi.GPIO.setup(RELAY,RPi.GPIO.OUT)
try:
while True:
RPi.GPIO.output(RELAY,RPi.GPIO.HIGH)
time.sleep(time_out)
RPi.GPIO.output(RELAY,RPi.GPIO.LOW)
time.sleep(time_out)
except KeyboardInterrupt:
pass
RPi.GPIO.cleanup()
----------------------------