树莓派GPIO使用大全

简介 源代码及其来源

RPi.GPIO是Python的一个module( 模块 ), 树莓派官方系统默认已经安装, 仍在不断更新中, 截至20160314, 最新版0.6.2,
适配了树莓派3, 访问 python主页 .

源代码(也有教程的链接)公布在Sourceforge上: 点 这里 查看源代码.

简介及注意事项:

A module to control Raspberry Pi GPIO channels.

This package provides a class to control the GPIO on a Raspberry Pi.

Note that this module is unsuitable for real-time or timing critical applications. This is because you can not predict when Python will be busy garbage collecting. It also runs under the Linux kernel which is not suitable for real time applications - it is
multitasking O/S and another process may be given priority over the CPU, causing jitter in your program. If you are after true real-time performance and predictability, buy yourself an Arduino http://www.arduino.cc !

Note that the current release does not support SPI, I2C, hardware PWM or serial functionality on the RPi yet. This is planned for the near future - watch this space! One-wire functionality is also planned.

Although hardware PWM is not available yet, software PWM is available to use on all channels.

大致含义是: 这个包提供了操作树莓派GPIO的一个类, 不是实时的, 不支持硬件的SPI,I2C,PWM等, 但软件PWM可以在所有通道使用. 如果想要实时控制, 推荐Arduino.

源代码包中可以看到下列文件:

source文件夹中放置了 C语言操作底层寄存器以及外设封装的源码, 感兴趣的可以下载下来研究一下:

test文件夹中放了test.py, 大量的测试代码, 很有参考价值:

RPi Board Information and RPi.GPIO version

树莓派官方系统默认已经安装了Python和RPi.GPIO, 树莓派终端输入命令 python --version 可查看Python版本,
输入命令 python 启动python, 输入 import
RPi.GPIO as GPIO
 导入RPi.GPIO模块.

To discover information about your RPi:

<code class="hljs avrasm has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; ">GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.RPI</span>_INFO</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li></ul>

To discover the Raspberry Pi board revision:

