bobo | NPC | 创造力: | 帖子: | 发消息 | 串个门 | 加好友 | 打招呼
2014-6-22 21:21:09 [显示全部楼层]
33974浏览
查看: 33974|回复: 6

[教程] 《边学边用树莓派-6》树莓派通过Python控制 GPIO

[复制链接]
Python的教程可以参考  《简明Python教程》  
在树莓派系统里面已经安装了两个版本的Python,Python 3不完全向下兼容 Python 2。当前咱们用Python 2即可。可以通过命令行测试:
  1. root@raspberrypi:/home/pi# python
  2. Python 2.7.3 (default, Jan 13 2013, 11:20:46)
  3. [GCC 4.6.3] on linux2
  4. Type "help", "copyright", "credits" or "license" for more information.
  5. >>> 2+3
  6. 5
  7. >>>
复制代码

检测 是否有安装好GPIO模块,如果没有出错提示,说明有安装
  1. >>> import RPi.GPIO as GPIO
复制代码

如果没有安装模块先按Ctrl+d 退出Python命令行,通过如下命令安装:
  1. root@raspberrypi:/home/pi# apt-get update
  2. root@raspberrypi:/home/pi# apt-get install python-rpi.gpio
复制代码
安装完后从新导入GPIO模块测试一下

此时我们可以先通过Python命令行的形式来测试下GPIO,这也是Python比较方便的地方
  1. >>> import RPi.GPIO as GPIO
  2. >>> GPIO.setmode(GPIO.BCM)
  3. >>> GPIO.setup(25,GPIO.OUT)
  4. >>> GPIO.output(25,GPIO.HIGH)
  5. >>> GPIO.output(25,GPIO.LOW)
复制代码

接下来我们编写一个LED灯闪烁的代码,并保存为blink.py   在这里 使用 SSH方式登录树莓派 然后使用nano来新建并保存文件实际上是速度最快最好操作的
  1. #!usr/bin/python
  2. import RPi.GPIO as GPIO
  3. import time
  4. GPIO.setmode(GPIO.BCM)
  5. GPIO.setup(25,GPIO.OUT)
  6. while True:
  7.     GPIO.output(25,GPIO.HIGH)
  8.     time.sleep(1)
  9.     GPIO.output(25,GPIO.LOW)
  10.     time.sleep(1)
复制代码
GPIO.setmode(GPIO.BCM) 是设置GPIO命名模式,我们使用芯片厂Broadcom编码方式
time.sleep(1)   是使用time模块的sleep函数 暂停 1秒

通过命令执行blink.py
  1. root@raspberrypi:/home/pi# python blink.py
复制代码
看看连接在GPIO25口上的LED是否在闪烁
按 Ctrl+c 推出程序

下面我们来测试 GPIO的输入功能,我们在GPIO24上接一个按钮,然后新建一个button.py 的文件编写以下代码:
  1. import RPi.GPIO as GPIO
  2. import time
  3. GPIO.setmode(GPIO.BCM)
  4. GPIO.setup(24,GPIO.IN)
  5. count = 0
  6. while True:
  7.     inputValue = GPIO.input(24)
  8.     if(inputValue == True):           #读取GPIO24状态
  9.         time.sleep(0.02)                #延迟20ms 重新读取GPIO24状态
  10.         inputValue = GPIO.input(24)
  11.         if(inputValue == True):      #如果两次都是高电平,说明有按钮按下,滤除干扰信号
  12.             while True:
  13.                 inputValue = GPIO.input(24)
  14.                 if(inputValue == False):        # 等待按钮释放才退出检测第二此按下动作
  15.                     count = count + 1
  16.                     print"Button pressed " + str(count) + " time."
  17.                     break
  18.             time.sleep(.01)            #在 while 死循环里面休眠,这样CPU其他任务才能执行
  19.     time.sleep(.01)
复制代码

执行这个代码
  1. root@raspberrypi:/home/pi# python button.py
  2. Button pressed 1 time.
  3. Button pressed 2 time.
  4. Button pressed 3 time.
  5. Button pressed 4 time.
复制代码

spoon  学徒

发表于 2022-12-9 20:51:32

  print"Button pressed " + str(count) + " time."这句报错
回复

使用道具 举报

BoBo  NPC

发表于 2022-12-23 14:41:59

spoon 发表于 2022-12-9 20:51
print"Button pressed " + str(count) + " time."这句报错

可能是Python版本问题,加括号
print("Button pressed " + str(count) + " time.")
回复

使用道具 举报

花生编程  中级技匠

发表于 2023-8-20 11:12:31

厉害厉害
回复

使用道具 举报

花生编程  中级技匠

发表于 2023-8-20 11:15:04

赞赞赞赞赞
回复

使用道具 举报

三春牛-创客  初级技神

发表于 2023-8-27 10:28:39

厉害厉害
回复

使用道具 举报

三春牛-创客  初级技神

发表于 2023-8-27 10:29:40

不错不错
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail