108个传感器之-三色LED(15)

108个传感器之-三色LED(15)

介绍

img

该LED模块包含通过公共阴极连接的红色,蓝色和绿色LED。红色LED的正向电压为1.8 V,而绿色和蓝色LED则需要2.8V。所有LED的正向电流为20 mA。使用此模块,您可以通过调整单个LED的亮度来创建不同的颜色。它是需要视觉信号或彩色显示器的应用。通用阴极使连接并集成到各种项目中,无论是用于装饰照明,状态显示还是其他创意应用程序。

Technical Data
Forward voltage [Red] 1.8 V
Forward voltage [Green, Blue] 2.8 V
Forward current 20 mA

引脚连接

pin 引脚连接开发板的 gpio 接口即可:

Arduino Sensor
Pin 2 LED Red
Pin 3 LED Green
Pin 4 LED Blue
GND GND

代码示例

1. 普通版

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
int led_red = A2; // Pin for red
int led_green = A3; // Pin for green
int led_blue = A4; // Pin for blue

void setup() {
pinMode(led_red, OUTPUT);
pinMode(led_green, OUTPUT);
pinMode(led_blue, OUTPUT);
}

void loop () {
digitalWrite(led_red, HIGH); // red LED is switched on
digitalWrite(led_green, LOW); // green LED is switched off
digitalWrite(led_blue, LOW); // blue LED is switched off
delay(3000);

digitalWrite(led_red, LOW); // red LED is switched off
digitalWrite(led_green, HIGH); // green LED is switched on
digitalWrite(led_blue, LOW); // blue LED is switched off
delay(3000);

digitalWrite(led_red, LOW); // red LED is switched off
digitalWrite(led_green, LOW); // green LED is switched off
digitalWrite(led_blue, HIGH); // blue LED is switched on
delay(3000);
}

2. 渐变版

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <Arduino.h>

// led pin 定义
const int redPin = A2;
const int greenPin = A3;
const int bluePin = A4;
const int freq = 5000; // PWM 信号频率(单位 Hz)
const int redChannel = 0; // PWM 通道
const int greenChannel = 1; // PWM 通道
const int blueChannel = 2; // PWM 通道
const int resolution = 8; // PWM 分辨率,8位代表 0-255 的范围

void setup()
{
Serial.begin(9600);
ledcSetup(redChannel, freq, resolution);
ledcAttachPin(redPin, redChannel);
ledcSetup(greenChannel, freq, resolution);
ledcAttachPin(greenPin, greenChannel);
ledcSetup(blueChannel, freq, resolution);
ledcAttachPin(bluePin, blueChannel);
}

void loop()
{
// 让 PWM 渐变从 0 到 255(亮度从暗到亮)
for (int dutyCycle = 0; dutyCycle <= 255; dutyCycle++) {
ledcWrite(redChannel, dutyCycle); // 设置 PWM 占空比
ledcWrite(greenChannel, 255 - dutyCycle); // 设置 PWM 占空比
ledcWrite(blueChannel, 128 - dutyCycle); // 设置 PWM 占空比
delay(10); // 延迟 10 毫秒,实现渐变效果
}

// 让 PWM 渐变从 255 到 0(亮度从亮到暗)
for (int dutyCycle = 255; dutyCycle >= 0; dutyCycle--) {
ledcWrite(redChannel, dutyCycle); // 设置 PWM 占空比
ledcWrite(greenChannel, 255 - dutyCycle); // 设置 PWM 占空比
ledcWrite(blueChannel, 128 - dutyCycle); // 设置 PWM 占空比
delay(10); // 延迟 10 毫秒,实现渐变效果
}

小结

待完善…

108个传感器之-三色LED(15)

http://blog.jzxer.cn/20241218/20241219-3led/

作者

dev

发布于

2024-12-18

更新于

2025-01-04

许可协议

评论

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×