当前位置:网站首页>树莓派GPIO引脚控制红绿灯与轰鸣器
树莓派GPIO引脚控制红绿灯与轰鸣器
2022-07-02 03:42:00 【loong_XL】
参考:
https://blog.csdn.net/weixin_44415639/article/details/114600919
https://blog.csdn.net/chenbo163/article/details/78716269
https://blog.51cto.com/u_11643026/4291142
这里文章用的编程引脚是物理引擎代号BOARD
1、GPIO控制包安装
参考:https://blog.csdn.net/weixin_42078116/article/details/119329032
gpio -v 版本2.52才行,2.50查看gpio readall输出报错
需要单独安装解决:
wget https://project-downloads.drogon.net/wiringpi-latest.deb
sudo dpkg -i wiringpi-latest.deb
gpio readall
2、代码控制红绿灯和有源轰鸣器
1)红绿灯
GND引脚用来接地,(GND引脚插在9,红色对应插在11;黄色插在13,绿色插在15)
2)有源轰鸣器
3个引脚,GND引脚用来接地,VCC引脚用来接3.3V的电源,I/O引脚用来GPIO编程控制蜂鸣器;(GND引脚插在39,这里VCC对应插在17;I/O引脚插在37)
有源轰鸣器,因为是低电平触发响,初始时设置为高电平GPIO.HIGH就可以不响
#!/usr/bin/python
#coding:utf-8
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
# 进行引脚的初始化,因为是低电平触发,初始时设置为高电平
GPIO.setup(37, GPIO.OUT,initial=GPIO.HIGH) #轰鸣器
GPIO.setup(11,GPIO.OUT) #红
GPIO.setup(13,GPIO.OUT) #黄
GPIO.setup(15,GPIO.OUT) #绿
def blink():
#通过交替输出高低电频来是LED灯闪烁
#红灯亮10s
GPIO.output(11,GPIO.HIGH)
time.sleep(10)
GPIO.output(11,GPIO.LOW)
time.sleep(0.5)
#黄灯亮3s
GPIO.output(13,GPIO.HIGH)
time.sleep(3.0)
GPIO.output(13,GPIO.LOW)
time.sleep(0.5)
# 绿灯亮10s
GPIO.output(15,GPIO.HIGH)
time.sleep(10)
GPIO.output(15,GPIO.LOW)
time.sleep(0.5)
# 黄灯亮3s
GPIO.output(13,GPIO.HIGH)
time.sleep(3.0)
GPIO.output(13,GPIO.LOW)
GPIO.output(37,GPIO.LOW)
time.sleep(3.0)
GPIO.output(37,GPIO.HIGH)
#主程序
try:
#循环执行
while True:
blink()
except KeyboardInterrupt:
pass
GPIO.cleanup()

树莓派 红绿灯 GPIO
边栏推荐
猜你喜欢

Nacos 配置中心整体设计原理分析(持久化,集群,信息同步)

Large screen visualization from bronze to the advanced king, you only need a "component reuse"!

The 9th Blue Bridge Cup single chip microcomputer provincial competition

Detailed explanation of ThreadLocal

2022-07-01:某公司年会上,大家要玩一食发奖金游戏,一共有n个员工, 每个员工都有建设积分和捣乱积分, 他们需要排成一队,在队伍最前面的一定是老板,老板也有建设积分和捣乱积分, 排好队后,所有

蓝桥杯单片机省赛第十届

Introduction to Robotics II. Forward kinematics, MDH method

蓝桥杯单片机省赛第十一届第一场

The 5th Blue Bridge Cup single chip microcomputer provincial competition

The second game of the 12th provincial single chip microcomputer competition of the Blue Bridge Cup
随机推荐
[designmode] builder model
Visual slam Lecture 3 -- Lie groups and Lie Algebras
Kotlin基础学习 14
软件测试人的第一个实战项目:web端(视频教程+文档+用例库)
Kotlin基础学习 17
Unity脚本的基础语法(6)-特定文件夹
One of the future trends of SAP ui5: embrace typescript
高性能 低功耗Cortex-A53核心板 | i.MX8M Mini
蓝桥杯单片机数码管技巧
High performance and low power cortex-a53 core board | i.mx8m Mini
In the era of programmers' introspection, five-year-old programmers are afraid to go out for interviews
近段时间天气暴热,所以采集北上广深去年天气数据,制作可视化图看下
Qt的网络连接方式
The 11th Blue Bridge Cup single chip microcomputer provincial competition
蓝桥杯单片机省赛第十届
MySQL index, transaction and storage engine
Oracle 查看被锁的表和解锁
Blue Bridge Cup single chip microcomputer sixth temperature recorder
接口调试工具模拟Post上传文件——ApiPost
How about Ping An lifetime cancer insurance?