本文是上一篇「點亮 nanoESP32-C6 開發板上的彩燈」的續篇,這裡要使用範例裡的程式直接點亮開發板上的彩燈。
彩燈會先點亮白光,接著熄滅,然後依序點亮紅光>綠光>藍光,然後熄滅。彩燈會依照上述順序跑迴圈點亮彩燈。
準備材料
1. nanoESP32-C6 開發板 *1
2. Type-C USB 線 *1
提醒您:因 ESP32 在開啟 Wifi 和 BLE 時會耗用較大的電流,因此建議您準備品質好一點的 USB 傳輸線。
電路接線
nanoESP32-C6 開發板自帶 CH343x USB2TTL,所以在接線時非常方便,只要使用傳輸線連接電腦和這個開發板的 CH343 接口即可。
提醒您:
如果您還沒安裝 CH343x 驅動程式,請您先安裝好。
如果您還沒安裝 CH343x 驅動程式,請您先安裝好。
程式
如何在 Arduino IDE 裡使用 ESP 系列的開發板我們就不在此重複說明,如尚未明瞭的人可詳
如果已安裝 ESP32 系列開發板,但列表中沒有 ESP32-C6 Dev Moudle 的項目,您可以到「開發板管理員」內做一次更新。
Step1 開啟 Arduino IDE,如下圖開啟 [File] > [Examples] > [ESP32] > [GPIO] > [BlinkRGB.ino]。
程式不是很長,所以貼在下方。
/*
BlinkRGB
Demonstrates usage of onboard RGB LED on some ESP dev boards.
Calling digitalWrite(RGB_BUILTIN, HIGH) will use hidden RGB driver.
RGBLedWrite demonstrates control of each channel:
void rgbLedWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue_val)
WARNING: After using digitalWrite to drive RGB LED it will be impossible to drive the same pin
with normal HIGH/LOW level
*/
//#define RGB_BRIGHTNESS 64 // Change white brightness (max 255)
// the setup function runs once when you press reset or power the board
void setup() {
// No need to initialize the RGB LED
}
// the loop function runs over and over again forever
void loop() {
#ifdef RGB_BUILTIN
digitalWrite(RGB_BUILTIN, HIGH); // Turn the RGB LED white
delay(1000);
digitalWrite(RGB_BUILTIN, LOW); // Turn the RGB LED off
delay(1000);
rgbLedWrite(RGB_BUILTIN, RGB_BRIGHTNESS, 0, 0); // Red
delay(1000);
rgbLedWrite(RGB_BUILTIN, 0, RGB_BRIGHTNESS, 0); // Green
delay(1000);
rgbLedWrite(RGB_BUILTIN, 0, 0, RGB_BRIGHTNESS); // Blue
delay(1000);
rgbLedWrite(RGB_BUILTIN, 0, 0, 0); // Off / black
delay(1000);
#endif
}
Step2 點擊下拉功能表 [工具] > [開發板],選擇 ESP32-C6 Dev Module。
Step3 點擊下拉功能表 [工具] > [序列埠]。我的是 COM25,請您選擇自己的序列埠。
Step4 點擊上傳程式。在顯示"上傳完畢"後,您可以發現開發板上的 LED 開始變換顏色,如下影片。




沒有留言:
張貼留言