<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>GPIO :: 标签 :: yafeng 的博客</title><link>http://yafengabc.github.io/tags/gpio/index.html</link><description/><generator>Hugo</generator><language>zh-cn</language><lastBuildDate>Sat, 19 Sep 2026 14:36:30 +0800</lastBuildDate><atom:link href="http://yafengabc.github.io/tags/gpio/index.xml" rel="self" type="application/rss+xml"/><item><title>让普通用户可以控制树莓派的GPIO(Archlinuxarm)</title><link>http://yafengabc.github.io/cnblogs/p8873397/index.html</link><pubDate>Wed, 18 Apr 2018 11:52:00 +0800</pubDate><guid>http://yafengabc.github.io/cnblogs/p8873397/index.html</guid><description>Raspbian上的Rpi.GPIO库是可以在普通用户下控制树莓派的GPIO的，然而在ArchlinuxARM下，却需要root用户才行，这无疑会引起安全问题，好在RPi.GPIO提供了一个获取权限的脚本：create_gpio_user_permissions.py&#10;然而这个脚本是没法在ArchlinuxARM上直接运行的，因为Archlinux缺少adduser命令。&#10;这个脚本是这样的：&#10;import grp import subprocess def ensure_gpiogroup(): try: grp.getgrnam('gpio') except KeyError: print('GPIO group does not exist - creating...') subprocess.call(['groupadd', '-f', '-r', 'gpio']) subprocess.call(['adduser', 'pi', 'gpio']) # in future, also for groups: # spi # i2c add_udev_rules() def add_udev_rules(): with open('/etc/udev/rules.d/99-gpio.rules','w') as f: f.write("""SUBSYSTEM=="bcm2835-gpiomem", KERNEL=="gpiomem", GROUP="gpio", MODE="0660" SUBSYSTEM=="gpio", KERNEL=="gpiochip*", ACTION=="add", PROGRAM="/bin/sh -c 'chown root:gpio /sys/class/gpio/export /sys/class/gpio/unexport ; chmod 220 /sys/class/gpio/export /sys/class/gpio/unexport'" SUBSYSTEM=="gpio", KERNEL=="gpio*", ACTION=="add", PROGRAM="/bin/sh -c 'chown root:gpio /sys%p/active_low /sys%p/direction /sys%p/edge /sys%p/value ; chmod 660 /sys%p/active_low /sys%p/direction /sys%p/edge /sys%p/value'" """) if __name__ == '__main__': ensure_gpiogroup() 反正很简单，手动搞进去就行了</description></item><item><title>树莓派高级GPIO库，wiringpi2 for python使用笔记（三）GPIO操作</title><link>http://yafengabc.github.io/cnblogs/p5096720/index.html</link><pubDate>Sun, 03 Jan 2016 16:57:00 +0800</pubDate><guid>http://yafengabc.github.io/cnblogs/p5096720/index.html</guid><description>GPIO库的核心功能，当然就是操作GPIO了，GPIO就是“通用输入/输出”接口，比如点亮一个LED、继电器等，或者通过iic spi 1-wire等协议，读取、写入数据，这都是GPIO的用处，可以说没有GPIO，树莓派只能当小电脑用，有了GPIO，就升级成一个控制器了。先来说说怎么操作一个数字量（高低电平）。&#10;先看代码：&#10;import wiringpi2 as gpio from wiringpi2 import GPIO gpio.wiringPiSetup() #初始化 gpio.pinMode(25,GPIO.OUTPUT) # 把pin25设置为输出模式 gpio.digitalWrite(25,GPIO.HIGH) #pin25输出为高电平 print(gpio.digitalRead(25)) #打印pin25的状态 值的注意的是，GPIO在输出模式时，也可以读取GPIO状态。&#10;wiringpi对树莓派2的GPIO定义如下：&#10;[root@RasPi ~/testcode]# gpio readall +-----+-----+---------+------+---+---Pi 2---+---+------+---------+-----+-----+ | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM | +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+ | | | 3.3v | | | 1 || 2 | | | 5v | | | | 2 | 8 | SDA.1 | IN | 1 | 3 || 4 | | | 5V | | | | 3 | 9 | SCL.1 | IN | 1 | 5 || 6 | | | 0v | | | | 4 | 7 | GPIO. 7 | IN | 1 | 7 || 8 | 1 | ALT0 | TxD | 15 | 14 | | | | 0v | | | 9 || 10 | 1 | ALT0 | RxD | 16 | 15 | | 17 | 0 | GPIO. 0 | IN | 0 | 11 || 12 | 0 | IN | GPIO. 1 | 1 | 18 | | 27 | 2 | GPIO. 2 | IN | 0 | 13 || 14 | | | 0v | | | | 22 | 3 | GPIO. 3 | IN | 0 | 15 || 16 | 0 | IN | GPIO. 4 | 4 | 23 | | | | 3.3v | | | 17 || 18 | 0 | IN | GPIO. 5 | 5 | 24 | | 10 | 12 | MOSI | IN | 0 | 19 || 20 | | | 0v | | | | 9 | 13 | MISO | IN | 0 | 21 || 22 | 0 | IN | GPIO. 6 | 6 | 25 | | 11 | 14 | SCLK | IN | 0 | 23 || 24 | 1 | IN | CE0 | 10 | 8 | | | | 0v | | | 25 || 26 | 1 | IN | CE1 | 11 | 7 | | 0 | 30 | SDA.0 | IN | 1 | 27 || 28 | 1 | IN | SCL.0 | 31 | 1 | | 5 | 21 | GPIO.21 | IN | 1 | 29 || 30 | | | 0v | | | | 6 | 22 | GPIO.22 | IN | 1 | 31 || 32 | 0 | IN | GPIO.26 | 26 | 12 | | 13 | 23 | GPIO.23 | IN | 0 | 33 || 34 | | | 0v | | | | 19 | 24 | GPIO.24 | IN | 0 | 35 || 36 | 0 | IN | GPIO.27 | 27 | 16 | | 26 | 25 | GPIO.25 | IN | 0 | 37 || 38 | 0 | IN | GPIO.28 | 28 | 20 | | | | 0v | | | 39 || 40 | 0 | IN | GPIO.29 | 29 | 21 | +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+ | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM | +-----+-----+---------+------+---+---Pi 2---+---+------+---------+-----+-----+ 我们看到，wringpi对针脚有三种定义方式，BCM代表GPIO模式，wPi代表pin模式，Physical代表物理针脚模式。</description></item><item><title>树莓派高级GPIO库，wiringpi2 for python使用笔记（一）安装</title><link>http://yafengabc.github.io/cnblogs/p5096300/index.html</link><pubDate>Sun, 03 Jan 2016 14:06:00 +0800</pubDate><guid>http://yafengabc.github.io/cnblogs/p5096300/index.html</guid><description>网上的教程，一般Python用RPi.GPIO来控制树莓派的GPIO，而C/C++一般用wringpi库来操作GPIO，RPi.GPIO过于简单，很多高级功能不支持，比如i2c/SPI库等，也缺乏高精度定时等高级特性。相比之下，wiringpi则功能丰富的多，其实wringpi已经有了python绑定，可以非常简单的在python中使用这个库。鉴于网上基本没有这个库的中文说明，我一边学习，一边以做笔记的形式，写几篇关于这个库的基本使用的文章。&#10;安装：首先安装python-pip：&#10;我用的Archlinux，python3，安装命令为：&#10;pacman -S python-pip 如果用python2，安装命令为：&#10;pacman -S python2-pip Raspbian下则为：&#10;apt-get install python3-pip apt-get install python-pip 安装完后，就可以用pip install来安装python库了。为避免繁琐，我下边的命令都以pip命令安装，Archlinux下默认为python3的pip3，如果使用个python2则用pip2来代替pip，debian下pip默认为pip2，若使用python3，则使用pip3来代替。&#10;pip install wiringpi2 pip库里除了wiringpi2外，还有老版本的wiringpi库，大家按需安装。&#10;安装完后，运行pip list，可以看到列表中包含了新装的wringpi2库了：&#10;在终端中敲入python，进入python控制台，导入一下，如果不报错，说明安装成功：</description></item></channel></rss>