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

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

介绍

该LED模块包含三个不同的颜色,分别为红色、蓝色和绿色。这三个LED通过公共阴极相互连接,这意味着它们都共享相同的负极连接。红色LED需要1.8V的电压才能点亮,而绿色和蓝色LED每个都需要2.8V的电压。所有LED都在20mA的电流下工作。

利用这个模块,您可以通过调整单个LED的亮度来创建各种颜色组合。通过混合红色、绿色和蓝色,几乎可以创建可见光谱的所有颜色。该模块非常适合需要视觉信号、显示器或装饰性照明效果的项目。

紧凑的设计允许轻松集成到各种电路和项目中。无论您是初学者还是经验丰富的开发人员,这款LED模块都能为您的照明需求提供灵活多样的解决方案。得益于清晰的色彩输出和简单的控制,您可以实现创意和令人印象深刻的灯光装置。

Technical Data
Forward voltage [red] 1,8 V
Forward voltage [green, blue] 2,8 V
Forward current 20 mA

引脚连接

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

DEV BOARD Sensor
Pin 10 LED RED
Pin 11 LED GREEN
Pin 12 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
27
28
int led_red = 10; // Pin for red
int led_green = 11; // Pin for green
int led_blue = 12; // Pin for blue

void setup() {
// Initialization of output pins for the LEDs
pinMode(led_red, OUTPUT);
pinMode(led_green, OUTPUT);
pinMode(led_blue, OUTPUT);
}
// Main program loop
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); // Wait for 3 seconds

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); // Wait for 3 seconds

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); // Wait for 3 seconds
}

2. 进阶版

注意这里的引脚要支持 pwm

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
#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()
{
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(10)

http://blog.jzxer.cn/20241218/20241219-rgb-led-ky009/

作者

dev

发布于

2024-12-18

更新于

2025-01-13

许可协议

评论

Your browser is out-of-date!

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

×