GPIO 与 40-pin

Raspberry Pi GPIO与40-pin 对应
2944f1654660374432707799f1e18ad5_GPIO-Pinout-Diagram-2.png
任何GPIO引脚都可以在软件中指定为输入或输出,适用广泛用途。
5ffb9800ec0b306295b64cae6255f14b_GPIO.png
::: warning
GPIO 引脚的编号不按数字顺序排列;板上存在 GPIO 引脚 0 和 1(物理引脚 27 和 28),但保留用于高级用途(见下文)。
:::

Voltages

两个5V引脚、两个3.3V引脚,以及一些不可配置的接地引脚(0V)。意味着输出设置为3.3V,输入为3.3V容差

Outputs

GPIO作为输出,可以设为高(3.3V) 或者低(0V).

Inputs

GPIO作为输入,可以读取为高(3.3V) 或者低(0V)。使用上拉或下拉电阻器可以更轻松地实现。GPIO2 和 GPIO3 固定上拉电阻,其他引脚可以在软件中配置。

More

除了简单的输入和输出设备,GPIO引脚还可以用于各种替代功能,有些可用于所有引脚,有些可用于特定引脚。
PWM (脉冲宽度调制)
Software PWM available on all pins
Hardware PWM available on GPIO12, GPIO13, GPIO18, GPIO19
SPI
SPI0:MOSI (GPIO10); MISO (GPIO9); SCLK (GPIO11); CE0 (GPIO8), CE1 (GPIO7)
SPI1: MOSI (GPIO20); MISO (GPIO19); SCLK (GPIO21); CE0 (GPIO18); CE1 (GPIO17); CE2 (GPIO16)
I2C
Data: (GPIO2); Clock (GPIO3)
EPROM Data: (GPIO0); EEPROM Clock (GPIO1)
Serial
TX (GPIO14); RX (GPIO15)

GPIO pinout

可以在终端窗口,运行 pinout 查看参考信息。这工具又 GPIO Zero Python库提供,Raspberry Pi os 默认已经安装。
6bb037de1d2cac11d8243de924ed50db_gpiozero-pinout.png
有关 GPIO 引脚高级功能的更多详细信息,参考.

Permissions

为了能使用GPIO端口,你需要将用户加到 gpio 组。 pi 用户默认是在gpio组中,其他用户需要自己加入。sudo usermod -a -G gpio <username>

GPIO in Python

GPIO Zero 可以很方便使用python控制GPIO,文档参考.

LED

使用GPIO17 控制led,示例:

from gpiozero import LED
from time import sleep

led = LED(17)

while True:
    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)

while True:
    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)

while True:
    if button.is_pressed:
        led.on()
    else:
        led.off()

或者:

from gpiozero import LED, Button

led = LED(17)
button = Button(2)

while True:
    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

深入更多关于 GPIO Zero Python库,参考

参考微信搜索:树莓派开发者 了解更多信息 www.edatec.cn/cn


树莓派开发者
1 声望0 粉丝

基于 Raspberry Pi 产品开发、项目应用、设计相关知识的分享。