树莓派4b的GPIO是其输入输出模块。
- 硬件接口
- 软件环境配置
1)检查python版本
python
2)按python的GPIO库
apt-get install rpi.gpio
检查GPIO
gpio readall
不识别GPIO版本,需要安装wiringPi(http://wiringpi.com/)
wget https://project-downloads.drogon.net/wiringpi-latest.deb sudo dpkg -i wiringpi-latest.deb
再次检查
gpio readall
pinout
其他常见硬件的pin检查:https://pinout.xyz/boards
- 软硬件结合测试
1)硬件连接
2)测试代码test2.py
注意此处操作对象按照gpio readall中的name项,另外以下脚本不能再win10下采用text文本存储。
#!/usr/bin/python ## Blinking LED ################################### import RPi.GPIO as GPIO ## Import GPIO library import time ## Need this for blink delay GPIO.setmode(GPIO.BOARD) ## Use board pin numbering GPIO.setwarnings(False) ## Disable "Channel already ## in use" warning led = 7 ## Variable for pin number GPIO.setup(led, GPIO.OUT) ## Set pin to output ## Blink the LED 60 times, once per second for 2 minutes print "Blinking" ## Blinking in progress for x in range(0, 59): ## repeats 60 times GPIO.output(led, 1) ## Turn LED on time.sleep(1) ## Keep it on for 1 second GPIO.output(led, 0) ## Turn LED off time.sleep(1) ## Wait 1 second GPIO.cleanup()
3)效果:
测试通过
- linux shell直接控制
注意此处操作对象按照gpio readall中的BCM项
echo 4 > /sys/class/gpio/export #创建一个可控硬件GPIO7的BCM为4 echo out > /sys/class/gpio/gpio4/direction 控制GPIO4的方向 echo 1 > /sys/class/gpio/gpio4/value 让PIN处于高电压3.3V echo 0 > /sys/class/gpio/gpio4/value 让PIN处于低电压0V echo 4 > /sys/class/gpio/unexport #释放端口
测试通过
- 补充
安装截图软件,为了使用screenshot截图,需要安装
apt-get scrot
参考资料:
- http://www.52pi.net/archives/1918
- Learning Computer Architecture with Raspberry Pi by Eben Upton, Jeffrey Duntemann 2016 (1st Edition)
- https://pinout.xyz/pinout/wiringpi
- http://wiringpi.com/download-and-install/
- https://www.cnblogs.com/guochaoxxl/p/11728108.html
- https://blog.csdn.net/qq_43161960/article/details/108305708