树莓派GPIO设置和使用输出

  • Post author:
  • Post category:其他





学习目标:


如何使用树莓派的GPIO打开和关闭来控制树莓派的输出端口





学习内容:


1、简单的格式


1.1 设置输入内容的方式


GPIO.setup(port_or_pin,GPIO.IN)


GPIO.setup(port_or_pin,GPIO.OUT)


1.2 然后把端口/引脚切换到3.3V(等于1/GPIO)


GPIO.output(port_or_pin,1)


或者,将端口/引脚切换到0V(等于0/GPIO)


GPIO.output(port_or_pin,0)


2、如何设置输出——完整的Python代码

import RPI.GPIO as GPIO  #导入RPI.GPIO模块
GPIO.setmode(GPIO.BCM) #选择BCM板
GPIO.setup(port_or_pin,GPIO.OUT)  #设置端口/引脚作为输出
GPIO.output(port_or_pin,1)        #设置端口/引脚值为1
GPIO.output(port_or_pin.0)        #设置端口/引脚值为0

还可以设置端口时对端口初始化,并附加可选参数值 initial=x (x为1或0)

GPIO.setup(port_or_pin,GPIO.OUT,initial=1)

GPIO.setup(port_or_pin,GPIO.OUT,initial=0)

举例:BCM板的GPIO24设置为一个输出,并半秒打开和关闭一次,直到我们退出CTRL+C,

import RPi.GPIO as GPIO            # import RPi.GPIO module 导入RPI.GPIO模块 
from time import sleep             # lets us have a delay  设置延迟
GPIO.setmode(GPIO.BCM)             # choose BCM or BOARD  选择板
GPIO.setup(24, GPIO.OUT)           # set GPIO24 as an output   
  
try:  
    while True:  
        GPIO.output(24, 1)         # set GPIO24 to 1/GPIO.HIGH/True  
        sleep(0.5)                 # wait half a second  
        GPIO.output(24, 0)         # set GPIO24 to 0/GPIO.LOW/False  
        sleep(0.5)                 # wait half a second  
  
except KeyboardInterrupt:          # trap a CTRL+C keyboard interrupt 发生中断 
    GPIO.cleanup()                 # resets all GPIO ports used by this program  重置次程序使用的所有GPIO端口

3、读取输出引脚的状态

读取引脚是1还是0

GPIO.input(24)

import RPI.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)
GPIO.setup(24,GPIO.OUT) 

try:
    while True:
        GPIO.output(24,1)
        sleep(0.5)
        if GPIO.input(24) : #如果24引脚输出为1
            print("high")
        GPIO.output(24,0)
        sleep(0.5)
        if not GPIO.input(24):
            print("low")
except KeyboardInterrupt: #ctrl+C就发生中断
    GPIO.cleanup()        #重置所有的引脚

其他例子:建议使用我们的 SD 卡或图像之一,如果您不是,您将需要安装

RPi.GPIO 库

sudo python
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17,GPIO.IN)
input = GPIO.input(17)
while True:
  if (GPIO.input(17)):
    print("Button Pressed")
import time
#initialise a previous input variable to 0 (assume button not pressed last)
prev_input = 0
while True:
  #take a reading
  input = GPIO.input(17)
  #if the last reading was low and this one high, print
  if ((not prev_input) and input):
    print("Button pressed")
  #update previous input
  prev_input = input
  #slight pause to debounce
  time.sleep(0.05
import RPi.GPIO as GPIO
import time
import os

#adjust for where your switch is connected
buttonPin = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(buttonPin,GPIO.IN)

while True:
  #assuming the script to call is long enough we can ignore bouncing
  if (GPIO.input(buttonPin)):
    #this is the script that will be called (as root)
    os.system("python /home/pi/start.py")

sudo python run.py

sudo nano /etc/rc.local

exit 0

python /home/pi/run.py








版权声明:本文为weixin_49166146原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。