<code class="hljs bash has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; ">GPIO.RPI_INFO[<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box; ">'P1_REVISION'</span>]
GPIO.RPI_REVISION    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box; ">#(deprecated 弃用)</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li></ul>

To discover the version of RPi.GPIO:

<code class="hljs avrasm has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; ">GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.VERSION</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li></ul>

RPi.GPIO使用说明

像学一门语言第一个程序是”Hello World”一样, 搞硬件的第一个任务是 点灯(Blink). 先来看下点灯的程序吧:

<code class="hljs python has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; "><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box; ">#!/usr/bin/python</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">import</span> RPi.GPIO <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">as</span> GPIO
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">import</span> sys
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">import</span> time
LED = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">21</span>
<span class="hljs-function" style="box-sizing: border-box; "><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">def</span> <span class="hljs-title" style="box-sizing: border-box; ">main</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box; ">()</span>:</span>
    GPIO.setwarnings(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">False</span>)
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(LED,GPIO.OUT)
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">while</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">True</span>):
        GPIO.output(LED,<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">True</span>)
        time.sleep(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">0.5</span>)
        GPIO.output(LED,<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">False</span>)
        time.sleep(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">0.5</span>)
main()</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">3</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">4</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">5</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">6</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">7</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">8</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">9</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">10</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">11</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">12</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">13</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">14</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">15</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">3</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">4</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">5</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">6</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">7</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">8</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">9</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">10</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">11</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">12</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">13</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">14</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">15</li></ul>

LED接在BCM编号方式下的21引脚, 每隔0.5s亮一次, time.sleep(), 延时秒数. 类似Arduino中的delay(), 当然,后者是延时的ms数.

具体每句话的意思, 往下面看:

Importing the module ( 导入模块 )

To import the RPi.GPIO module(导入RPi.GPIO模块):

<code class="hljs haskell has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; "><span class="hljs-import" style="box-sizing: border-box; "><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">import</span> RPi.GPIO <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">as</span> GPIO</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li></ul>

To import the module and check to see if it is successful( 判断检查导入模块是否成功, 可能需要超级用户权限执行: sudo python xx.py ):

<code class="hljs python has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; "><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">try</span>:
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">import</span> RPi.GPIO <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">as</span> GPIO
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">except</span> RuntimeError:
    print(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box; ">"Error importing RPi.GPIO!  This is probably because you need superuser privileges.  You can achieve this by using 'sudo' to run your script"</span>)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">3</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">4</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">3</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">4</li></ul>

Pin numbering ( 引脚编号 )

There are two ways of numbering the IO pins on a Raspberry Pi within RPi.GPIO. The first is using the BOARD numbering system. This refers to the pin numbers on the P1 header of the Raspberry Pi board. The advantage of using this numbering system is that your
hardware will always work, regardless of the board revision of the RPi. You will not need to rewire your connector or change your code.

The second numbering system is the BCM numbers. This is a lower level way of working - it refers to the channel numbers on the Broadcom SOC. You have to always work with a diagram of which channel number goes to which pin on the RPi board. Your script could
break between revisions of Raspberry Pi boards.

大致含义: 树莓派引脚有BOARD和BCM两种编号方式( 使用python时? 似乎使用C还有一种wringPi编号方式 ), BOARD具有很好的适用性( 不用看接口图,数引脚1~40就可以接线 ), 不论树莓派1 2 3, 都不用修改代码, 吼啊! BCM编号方式换个版本再接线时数引脚是不行的, 需要看下下面的接口图…不难看出推荐用BOARD编号方式. 但很多程序中使用BCM方式.

下面给出一张树莓派2B的硬件接口图( 来源找不到了,侵删 ):

图中的GPIOxx的方框即是BCM编码方式, 直接写数字的深灰框是BOARD编码方式, 如BCM编码方式的 GPIO02 对应BOARD编码方式的 3.

只需要使用BCM编号方式时, 用下面这两张好了, 不眼花:

图片来源:

https://www.raspberrypi.org/documentation/usage/gpio-plus-and-raspi2/README.md

http://elinux.org/RPi_Low-level_peripherals#I.C2.B2C

终端查看BCM编号的方式:

To specify which you are using using (mandatory)(确认使用的引脚编号方式):

<code class="hljs avrasm has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; ">GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.setmode</span>(GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.BOARD</span>)
  <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; "># or</span>
GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.setmode</span>(GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.BCM</span>)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">3</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">3</li></ul>

To detect which pin numbering system has been set (for example, by another Python module)(检测使用的是哪种编号方式):

<code class="hljs mizar has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; ">#The <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">mode</span> will <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">be</span> GPIO.BOARD, GPIO.BCM <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">or</span> None
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">mode</span> = GPIO.getmode()</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li></ul>

Warning(警告)

It is possible that you have more than one script/circuit on the GPIO of your Raspberry Pi. As a result of this, if RPi.GPIO detects that a pin has been configured to something other than the default (input), you get a warning when you try to configure a script.
To disable these warnings:

树莓派可能不止有一个脚本/电路在操纵GPIO, 如果树莓派检测到引脚不是默认的输入状态, 会给出警告, 可以用一行代码避免警告:

<code class="hljs mathematica has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; ">GPIO.setwarnings(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">False</span>)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li></ul>

Setup up a channel(设置一个通道)

You need to set up every channel you are using as an input or an output.

你需要设置用作输入输出的引脚.

To configure a channel as an input( 通道设置为输入 ) :

<code class="hljs avrasm has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; ">GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.setup</span>(channel, GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.IN</span>)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li></ul>

(where channel is the channel number based on the numbering system you have specified (BOARD or BCM))( channel参数为BOARD或BCM的引脚编号 ).

To read the value of a GPIO pin( 读输入引脚的值 ):

<code class="hljs lasso has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; "><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box; ">#This</span> will <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">return</span> either <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">0</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box; ">/</span> GPIO<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box; ">.</span>LOW <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box; ">/</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box; ">False</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box; ">or</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">1</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box; ">/</span> GPIO<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box; ">.</span>HIGH <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box; ">/</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box; ">True</span><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box; ">.</span>
GPIO<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box; ">.</span>input(channel) </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li></ul>

To set up a channel as an output( 设置为输出 ):

<code class="hljs avrasm has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; ">GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.setup</span>(channel, GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.OUT</span>)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li></ul>

To set the output state of a GPIO pin( 设置输出引脚的状态 ):

<code class="hljs lasso has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; "><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box; ">#State</span> can be <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">0</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box; ">/</span> GPIO<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box; ">.</span>LOW <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box; ">/</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box; ">False</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box; ">or</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">1</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box; ">/</span> GPIO<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box; ">.</span>HIGH <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box; ">/</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box; ">True</span><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box; ">.</span>
GPIO<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box; ">.</span>output(channel, state)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li></ul>

You can also specify an initial value for your output channel( 可以初始化引脚的输入输出和电平 ):

<code class="hljs avrasm has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; ">GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.setup</span>(channel, GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.OUT</span>, initial=GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.HIGH</span>)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li></ul>

Setup more than one channel( 设置超过一个通道 )

You can set up more than one channel per call (release 0.5.8 onwards). For example:

每次调用你可以设置超过一个通道(0.5.8版本之后):

<code class="hljs avrasm has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; ">chan_list = [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">11</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">12</span>]    <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; "># add as many channels as you want!</span>
                       <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; "># you can tuples instead i.e.:</span>
                       <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">#   chan_list = (11,12)</span>
GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.setup</span>(chan_list, GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.OUT</span>)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">3</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">4</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">3</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">4</li></ul>

You can output to many channels in the same call (release 0.5.8 onwards). For example:

每次调用你可以设置输出到多个通道(0.5.8版本之后):

<code class="hljs avrasm has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; ">chan_list = [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">11</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">12</span>]                             <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; "># also works with tuples</span>
GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.output</span>(chan_list, GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.LOW</span>)                <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; "># sets all to GPIO.LOW</span>
GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.output</span>(chan_list, (GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.HIGH</span>, GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.LOW</span>))   <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; "># sets first HIGH and second LOW</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">3</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">3</li></ul>

用的是Python的列表.

Cleanup(清除引脚设置恢复默认)

At the end any program, it is good practice to clean up any resources you might have used. This is no different with RPi.GPIO. By returning all channels you have used back to inputs with no pull up/down, you can avoid accidental damage to your RPi by shorting
out the pins. Note that this will only clean up GPIO channels that your script has used. Note that GPIO.cleanup() also clears the pin numbering system in use.

程序结束时, 最好清除引脚设置并恢复默认.

To clean up at the end of your script:

<code class="hljs avrasm has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; ">GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.cleanup</span>()</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li></ul>

It is possible that don’t want to clean up every channel leaving some set up when your program exits. You can clean up individual channels, a list or a tuple of channels:

有可能程序退出时, 不想清除某些通道的设置, 你可以使用python的列表或元组 来只清除某几个通道:

<code class="hljs avrasm has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; ">GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.cleanup</span>(channel)
GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.cleanup</span>( (channel1, channel2) )
GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.cleanup</span>( [channel1, channel2] )</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">3</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">3</li></ul>

输入的高级应用

关于Input引脚状态的检测

There are several ways of getting GPIO input into your program. The first and simplest way is to check the input value at a point in time. This is known as ‘polling’ and can potentially miss an input if your program reads the value at the wrong time. Polling
is performed in loops and can potentially be processor intensive. The other way of responding to a GPIO input is using ‘interrupts’ (edge detection). An edge is the name of a transition from HIGH to LOW (falling edge) or LOW to HIGH (rising edge).

有polling( 轮询 )式 和 interrupt( 中断 )式( edge detection 边缘检测 ).

(如: 单片机中, 轮询并不一定在while中, 也可以在定时器中, 如在时基定时器中使用静态变量和定时器间隔进行按键消抖, 可以避免全部使用外部中断带来的中断消耗, 当然树莓派使用Python有时可能要稍微转变下思想, 从中断转成线程, 从串行到并行? RPi.GPIO提供了按键消抖的GPIO函数, 具体可以参看下面).

设置上拉下拉电阻

If you do not have the input pin connected to anything, it will ‘float’. In other words, the value that is read in is undefined because it is not connected to anything until you press a button or switch. It will probably change value a lot as a result of receiving
mains interference.

To get round this, we use a pull up or a pull down resistor. In this way, the default value of the input can be set. It is possible to have pull up/down resistors in hardware and using software. In hardware, a 10K resistor between the input channel and 3.3V
(pull-up) or 0V (pull-down) is commonly used. The RPi.GPIO module allows you to configure the Broadcom SOC to do this in software:

树莓派引脚默认是高阻状态?如用于按键时, 按键按下之前读的时候值不确定, 可以用内部上拉下拉电阻(10K, 也有说10K以上)来稳定电平, 这样引脚外部就不用接电阻了:

<code class="hljs avrasm has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; ">GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.setup</span>(channel, GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.IN</span>, pull_up_down=GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.PUD</span>_UP)
  <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; "># or</span>
GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.setup</span>(channel, GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.IN</span>, pull_up_down=GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.PUD</span>_DOWN)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">3</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">3</li></ul>

用轮询的方式检测输入

You can take a snapshot(快照) of an input at a moment in time( 及时地 ):

<code class="hljs lua has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; "><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">if</span> GPIO.input(channel):
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box; ">print</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box; ">'Input was HIGH'</span>)
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">else</span>:
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box; ">print</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box; ">'Input was LOW'</span>)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">3</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">4</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">3</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">4</li></ul>

To wait for a button press by polling in a loop(循环中轮询按键是否按下):

<code class="hljs vhdl has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; "><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">while</span> GPIO.input(channel) == GPIO.LOW:
    <span class="hljs-typename" style="color: rgb(102, 0, 102); box-sizing: border-box; ">time</span>.sleep(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">0.01</span>)  # <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">wait</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">10</span> ms <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">to</span> give CPU chance <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">to</span> do other things</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li></ul>

(this assumes that pressing the button changes the input from LOW to HIGH)(假设按键按下输入从低到高)

边缘检测

An edge is the change in state of an electrical signal from LOW to HIGH (rising edge) or from HIGH to LOW (falling edge). Quite often, we are more concerned by a change in state of an input than it’s value. This change in state is an event.

To avoid missing a button press while your program is busy doing something else, there are two ways to get round this:

the wait_for_edge() functionthe

event_detected() function

a threaded callback function that is run when an edge is detected

我们很多时候并不关心电平值, 而关心电平 从低到高 或 从高到低 的变化(如编码器测速/按键按下弹开等), 为避免主程序忙于其它事情错过引脚的电平改变, 有两种方式: wait_for_edge() 函数和 event_detected() 函数.
threaded callback(见下一小节)函数(一个新的线程)在检测到边缘后会运行.

( 检测的两个函数是直接调用了底层的寄存器, 还是开了新线程的软中断 还是要去看看源代码) ((⊙_⊙)?明白的小伙伴可以在评论区解释一下这两个函数实现的原理).

①wait_for_edge() function

The wait_for_edge() function is designed to block execution of your program until an edge is detected. In other words, the example above that waits for a button press could be rewritten as:

wait_for_edge() 函数设计成block execution ((⊙_⊙)?)知道一个边缘被检测, 上面 循环中轮询按键是否按下
可以重写为:

<code class="hljs avrasm has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; ">GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.wait</span>_for_edge(channel, GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.RISING</span>)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li></ul>

Note that you can detect edges of type GPIO.RISING, GPIO.FALLING or GPIO.BOTH. The advantage of doing it this way is that it uses a negligible amount of CPU, so there is plenty left for other tasks.

If you only want to wait for a certain length of time, you can use the timeout parameter:

注意检测的边缘参数有 GPIO.RISING, GPIO.FALLING or GPIO.BOTH (上升沿, 下降沿 或 升降沿), 这样用几乎不占用CPU, 如果你只希望在确定的时间段内查询, 可以使用 timeout 参数:

<code class="hljs vhdl has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; "># <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">wait</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">for</span> up <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">to</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">5</span> seconds <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">for</span> a rising edge (timeout <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">is</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">in</span> milliseconds)
channel = GPIO.wait_for_edge(channel, GPIO_RISING, timeout=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">5000</span>)
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">if</span> channel <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">is</span> None:
    print(<span class="hljs-attribute" style="box-sizing: border-box; ">'Timeout</span> occurred')
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">else</span>:
    print(<span class="hljs-attribute" style="box-sizing: border-box; ">'Edge</span> detected <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">on</span> channel', channel)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">3</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">4</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">5</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">6</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">3</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">4</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">5</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">6</li></ul>

②event_detected() function

The event_detected() function is designed to be used in a loop with other things, but unlike polling it is not going to miss the change in state of an input while the CPU is busy working on other things. This could be useful when using something like Pygame
or PyQt where there is a main loop listening and responding to GUI events in a timely basis.

event_detected()函数被设计用来在循环中使用, 不同于polling轮询, 当CPU忙于其它事时, 不会错过引脚状态的改变, 这使得Pygame 或 PyQt 用主循环监听时可以及时回应GUI事件.((⊙_⊙)?).

<code class="hljs oxygene has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; ">#Note that you can detect events <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">for</span> GPIO.RISING, GPIO.FALLING <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">or</span> GPIO.BOTH.
GPIO.add_event_detect(channel, GPIO.RISING)  # <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">add</span> rising edge detection <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">on</span> a channel
do_something()
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">if</span> GPIO.event_detected(channel):
    print(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box; ">'Button pressed'</span>)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">3</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">4</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">5</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">3</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">4</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">5</li></ul>

Threaded callbacks

RPi.GPIO runs a second thread for callback functions. This means that callback functions can be run at the same time as your main program, in immediate response to an edge.

RPi.GPIO为边缘检测的回调函数又开了一个线程.

For example:

<code class="hljs python has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; "><span class="hljs-function" style="box-sizing: border-box; "><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">def</span> <span class="hljs-title" style="box-sizing: border-box; ">my_callback</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box; ">(channel)</span>:</span>
    print(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box; ">'This is a edge event callback function!'</span>)
    print(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box; ">'Edge detected on channel %s'</span>%channel)
    print(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box; ">'This is run in a different thread to your main program'</span>)
GPIO.add_event_detect(channel, GPIO.RISING, callback=my_callback)  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box; "># add rising edge detection on a channel</span>
...the rest of your program...</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">3</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">4</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">5</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">6</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">3</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">4</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">5</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">6</li></ul>

If you wanted more than one callback function(如果你想要不止一个回调函数):

<code class="hljs python has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; "><span class="hljs-function" style="box-sizing: border-box; "><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">def</span> <span class="hljs-title" style="box-sizing: border-box; ">my_callback_one</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box; ">(channel)</span>:</span>
    print(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box; ">'Callback one'</span>)
<span class="hljs-function" style="box-sizing: border-box; "><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">def</span> <span class="hljs-title" style="box-sizing: border-box; ">my_callback_two</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box; ">(channel)</span>:</span>
    print(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box; ">'Callback two'</span>)
GPIO.add_event_detect(channel, GPIO.RISING)
GPIO.add_event_callback(channel, my_callback_one)
GPIO.add_event_callback(channel, my_callback_two)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">3</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">4</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">5</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">6</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">7</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">3</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">4</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">5</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">6</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">7</li></ul>

回调函数只有一个线程:

Note that in this case, the callback functions are run sequentially, not concurrently. This is because there is only one thread used for callbacks, in which every callback is run, in the order in which they have been defined.

注意因为边缘检测的回调函数都在一个线程中, 有先后顺序.

Switch debounce ( 开关去抖 )

You may notice that the callbacks are called more than once for each button press. This is as a result of what is known as ‘switch bounce’. There are two ways of dealing with switch bounce:

add a 0.1uF capacitor across your switch.

software debouncing

a combination of both

To debounce using software, add the bouncetime= parameter to a function where you specify a callback function. Bouncetime should be specified in milliseconds. For example:

由于存在开关抖动(用示波器可以看到), 每次按下开关会调用多次回调函数, 这不是我们希望的, 有两种方式处理开关抖动:

①在开关两个引脚之间添加一个0.1uF的电容

②软件消抖

③二者结合使用

使用软件消抖时, 给回调函数添加一个弹跳时间的参数( bouncetime= ), 弹跳时间(参照单片机可以为10~20ms)在ms级别, 下面的程序用200ms来消抖:

<code class="hljs oxygene has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; "># <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">add</span> rising edge detection <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">on</span> a channel, ignoring further edges <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">for</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">200</span>ms <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">for</span> switch bounce handling
GPIO.add_event_detect(channel, GPIO.RISING, callback=my_callback, bouncetime=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">200</span>)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li></ul>

Remove event detection(移除边缘检测)

If for some reason, your program no longer wishes to detect edge events, it is possible to stop them(由于某些原因, 你的程序可能不希望用边缘检测了, 可以停止它们):

<code class="hljs avrasm has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; ">GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.remove</span>_event_detect(channel)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li></ul>

Using PWM in RPi.GPIO (RPi.GPIO的软件PWM)

To create a PWM instance(创建一个PWM实例):

<code class="hljs fix has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; "><span class="hljs-attribute" style="box-sizing: border-box; ">p </span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box; "> GPIO.PWM(channel, frequency)</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li></ul>

To start PWM(开启PWM):

<code class="hljs sql has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; ">p.<span class="hljs-operator" style="box-sizing: border-box; "><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">start</span>(dc)   # <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">where</span> dc <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">is</span> the duty cycle (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">0.0</span> <= dc <= <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">100.0</span>)</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li></ul>

To change the frequency(改变频率):

<code class="hljs oxygene has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; ">p.ChangeFrequency(freq)   # <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">where</span> freq <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">is</span> the <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">new</span> frequency <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">in</span> Hz</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li></ul>

To change the duty cycle(改变占空比):

<code class="hljs avrasm has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; ">p<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.ChangeDutyCycle</span>(dc)  <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; "># where 0.0 <= dc <= 100.0</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li></ul>

To stop PWM(停止PWM):

<code class="hljs vbnet has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; ">p.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">stop</span>()</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li></ul>

Note that PWM will also stop if the instance variable ‘p’ goes out of scope.

注意实例变量’p’越界时PWM将停止.

An example to blink an LED once every two seconds(一个每2秒闪灯的例子, 固定占空比):

<code class="hljs vhdl has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; ">import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setup(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">12</span>, GPIO.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">OUT</span>)
p = GPIO.PWM(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">12</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">0.5</span>)
p.start(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">1</span>)
input(<span class="hljs-attribute" style="box-sizing: border-box; ">'Press</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">return</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">to</span> stop:')   # <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">use</span> raw_input <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">for</span> Python <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">2</span>
p.stop()
GPIO.cleanup()</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">3</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">4</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">5</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">6</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">7</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">8</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">9</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">3</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">4</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">5</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">6</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">7</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">8</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">9</li></ul>

An example to brighten/dim an LED(一个改变LED明暗亮度的例子, 改变占空比):

<code class="hljs avrasm has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; ">import time
import RPi<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.GPIO</span> as GPIO
GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.setmode</span>(GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.BOARD</span>)
GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.setup</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">12</span>, GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.OUT</span>)
p = GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.PWM</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">12</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">50</span>)  <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; "># channel=12 frequency=50Hz</span>
p<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.start</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">0</span>)
<span class="hljs-label" style="box-sizing: border-box; ">try:</span>
    while <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">1</span>:
        for dc <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">in</span> range(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">101</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">5</span>):
            p<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.ChangeDutyCycle</span>(dc)
            time<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.sleep</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">0.1</span>)
        for dc <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; ">in</span> range(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">100</span>, -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">1</span>, -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">5</span>):
            p<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.ChangeDutyCycle</span>(dc)
            time<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.sleep</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; ">0.1</span>)
except KeyboardInterrupt:
    pass
p<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.stop</span>()
GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.cleanup</span>()</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">3</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">4</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">5</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">6</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">7</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">8</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">9</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">10</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">11</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">12</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">13</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">14</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">15</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">16</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">17</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">18</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">3</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">4</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">5</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">6</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">7</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">8</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">9</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">10</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">11</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">12</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">13</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">14</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">15</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">16</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">17</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">18</li></ul>

gpio_function(channel)

Shows the function of a GPIO channel(返回GPIO通道功能的函数).

For example:

<code class="hljs avrasm has-numbering" style="display: block; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; ">import RPi<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.GPIO</span> as GPIO
GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.setmode</span>(GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.BOARD</span>)
func = GPIO<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; ">.gpio</span>_function(pin)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">3</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 6px; padding-right: 0px; padding-bottom: 40px; padding-left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style-type: none; list-style-position: initial; list-style-image: initial; text-align: right; "><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">1</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">2</li><li style="box-sizing: border-box; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; ">3</li></ul>

will return a value from:

GPIO.IN, GPIO.OUT, GPIO.SPI, GPIO.I2C, GPIO.HARD_PWM, GPIO.SERIAL, GPIO.UNKNOWN.

(我居然没试成功/(ㄒoㄒ)/~~)

时间: 2024-11-11 03:32:22

树莓派GPIO使用大全的相关文章

树莓派GPIO控制

陈拓[email protected] 2018.06.09/2018.06.10 0.  概述 本文介绍树莓派 Zero W的GPIO控制,并用LED看效果. 0.1 树莓派GPIO编号方式 功能物理引脚 从左到右,从上到下:左边基数,右边偶数:1-40 BCM 编号侧重CPU寄存器,根据BCM2835的GPIO寄存器编号. wiringPi 编号侧重实现逻辑,把扩展GPIO端口从0开始编号,这种编号方便编程.如图 WiringPi一栏. 操作GPIO时一定先要清楚使用那一套编号. 1.  准

树莓派 GPIO 控制

Raspberr Pi 官方提供了GPIO的usage,具体位置 https://www.raspberrypi.org/documentation/usage/gpio/ 这里整理了一下使用c语言做开发的实际例子. 常用开源工程简介 树莓派内核中已经编译自带了 gpio 的驱动,我们常通过一些第三方写好的库函数来完成具体的操作,比较常见的操作库函数有: python GPIO 开发语言--python 简单介绍--树莓派官方资料中推荐且容易上手.python GPIO 是一个小型的 pytho

树莓派GPIO输入输出--控制LED

GPIO引脚有两种模式BOARD和BCM. 1.GPIO输出控制LED 效果图: 代码: (1)使用BOARD模式,GPIO.setmode(GPIO.BOARD).35号引脚在BCM下是GPIO19 (2)将引脚35设置为输出模式,GPIO.setup(35,GPIO.OUT) (3)对GPIO35引脚输出高电平,也可以为GPIO.HIGH 2.GPIO接口获取输入信号 效果图: 电路图 当开关断开时,GPIO23是高点平获取到的输入信号是Ture.当开关按下时GPIO23为接地,获取到的信号

树莓派GPIO口驱动编写

一.wiringpi写法 #include <wiringPi.h> #include <stdlib.h> int main(int argc,char *argv[]) { if(argc<2) return -1; if(argc==2) { wiringPiSetup(); pinMode(0, OUTPUT); if(atoi(argv[1]) == 1) digitalWrite(0, HIGH); else if(atoi(argv[1]) == 0) digi

树莓派GPIO引脚标识图

原文地址:https://www.cnblogs.com/Chorder/p/9114470.html

树莓派2代B model 上手初体验,不用显示器,Python GPIO 点亮一颗LED

开题:[好东西,值得研究!] 标题:树莓派2代B model 上手初体验,不用显示器,Python GPIO 点亮一颗LED [知识普及] 1,树莓派各版本对比: 2,树莓派2代BModel 主板,图样 树莓派2 代B GPIO 图 [所需硬件] 一张TF卡,8G或者8G以上,我的是 [三星TF卡16g class10 EVO] 一根网线,让树莓派与路由器连接 一个5V 500MA 的普通USB电源,为树莓派供电 ,我试过了,5V 500ma没问题 一个树莓派2代B 一个普通路由器[如果你连路由

(10)用树莓派B+的GPIO接口控制发光二极管闪烁

从本篇开始,将逐步学习和分享树莓派GPIO相关操作,由于本人也是这方面的小白,关于接口.跳线.面包板.电流电压之类的叙述,可能有不准确的地方,还请大家仅将本系列文章作为入门读物. 本文需要的一些元件:母对公杜邦线N条,面包板1块,暂不需要面包板外接电源,发光二极管(电压2V-3.3V),电阻约400欧1个. 必须要了解的知识:根据树莓派官方文档的描述(见https://www.raspberrypi.org/documentation/hardware/raspberrypi/power/REA

树莓派学习笔记——使用文件IO操作GPIO SysFs方式

0 前言 本文描述如果通过文件IO sysfs方式控制树莓派 GPIO端口.通过sysfs方式控制GPIO,先访问/sys/class/gpio目录,向export文件写入GPIO编号,使得该GPIO的操作接口从内核空间暴露到用户空间,GPIO的操作接口包括direction和value等,direction控制GPIO方向,而value可控制GPIO输出或获得GPIO输入. Linux学习可从应用出发,先不纠结Linux驱动编写,先把Linux给玩起来. [相同与不同] 本文和[EasyARM

(12)树莓派B+ GPIO控制四驱车

吐槽一下:本人还是个小白,CSDN文章分类不知道选哪个 如果你手上的材料是五线四相步进电机驱动板ULN2003+五线四相步进电机28BYJ-48-5V,那么应该算是非常幸运,因为这两样是配套的,一个驱动板可以接一个步进电机,实现正转.反转.加速.减速的功能(个人猜测).很多人因为在购置树莓派外围设备时,没有考虑清楚,随随便便就买了电机和驱动板,导致无法正常驱动电机,譬如我买的是带4个直流电机的四驱车,其中的电机只有两条线接正极和负极,以及ULN2003五线四相步进电机驱动板,这两样是不配套的,不