当前位置:网站首页>树莓派入门(3)树莓派GPIO学习
树莓派入门(3)树莓派GPIO学习
2022-08-10 05:25:00 【jualay】
目录
一、安装RPi.GPIO
sudo apt-get update
sudo apt-get install python3-rpi.gpio
二、树莓派4B引脚图
GPIO:就是一些可以改变的引脚,输入或输出为高低电平
三、RPi.GPIO
1.导入它,并用GPIO这个名字来代替
import RPi.GPIO as GPIO
2.检查是否导入成功
try:
import RPi.GPIO as GPIO
except RuntimeError:
print("import RPi.GPIO error")
导入失败就会打印import RPi.GPIO error
3.GPIO编号
要按照GPIO引脚图来使用相应的编号
GPIO.setmode(GPIO.BOARD)
#或者是
GPIO.setmode(GPIO.BCM)
4.GPIO模式
在编程前要将GPIO设置为相应的工作状态
(1)输入
GPIO.setup(channel,GPIO.IN)
(2)输出
GPIO.setup(channel,GPIO.OUT)
设置初始化高电平:
GPIO.setup(channel,GPIO.OUT,initial=GPIO.HIGH)
设置初始化低电平:
GPIO.setup(channel,GPIO.OUT,initial=GPIO.LOW)
(3)多引脚输入输出
创建一个列表,将列表包含的引脚代入
PinList=[pin1,pin2,pin3]
GPIO.setup(PinList,GPIO.IN)
5.读取输入电平
GPIO.input(channel)
6.设置GPIO输出状态
(1)设置高电平(上拉)
GPIO.setup(channel,1)
#或者是
GPIO.setup(channel,GPIO.HIGH)
(2)设置高电平(下拉)
GPIO.setup(channel,0)
#或者是
GPIO.setup(channel,GPIO.LOW)
7.清理GPIO资源
将所有使用过的GPIO的状态变为输入状态,避免短路损坏
GPIO.cleanup()
边栏推荐
- canvas 画布绘制时钟
- 2022 R2 transportable pressure vessel filling operation examination question bank simulation platform
- FPGA工程师面试试题集锦11~20
- 线程(中):线程安全
- summer preschool assignments
- The sword refers to Offer 033. Variation array
- FPGA工程师面试试题集锦21~30
- FPGA工程师面试试题集锦1~10
- Ask you guys.The FlinkCDC2.2.0 version in the CDC community has a description of the supported sqlserver version, please
- Linear Algebra (4)
猜你喜欢
线性代数(四)
Order table delete, insert and search operations
如何模拟后台API调用场景,很细!
线程(上篇):线程的创建
Conda creates a virtual environment method and pqi uses a domestic mirror source to install a third-party library method tutorial
【无标题】
Matlab simulation of multi-factor house price prediction based on BP neural network
Depth of carding: prevent model fitting method
【静态代理】
顺序表的删除,插入和查找操作
随机推荐
Thread.sleep, Thread.yield 作用解释
【Pei Shu Theorem】CF1055C Lucky Days
并发工具类——CountDownLatch、CyclicBarrier、Semaphore、Exchanger的介绍与使用
Consulting cdc 2.0 for mysql does not execute flush with read lock. How to ensure bin
leetcode每天5题-Day10
How to use Apifox's Smart Mock function?
Abstract problem methodology
接口文档进化图鉴,有些古早接口文档工具,你可能都没用过
leetcode每天5题-Day12
awk of the Three Musketeers of Shell Programming
接口调试还能这么玩?
Joomla vulnerability reproduced
Nexus_Warehouse Type
Nexus_仓库类型
I have a dream for Career .
flex related
Matlab simulation of multi-factor house price prediction based on BP neural network
Order table delete, insert and search operations
How to get the last day of a month
Conda creates a virtual environment method and pqi uses a domestic mirror source to install a third-party library method tutorial