筆者在 ESP32 模塊上編寫了第二個 Arduino 程式,這是個 LED 燈閃爍程式,程式是非常簡單,setup{} 是入口函數,相當於 main 函數,首先設定 D2 (GPIO 27) 為輸出口,loop{} 是循環函數,循環函數是包括了驅動 D2 (GPIO 27) 高電平,便延遲 1秒(Delay 1000),再驅動 D2 (GPIO 27) 低電平,再延遲 1秒(Delay 1000),然後循環不停,便可以達到 LED 閃爍效果。
ESP32 Arduino LED 閃燈程式 |
開發環境:Arduino(阿爾杜伊諾)的 IDE (整合開發環境)v1.18.19 版本
原程式:C:\Development\Develop_ESP32_Arduino\Arduino_Project\DIY-Arduino-002-01 LED Blink 20240619
程式:C:\Development\Develop_ESP32_Arduino\Arduino_Project\DIY-Arduino-002-01 LED Blink 20240619
ESP32 Arduino LED 閃燈程式:
/* ESP 32 Blink Turns on an LED on for one second, then off
for one second, repeatedly. The ESP32 has an internal blue LED at D2
(GPIO 27) */ { pinMode(LED_BUILTIN, OUTPUT); } {
// put your main code here, to run
repeatedly: digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage
level) delay(1000); // wait for a second digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage
LOW delay(1000); // wait for a second } |
限流電阻 = 輸出口電壓 – LED 的正向電壓 / LED 的正向電流
ψ3mm LED 的額定電流為 1~10mA; (3.3-2.4)/10mA≒900Ω
ψ5mm LED 的額定電流為 5~25mA; (3.3-2.4)/20mA≒450Ω
ψ10mm LED 的額定電流為 25~100mA; (3.3-2.4)/100mA≒9Ω
ESP32 Arduino LED 閃燈 OFF |
ESP32 Arduino LED 閃燈 ON |
BugWorkShop - 甲蟲工作室: DIY - ESP8266:ESP8266 第一個程式 LED 閃燈(十四)