Arduino IDE 跳出「有可用的更新」,順手把 esp32 開發板套件從 2.x 更新上去,再按一次上傳,舊程式整排紅字。
ESP32 的 Arduino 核心從 3.0 開始,配合底層 ESP-IDF 5 的新驅動,把 LEDC(PWM)和硬體計時器的 API 改掉了,舊函式直接移除,沒有相容層。2026 年 11 月釋出的 4.0.0 又改了一些細節。
舊程式
一支很常見的寫法:LEDC 做呼吸燈,硬體計時器每秒觸發一次中斷。
const int LED_PIN = 2;
const int LEDC_CHANNEL = 0;
hw_timer_t *timer = nullptr;
volatile uint32_t ticks = 0;
void IRAM_ATTR onTimer() {
ticks++;
}
void setup() {
Serial.begin(115200);
ledcSetup(LEDC_CHANNEL, 5000, 8); // 通道 0、5 kHz、8 位元
ledcAttachPin(LED_PIN, LEDC_CHANNEL); // 把腳位接到通道
timer = timerBegin(0, 80, true); // 計時器 0、除頻 80(1 MHz)、往上數
timerAttachInterrupt(timer, &onTimer, true);
timerAlarmWrite(timer, 1000000, true); // 1,000,000 次 = 1 秒、自動重載
timerAlarmEnable(timer);
}
void loop() {
for (int duty = 0; duty 0
ledcWrite(2, 128) -> 1
傳通道號碼 0 進去,函式回傳 false,什麼都沒做,LED 就是不亮。很少人會檢查 ledcWrite 的回傳值,所以這種錯誤只會表現成「燈不會動」。改程式時要把每一個 ledcWrite、ledcRead 的第一個參數都確認過。
真的需要指定通道時,3.x 另外有 ledcAttachChannel(pin, freq, res, channel)。
計時器的改法
| 2.x | 3.x、4.x |
|---|---|
timerBegin(num, divider, countUp) |
timerBegin(frequency) |
timerAttachInterrupt(t, fn, edge) |
timerAttachInterrupt(t, fn) |
timerAlarmWrite(t, value, autoreload) + timerAlarmEnable(t) |
timerAlarm(t, value, autoreload, reload_count) |
timerBegin 不再指定計時器編號和除頻值,直接給計時器的頻率,除頻由核心自己算。原本「80 MHz 除 80」得到 1 MHz,現在直接寫 timerBegin(1000000)。
timerAlarm 的最後一個參數是自動重載的次數,0 代表不限次數。
改好的程式
const int LED_PIN = 2;
const int LEDC_RESOLUTION = 8;
#if ESP_ARDUINO_VERSION_MAJOR >= 4
const uint32_t LEDC_FULL = 1u << LEDC_RESOLUTION; // 4.x:256 才是全亮
#else
const uint32_t LEDC_FULL = (1u << LEDC_RESOLUTION) - 1; // 3.x:255 是全亮
#endif
hw_timer_t *timer = nullptr;
volatile uint32_t ticks = 0;
void IRAM_ATTR onTimer() {
ticks++;
}
void setup() {
Serial.begin(115200);
ledcAttach(LED_PIN, 5000, LEDC_RESOLUTION); // 頻率、解析度一次設定,通道自動分配
timer = timerBegin(1000000); // 直接給計時器頻率:1 MHz
timerAttachInterrupt(timer, &onTimer); // 不再有 edge 參數
timerAlarm(timer, 1000000, true, 0); // 1 秒、自動重載、不限次數
}
void loop() {
for (uint32_t duty = 0; duty <= LEDC_FULL; duty++) {
ledcWrite(LED_PIN, duty); // 寫的是「腳位」
delay(4);
}
Serial.printf("ticks=%u\n", ticks);
}
3.3.13:Sketch uses 289736 bytes (22%) of program storage space.
4.0.0: Sketch uses 294431 bytes (22%) of program storage space.
用 4.0.0 編譯,在 Espressif 的 QEMU 模擬器(esp32s3 機型)上執行,計時器中斷每秒觸發一次:
ticks=1
ticks=2
ticks=3
ticks=4
ticks=5
ticks=7
5 跳到 7 是因為 loop() 跑一輪呼吸燈要 257 × 4 ms,略超過 1 秒,不是漏了中斷。
反過來,新程式拿到 2.0.17 編譯也不會過:
new_sketch.ino:20:3: error: 'ledcAttach' was not declared in this scope
new_sketch.ino:22:29: error: too few arguments to function 'hw_timer_t* timerBegin(uint8_t, uint16_t, bool)'
new_sketch.ino:23:39: error: too few arguments to function 'void timerAttachInterrupt(hw_timer_t*, void (*)(), bool)'
new_sketch.ino:24:3: error: 'timerAlarm' was not declared in this scope
程式要同時支援新舊核心,就得用 ESP_ARDUINO_VERSION_MAJOR 包兩套寫法。自己的專案通常直接改成新寫法比較單純。
4.0 的 PWM 全亮值
上面程式裡的 LEDC_FULL 是 4.0 的改動。依照 Espressif 官方的 3.x 到 4.0 遷移指南,ledcWrite()、ledcFade*() 的合法範圍變成 0 到 2^解析度(含)。8 位元解析度時,全亮從 255 變成 256。
舊程式寫 ledcWrite(pin, 255),在 4.0 不會報錯,只是輸出差一格,沒有真正全亮。這個改動編譯器抓不到,要自己搜尋 255、4095、(1 << res) - 1 這類寫法。analogWrite() 維持原本的 0 到 255,不受影響。
這一段的行為依官方文件整理,我只有驗證程式能編譯執行,沒有用示波器量輸出波形。
4.0 其他要注意的
同一份遷移指南裡,比較可能影響一般專案的:
HTTPClient連 HTTPS 時,沒有設定憑證就不再略過驗證,而是連線失敗。要提供 CA 憑證、呼叫useBuiltinCACertBundle(),或明確呼叫setInsecure()回到舊行為。- BLE 函式庫改寫成新的 API,舊的 BLE 程式大多要改。
- 編譯器換成 GCC 15,
.c檔預設 C23,bool、true、false變成保留字,舊的 C 程式自己定義bool會編譯失敗。
先不升級也可以
專案還沒時間改,可以把核心固定在舊版。Arduino IDE 的開發板管理員可以選版本安裝;用 arduino-cli 的話:
arduino-cli core install esp32:esp32@2.0.17
只是 2.x 已經不會再有新的修正,新的晶片(例如 ESP32-C6、ESP32-P4)也需要 3.x 以上。舊專案還是建議找時間改完,往後才跟得上。
發表留言