Raspberry Pi Blink(Python RPi.GPIO)
来源:互联网 发布:node w3cschool 编辑:程序博客网 时间:2024/05/17 20:35
上次我们使用WiringPi 来Blink, 这次使用Python的RPi.GPIO模块来操作树莓派的GPIO.
安装RPi.GPIO
① 方法一 (APT)
sudo apt-get install python-rpi.gpio
②方法二 (pip)
安装Python的pip工具, 然后使用使用Python的pip(pip is a package management system used to install and manage software packages written in Python. pip is already installed if you're using Python 2 >=2.7.9 or Python 3 >=3.4
)来安装RPi.GPIO.
先来查看一下树莓派的python版本, 输入命令 python --version
:
我的python版本是2.7.9, 所以已经安装了pip, 没有安装pip的可以用这个命令 sudo apt-get install python-pip
来安装.
使用pip安装RPi.GPIO
sudo pip install RPi.GPIO
RPi.GPIO使用说明
RPi.GPIO的使用可以参考我的另一篇博客: RPi.GPIO 官方使用文档翻译 .
Blink
①接线
输入 gpio readall
命令查看引脚命名:
我们把BCM编号方式的17引脚接到LED的正极, LED负极引脚接10kΩ电阻到GND. 如图:
②程序
切换到/home/pi/Desktop目录, 新建一个Blink_Python的文件夹, 切换到该文件夹, 新建一个 blink.py的文件:
cd /home/pi/Desktop/mkdir Blink_Pythoncd Blink_Pythonsudo nano blink.py
写入以下代码:
#!/usr/bin/python # -*- coding: utf-8 -*- import RPi.GPIO as GPIO import time # BCM GPIO编号 LED = 17#初始化函数 def setup(): # 采用BCM编号 GPIO.setmode(GPIO.BCM) #忽略警告 GPIO.setwarnings(False) # 设置LED为输出状态,且输出低电平 GPIO.setup(LED, GPIO.OUT) GPIO.output(LED, GPIO.LOW) #循环 def loop(): while True: GPIO.output(LED, GPIO.HIGH) #LED亮 time.sleep(0.5) #延时0.5s GPIO.output(LED, GPIO.LOW) #LED灭 time.sleep(0.5)#退出前的清除函数 def destroy(): GPIO.cleanup(LED) if __name__ == '__main__': # 初始化GPIO setup() try: loop() except KeyboardInterrupt: # 恢复GPIO口状态 destroy()
按下Ctrl+X, 然后Y, 然后回车, 输入以下命令执行:
sudo python blink.py
可以看到LED闪烁, 按下 Ctrl+C
结束程序运行, 可以试着自己修改下代码中的参数来查看效果.
参考
树莓派学习笔记——RPi.GPIO 流水灯
- Raspberry Pi Blink(Python RPi.GPIO)
- Raspberry Pi+Python+ GPIO
- How To Fix Error Loading RPi.GPIO Python Library On Your Brand New Raspberry Pi 2
- python GPIO安装 How To Fix Error Loading RPi.GPIO Python Library On Your Brand New Raspberry Pi 2
- 树莓派 raspberry pi GPIO python
- Raspberry Pi Blink--NO delay(WiringPi)
- Raspberry Pi GPIO with Python (A WiringPi wrapper :D)
- Python RPi GPIO编程控制
- 【RPi树莓派使用指南】An Introduction to Raspberry Pi
- Raspberry Pi I2C (Python)
- Raspberry Pi + Python + Serial
- raspberry-gpio-python
- raspberry Pi on Archlinux, Install wiringPi , GPIO
- 【智能家居】Raspberry Pi GPIO接口篇
- 在树莓派上用Python Rpi.GPIO调试液晶模块
- Python, Crawler and Raspberry Pi
- 通过Raspberry Pi(树莓派)的GPIO接口控制发光二极管
- Raspberry Pi 2 ubuntu wiringPi GPIO点亮LED
- Android Fragment 基本介绍
- Java操作oracle中的事物回滚
- 开始写技术博客
- c# 读取excle表
- RxJava使用场景小结
- Raspberry Pi Blink(Python RPi.GPIO)
- php极速入门
- 使用new和vector实现一维和二维数组
- 21、Oracle中drop user和drop user cascade的区别?
- Mybatis实现Mapper动态代理方式
- 十五个数的顺序排列
- php极速入门2
- 报错storage size of ‘act’ isn’t known当使用std=c99编译struct sigaction
- 计算时间差