GPIO 与 40-pin Raspberry Pi GPIO与40-pin 对应
任何GPIO引脚都可以在软件中指定为输入或输出,适用广泛用途。
::: warning GPIO 引脚的编号不按数字顺序排列;板上存在 GPIO 引脚 0 和 1(物理引脚 27 和 28),但保留用于高级用途(见下文)。 ::: Voltages两个5V引脚、两个3.3V引脚,以及一些不可配置的接地引脚(0V)。意味着输出设置为3.3V,输入为3.3V容差 OutputsGPIO作为输出,可以设为高(3.3V) 或者低(0V). InputsGPIO作为输入,可以读取为高(3.3V) 或者低(0V)。使用上拉或下拉电阻器可以更轻松地实现。GPIO2 和 GPIO3 固定上拉电阻,其他引脚可以在软件中配置。 More除了简单的输入和输出设备,GPIO引脚还可以用于各种替代功能,有些可用于所有引脚,有些可用于特定引脚。 PWM (脉冲宽度调制)
SPI
I2C
Serial
GPIO pinout可以在终端窗口,运行 pinout 查看参考信息。这工具又 GPIO Zero python库提供,Raspberry Pi os 默认已经安装。
有关 GPIO 引脚高级功能的更多详细信息,参考.
Permissions为了能使用GPIO端口,你需要将用户加到 gpio 组。 pi 用户默认是在gpio组中,其他用户需要自己加入。 sudo usermod -a -G gpio <username>GPIO in PythonGPIO Zero 可以很方便使用python控制GPIO,文档参考. LED使用GPIO17 控制led,示例:
from gpiozero import LED
from time import sleep- led = LED(17)
- whileTrue:
- led.on()
sleep(1)
- led.off()
sleep(1)
使用 python file 运行示例,Led将反复闪烁和熄灭。 LED 函数包括: on(), off(), toggle(), and blink(). BUTTON使用GPIO2 读取按键状态,示例: - from gpiozero import Button
- from time import sleep
- button = Button(2)
- whileTrue:
- if button.is_pressed:
- print("Pressed")
- else:
- print("released")
- sleep(1)
按钮功能包括 属性: is_pressed 、 is_held 回调函数: when_pressed 、 when_released 、 when_held 方法:wait_for_press 、wait_for_release BUTTON + LED按钮和LED组合使用: - from gpiozero import LED, Button
- led = LED(17)
- button = Button(2)
- whileTrue:
- if button.is_pressed:
- led.on()
- else:
- led.off()
或者: - from gpiozero import LED, Button
- led = LED(17)
- button = Button(2)
- whileTrue:
- button.wait_for_press()
- led.on()
- button.wait_for_release()
- led.off()
or: - from gpiozero import LED, Button
- led = LED(17)
- button = Button(2)
- button.when_pressed = led.on
- button.when_released = led.off
|