Arduino RGB LED 模組
常見使用的三色LED,不是透過通訊控制,而是透過IO控制紅藍綠三個發光二極體,使燈管亮滅進行光的三原色混合進行調色。
實驗目標
透過Arduino 控制PWM輸出至RGB 模組調整色彩顏色
電路接線
RGB – 分別對應到arduino NANO 腳位
arduino NANO
RGB 模組
PIN3
R
PIN5
B
PIN6
G
GND
-
與arduino NANO接線前,需確認模組功能正常
使用電表的短路偵測檔位進行測試,模組上的"-"腳接黑色探棒,分別用紅色探棒依序,碰觸RGB腳,確認顏色是否正常,如下圖所示
程式碼
//RGB
LED
#define
G_PIN 3
#define
B_PIN 5
#define
R_PIN 6
int G_PIN_Duty = 0;
int B_PIN_Duty = 0;
int R_PIN_Duty = 0;
void RGB_Demo(void)
{
//Red Light On/Off
analogWrite(R_PIN, 255);
delay(1000);
analogWrite(R_PIN, 0);
delay(1000);
//Green Light On/Off
analogWrite(G_PIN, 255);
delay(1000);
analogWrite(G_PIN, 0);
delay(1000);
//Blue Light On/Off
analogWrite(B_PIN, 255);
delay(1000);
analogWrite(B_PIN, 0);
delay(1000);
}
void setup() {
analogWrite(G_PIN, 0);
analogWrite(B_PIN,
0);
analogWrite(R_PIN, 0);
RGB_Demo();
}
void Control_LED(int PIN, int DelayusTime, int Lightmode)
{
if (Lightmode ==
true)//Off to On
{
for (int i = 0; i <= 255; i++)
{
analogWrite(PIN, i);
delayMicroseconds(DelayusTime);
}
}
else //On to Off
{
for (int i = 255; i >= 0; i--)
{
analogWrite(PIN, i);
delayMicroseconds(DelayusTime);
}
}
}
void loop() {
Control_LED(R_PIN,3000,true);
Control_LED(G_PIN,3000,true);
Control_LED(B_PIN,3000,true);
delay(2000);
Control_LED(R_PIN,3000,false);
Control_LED(G_PIN,3000,false);
Control_LED(B_PIN,3000,false);
delay(2000);
Control_LED(R_PIN,3000,true);
Control_LED(G_PIN,3000,true);
delay(2000);
Control_LED(R_PIN,3000,false);
Control_LED(G_PIN,3000,false);
delay(2000);
Control_LED(G_PIN,3000,true);
Control_LED(B_PIN,3000,true);
delay(2000);
Control_LED(G_PIN,3000,false);
Control_LED(B_PIN,3000,false);
delay(2000);
Control_LED(R_PIN,3000,true);
Control_LED(B_PIN,3000,true);
delay(2000);
Control_LED(R_PIN,3000,false);
Control_LED(B_PIN,3000,false);
delay(2000);
}
注意事項
1. LED 電流跟電壓注意,記得配電阻避免電流過大
2. 每家廠牌的可能不一樣,若沒有使用模組直接使用,建議串聯電阻,可避免燒毀
3. 模組使用前先使用電表確認功能
參考資料
https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/
https://zh.wikipedia.org/wiki/%E4%B8%89%E5%8E%9F%E8%89%B2%E5%85%89%E6%A8%A1%E5%BC%8F
沒有留言:
發佈留言