当前位置:网站首页>树莓派:双色LED灯实验
树莓派:双色LED灯实验
2022-04-23 06:10:00 【林-金鹏】
简介
双色LED灯(双基色 LED灯),是指模块只能显示2种颜色,一般是红色和绿色,可以有三种状态:灭、颜色1亮、颜色2亮;根据颜色组合的不同,分为红蓝双色,黄蓝双色,红绿双色等等。
双色 LED常于指示状态:比如红色表示设备异常,绿色表示正常。
一、实验效果
树莓派:双色LED灯实验
二、组件
1、树莓派主板*1
2、树莓派电源*1
3、40P软排线*1
4、双色LED模块*1
5、面包板*1
6、跳线若干
三、实验原理
将引脚S(绿色)和中间管脚R(红色)连接到树莓派的GPO 接口上,对树莓派进行编程控制,将LED的颜色从红色变为绿色,然后使用PWM混合其他颜色。
该模块的原理图如下所示:
四、实验步骤
建立电路:
| 树莓派 | 面包板 | 双色LED模块 |
|---|---|---|
| GPIO1 | GPIO18 | G(S) |
| GPIO0 | GPIO17 | R(中间) |
| GND | GND | GND |
五、代码实现
WM的频率:P决定了输出的数字信号on (1) 和 off(0 )的切换速度,频率越高,切换就越快。
占空比:指一串理想脉冲序列中,正脉冲的持续时间与脉冲总周期的比值,用来调节灯的亮度。
import RPi.GPIO as GPIO
import time
colors = [0xFF00, 0x00FF, 0x0FF0, 0xF00F]
makerobo_pins = (11, 12) # PIN管脚字典
GPIO.setmode(GPIO.BOARD) # 采用实际的物理管脚给GPIO口
GPIO.setwarnings(False) # 去除GPIO口警告
GPIO.setup(makerobo_pins, GPIO.OUT) # 设置Pin模式为输出模式
GPIO.output(makerobo_pins, GPIO.LOW) # 设置Pin管脚为低电平(0V)关闭LED
p_R = GPIO.PWM(makerobo_pins[0], 2000) # 设置频率为2KHz
p_G = GPIO.PWM(makerobo_pins[1], 2000) # 设置频率为2KHz
# 初始化占空比为0(led关闭)
p_R.start(0)
p_G.start(0)
def makerobo_pwm_map(x, in_min, in_max, out_min, out_max):
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
def makerobo_set_Color(col): # 例如:col = 0x1122
R_val = col >> 8
G_val = col & 0x00FF
# 把0-255的范围同比例缩小到0-100之间
R_val = makerobo_pwm_map(R_val, 0, 255, 0, 100)
G_val = makerobo_pwm_map(G_val, 0, 255, 0, 100)
p_R.ChangeDutyCycle(R_val) # 改变占空比
p_G.ChangeDutyCycle(G_val) # 改变占空比
# 调用循环函数
def makerobo_loop():
while True:
for col in colors:
makerobo_set_Color(col)
time.sleep(0.5)
# 释放资源
def makerobo_destroy():
p_G.stop()
p_R.stop()
GPIO.output(makerobo_pins, GPIO.LOW) # 关闭所有LED
GPIO.cleanup() # 释放资源
# 程序入口
if __name__ == "__main__":
try:
makerobo_loop() # 调用循环函数
except KeyboardInterrupt: # 当按下Ctrl+C时,将执行destroy()子程序。
makerobo_destroy() # 释放资源

版权声明
本文为[林-金鹏]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_46105038/article/details/120304693
边栏推荐
- npm ERR code 500解决
- MySQL5. 7 insert Chinese data and report an error: ` incorrect string value: '\ xb8 \ XDF \ AE \ xf9 \ X80 at row 1`
- Oracle索引状态查询与索引重建
- iTOP4412 HDMI显示(4.4.4_r1)
- Binder mechanism principle
- 【2021年新书推荐】Practical IoT Hacking
- DCMTK (dcm4che) works together with dicoogle
- oracle数据库将多个列的查询结果集合并到一行中
- [sm8150] [pixel4] LCD driver
- oracle杀死正在执行的sql
猜你喜欢

Personal blog website construction

Component based learning (1) idea and Implementation

Binder mechanism principle

Ffmpeg common commands

【2021年新书推荐】Practical Node-RED Programming

Itop4412 HDMI display (4.4.4_r1)
![[2021 book recommendation] effortless app development with Oracle visual builder](/img/db/d8802b42d5374e4117db638a0b82b0.png)
[2021 book recommendation] effortless app development with Oracle visual builder

项目,怎么打包

this.getOptions is not a function

Viewpager2 realizes Gallery effect. After notifydatasetchanged, pagetransformer displays abnormal interface deformation
随机推荐
adb shell top 命令详解
MySQL笔记5_操作数据
【2021年新书推荐】Enterprise Application Development with C# 9 and .NET 5
Oracle Job定时任务的使用详解
Three methods to realize the rotation of ImageView with its own center as the origin
ARGB透明度换算
Oracle锁表查询和解锁方法
Android interview Online Economic encyclopedia [constantly updating...]
Itop4412 HDMI display (4.0.3_r1)
【2021年新书推荐】Red Hat RHCSA 8 Cert Guide: EX200
MySQL笔记3_约束_主键约束
最简单完整的libwebsockets的例子
ffmpeg常用命令
Itop4412 cannot display boot animation (4.0.3_r1)
sys.dbms_scheduler.create_job创建定时任务(功能更强大丰富)
Miscellaneous learning
Android暴露组件——被忽略的组件安全
Using stack to realize queue out and in
【2021年新书推荐】Effortless App Development with Oracle Visual Builder
oracle生成毫秒级时间戳