樹莓派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
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, 輸入 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編號方式時, 用下面這兩張好了, 不眼花:
終端檢視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-sty
相關推薦
樹莓派GPIO使用大全
簡介 原始碼及其來源 RPi.GPIO是Python的一個module( 模組 ), 樹莓派官方系統預設已經安裝, 仍在不斷更新中, 截至20160314, 最新版0.6.2, 適配了樹莓派3, 訪問 python主頁 . 原始碼(也有教程的連結
樹莓派 GPIO 控制
操作 spi visio vcc ike detail 簡單介紹 發的 model b Raspberr Pi 官方提供了GPIO的usage,具體位置 https://www.raspberrypi.org/documentation/usage/gpio/ 這裏整理了一
java操作樹莓派GPIO控制LED燈--結合springboot實現介面呼叫
1、概述 本文使用java結合springboot實現了對樹莓派GPIO介面的操作以達到控制LED燈的功能 2、pom檔案如下: <project xmlns="http://maven.apache.org/POM/4.0.0" &nb
樹莓派GPIO 基礎(一)
樹莓派3B+ 引腳圖: 其中: 標註了數字的引腳都能夠用作GPIO; 標註了其他名稱的引腳也有一些其他特殊用途標註了其他名稱的引腳也有一些其他特殊用途; SDA 和SCL 引腳構成I2C 介面; MOSI,MISO,SCKL 用於SPI 介面; GPI
樹莓派---GPIO應用詳解
https://www.jianshu.com/p/f31b90cc756f https://www.jianshu.com/p/e93e2fe3f66a http://wiki.jikexueyuan.com/project/raspberry-pi/gpio.html https://blog.cs
樹莓派GPIO 模組使用基礎
這篇日誌的內容應該算是《RPi.GPIO 模組使用基礎》Input 部分的擴充套件講解,詳細講解了 Input 部分的一些高階應用技巧。目前有幾種途徑可以在您的程式中獲得 GPIO 的輸入資訊。第一種也是最簡易的一種為在某個時間點檢查輸入值。這即是所謂的“輪詢(poll
樹莓派GPIO口的使用
引腳物理編號排序:方形焊介面為1腳,兩兩為序,先短後長 管腳編號: BCM: 編號側重 CPU 暫存器,根據 BCM2835 的 GPIO 暫存器編號。 wpi: 編號側重實現邏輯,把擴充套件 GPIO 埠從 0 開始編號,這種編號方便程式設計。正如下圖 WiringPi 欄。 配置GIPO為輸出:
【方法】樹莓派GPIO控制使用教程
文章目錄程式碼 程式碼 # coding:utf-8 # 本程式碼實現 樹莓派+L298N驅動板 小車的2s前進運動 import RPi.GPIO as gpio import time # 定義
樹莓派開發系列教程9——樹莓派GPIO控制
一、常用開源工程簡介 樹莓派核心中已經編譯自帶了gpio的驅動,我們常通過一些第三方寫好的庫函式來完成具體的操作,比較常見的操作庫函式有: 1、python GPIO 【開發語言】——python 【簡單介紹】——樹莓派官方資料中推薦且容易上手。py
樹莓派GPIO控制-flask
樹莓派提供了GPIO介面可進行控制;Flask是一個輕量級Web框架,通過在raspberry Pi上部署flask框架,能夠實現在網路前端對Pi進行GPIO控制 連線關係如之前博文進行設定和連線 利用Xshell工具保持PC和Pi之間的連線關係,便於指令輸入和控制 利用F
樹莓派GPIO
首先需要安裝python-dev。執行命令: sudo apt-get install python-dev然後安裝RPI.GPIO解壓tar xvzf RPi.GPIO-0.6.3a.tar.gz進入資料夾:cd RPi.GPIO-0.6.3/安裝: sudo pyth
樹莓派GPIO學習和LCD時鐘設定
USB 轉 TTL 中的四根線這一端是需要連結到樹莓派 GPIO 上面的。(如下圖) 紅線(VCC)接排針外面一排,靠近顯示燈一端的第一針 黑線(GND)接排針外面一排,靠近顯示燈一端的第三針 白線(TXD)接排針外面一排,靠近顯示燈一端的第四針 綠線(RTX)接排針外面一排,靠近顯示燈一
樹莓派GPIO控制--C語言篇
一. 常用開源工程簡介 樹莓派核心中已經編譯自帶了gpio的驅動,我們常通過一些第三方寫好的庫函式來完成具體的操作,比較常見的操作庫函式有: 1. Python GPIO 【開發語言】——python 【簡單介紹】——樹莓派官方資料中推薦且容易上手。
3、樹莓派3 Model B ——— 樹莓派GPIO點亮第一個LED
1、實驗前準備 樹莓派3B及電源 x1麵包板 x1LED燈 x1電阻220歐 x1 杜邦線 x2 2、實驗原理 樹莓派的IO口可以外接很多外設,如舵機、紅外發送接收模組、繼電器、步進電機、各類感測器、螢幕等。通過這些外設可以製作很多有趣的創客設計,改善我們的生活方式提高我
繼續樹莓派GPIO:兩片74HC595控制一個8x8LED點陣顯示愛心
先上效果圖: 8x8LED點陣其實就是64只LDE按8行,每行8列排列。我用的點陣是共陽的,共有16個引腳,8個做行選擇,8個做列選擇。具體引腳排列規則請參考http://www.docin.com/p-42115784.html。我這裡整理如下: C8 C7 R2
樹莓派GPIO介面命名規則 A different mode has already been set解決方案
在編寫python呼叫GPIO介面的時候,發現很多python設定的 GPIO.setmode(GPIO.BCM) GPIO.setmode(GPIO.BOARD) 太麻煩了,之前沒有把樹莓派的命名規
樹莓派GPIO口上手
前言 玩過微控制器的同學就知道,你的第一個微控制器程式可能就是點亮一個LED燈,下面我們也通過點亮一個LED為目的來介紹下樹莓派的GPIO使用方式。 樹莓派GPIO控制有以下三種方式: Python GPIO 該庫更確切的名稱為raspberry-
樹莓派GPIO口的操作
wiringPi安裝 wiringPi的安裝存在方案A和方案B。wiringPi使用GIT工具維護個更新程式碼,但是如果處於一些其他原因不能使用GIT,那麼也可以使用方案B下載和安裝wiringPi。 方案A——使用GIT工具 如果在你的平臺上還沒有安
Python自動化編程-樹莓派GPIO編程(二)
兩種 安裝 some 分享 拓撲 監聽 導入 說明 true 樹莓派我們編程一般都直接用高效的python,針對於GPIO編程,python也是有這一方面的庫的,這裏最有名也是最常用的就是RPI.GPIO了。這個庫是專門為樹莓派GPIO編程所設計的,利用它你可以愉快的像Ar
樹莓派通過GPIO控制步進電機
tps 分享 images logs http 分享圖片 .com 控制 圖片 一、接線方式與GPIO調用方法: 電源接入+5v和GND In1-4分別接GPIO1-4 正轉時,GPIO1-4分次傳入:[1,0,0,0],[sleep],[0,1,0,0],[sl