LEDC PWM លើ Core 3.x: ledcAttach() និង ledcWrite()
ស្គាល់ Peripheral LEDC, ការផ្លាស់ប្ដូរ API ពី Core 2.x ទៅ 3.x, ទំនាក់ទំនងប្រេកង់/Resolution និងបង្កើតសំឡេងដោយ ledcWriteTone()។
- ប្រើ ledcAttach()/ledcWrite()/ledcWriteTone() និងជ្រើសប្រេកង់ និង Resolution ឱ្យត្រូវគ្នា
- អាន ADC 12-bit លើ ADC1 ដោយ analogReadMilliVolts() និង Averaging ហើយពន្យល់ហេតុអ្វី ADC2 ប្រើមិនបានជាមួយ Wi-Fi
- បង្កើតប៊ូតុង Touch ដោយ touchRead() ជាមួយ Baseline Calibration និង Hysteresis
ESP32 មាន Peripheral LEDC (LED Control) ដែលមាន 16 Channel ហើយអាចភ្ជាប់ PWM ទៅ GPIO Output ណាមួយក៏បាន តាមរយៈ GPIO Matrix — មិនដូច Uno ដែល PWM មានតែលើជើង ~ ប៉ុណ្ណោះ។ អ្នកកំណត់ទាំងប្រេកង់ (Hz) និង Resolution (bits) ដោយខ្លួនឯង។ Core ក៏មាន analogWrite() ដែលប្រើ LEDC នៅពីក្រោយ ប៉ុន្តែ LEDC ផ្ទាល់ឱ្យការគ្រប់គ្រងប្រេកង់ និង Resolution ច្បាស់លាស់ជាង។
ការផ្លាស់ប្ដូរ Core 3.x៖ API ចាស់ (2.x) ផ្អែកលើ Channel៖ ledcSetup(channel, freq, bits) + ledcAttachPin(pin, channel) + ledcWrite(channel, duty)។ ក្នុង Core 3.x អនុគមន៍ទាំងនោះត្រូវបានដកចេញ ហើយជំនួសដោយ API ផ្អែកលើ Pin៖ ledcAttach(pin, freq, bits) (ជ្រើស Channel ទំនេរដោយស្វ័យប្រវត្តិ ហើយត្រឡប់ true/false) និង ledcWrite(pin, duty)។ បើត្រូវការកំណត់ Channel ជាក់លាក់ ប្រើ ledcAttachChannel(pin, freq, bits, channel)។ ឃើញ Error 'ledcSetup' was not declared in this scope មានន័យថាកូដនោះសរសេរសម្រាប់ Core 2.x។
ប្រេកង់ និង Resolution ទាក់ទងគ្នា៖ Timer LEDC ដើរពី Clock 80 MHz ដូច្នេះ f × 2bits ≤ 80 MHz (ប្រហែល)។ ឧ. 5 kHz ប្រើបានដល់ 13-bit (5000 × 8192 ≈ 41 MHz) ប៉ុន្តែ 14-bit (5000 × 16384 ≈ 82 MHz) លើស។ សម្រាប់ LED ប្រើ 5 kHz / 10-bit (duty 0–1023); សម្រាប់ Servo ប្រើ 50 Hz / 16-bit ដែល Pulse 1.5 ms ស្មើ duty 1.5/20 × 65535 ≈ 4915។
សម្រាប់ Passive Buzzer ប្រើ ledcWriteTone(pin, freqHz) ដែលប្ដូរប្រេកង់ ហើយកំណត់ Duty 50% (ជើងត្រូវ Attach រួចសិន) ហើយបិទសំឡេងដោយ ledcWrite(pin, 0)។ Active Buzzer មិនត្រូវការ PWM ទេ — គ្រាន់តែ digitalWrite()។ ចំណាំ៖ Channel ជាគូ (0/1, 2/3, ...) ចែករំលែក Timer តែមួយ ដូច្នេះជើងពីរដែលត្រូវការប្រេកង់ខុសគ្នាក្នុងពេលតែមួយ គួរនៅលើ Channel នៃ Timer ផ្សេងគ្នា — កូដខាងក្រោមដាក់ Buzzer លើ Channel 2 ដោយ ledcAttachChannel() ដើម្បីកុំឱ្យ ledcWriteTone() ប៉ះពាល់ប្រេកង់ LED។
- Core 3.x៖ ledcAttach(pin, freq, bits) + ledcWrite(pin, duty) ជំនួស ledcSetup/ledcAttachPin
- ពិនិត្យតម្លៃត្រឡប់របស់ ledcAttach() — false មានន័យថាប្រេកង់/Resolution មិនអាចធ្វើបាន ឬអស់ Channel
- f × 2^bits ≤ 80 MHz៖ LED 5 kHz/10-bit, Servo 50 Hz/16-bit
- ledcWriteTone() សម្រាប់ Passive Buzzer, digitalWrite() សម្រាប់ Active Buzzer
// ledc_fade_beep.ino - LEDC PWM with the Arduino-ESP32 core 3.x pin-based API
// LED + 220 ohm on GPIO18. Passive buzzer on GPIO19 (drive loud buzzers through an NPN transistor).
constexpr uint8_t PIN_LED = 18;
constexpr uint32_t LED_FREQ_HZ = 5000;
constexpr uint8_t LED_RES_BITS = 10; // duty range 0..1023
constexpr int32_t LED_MAX_DUTY = (1 << LED_RES_BITS) - 1;
constexpr int32_t FADE_STEP = 8;
constexpr uint32_t FADE_INTERVAL_MS = 5;
constexpr uint8_t PIN_BUZZER = 19;
constexpr uint8_t BUZZER_CHANNEL = 2; // channels 0/1 share a timer; use another timer for the buzzer
constexpr uint32_t BEEP_FREQ_HZ = 2000;
constexpr uint8_t BUZZER_RES_BITS = 10;
constexpr uint32_t BEEP_PERIOD_MS = 3000;
constexpr uint32_t BEEP_LENGTH_MS = 120;
int32_t ledDuty = 0;
int32_t fadeDirection = 1;
uint32_t lastFadeMs = 0;
bool beeping = false;
uint32_t lastBeepStartMs = 0;
void setup() {
Serial.begin(115200);
// Core 3.x: ledcAttach(pin, frequency, resolution) picks a free channel automatically.
if (!ledcAttach(PIN_LED, LED_FREQ_HZ, LED_RES_BITS)) {
Serial.println("ERROR: ledcAttach failed for the LED pin");
}
// ledcAttachChannel(pin, frequency, resolution, channel) when you need a specific channel.
if (!ledcAttachChannel(PIN_BUZZER, BEEP_FREQ_HZ, BUZZER_RES_BITS, BUZZER_CHANNEL)) {
Serial.println("ERROR: ledcAttachChannel failed for the buzzer pin");
}
ledcWrite(PIN_BUZZER, 0); // start silent
}
void updateFade(uint32_t now) {
if (now - lastFadeMs < FADE_INTERVAL_MS) return;
lastFadeMs = now;
ledDuty += fadeDirection * FADE_STEP;
if (ledDuty >= LED_MAX_DUTY) { ledDuty = LED_MAX_DUTY; fadeDirection = -1; }
if (ledDuty <= 0) { ledDuty = 0; fadeDirection = 1; }
ledcWrite(PIN_LED, ledDuty); // core 3.x: the first argument is the PIN, not a channel
}
void updateBeep(uint32_t now) {
if (!beeping && now - lastBeepStartMs >= BEEP_PERIOD_MS) {
ledcWriteTone(PIN_BUZZER, BEEP_FREQ_HZ); // 50 % duty square wave
beeping = true;
lastBeepStartMs = now;
} else if (beeping && now - lastBeepStartMs >= BEEP_LENGTH_MS) {
ledcWrite(PIN_BUZZER, 0); // duty 0 = silence
beeping = false;
}
}
void loop() {
const uint32_t now = millis();
updateFade(now);
updateBeep(now);
}ការពន្យល់៖ LED Fade ពី 0 ទៅ 1023 ហើយត្រឡប់វិញ (ប្រហែល 0.64 វិនាទីម្ខាង) ខណៈ Buzzer បន្លឺ 2 kHz រយៈពេល 120 ms រៀងរាល់ 3 វិនាទី — ទាំងពីរដំណើរការព្រមគ្នាដោយគ្មាន delay()។ ប្រៀបធៀប៖ កូដ 2.x នឹងសរសេរ ledcSetup(0, 5000, 10); ledcAttachPin(18, 0); ledcWrite(0, duty);
លំហាត់ ESP32 Core 3.x៖ កំណត់ LEDC PWM ដោយ ledcAttach()
ប្រើ API ថ្មីរបស់ Arduino-ESP32 Core 3.x៖ ហៅ ledcAttach(PIN_LED, 5000, 10) ក្នុង setup() រួចហៅ ledcWrite(PIN_LED, 512) ដើម្បីបើកពន្លឺ 50% (10-bit)។
សំណួរវាស់សមត្ថភាព (Knowledge Check)
ឆ្លើយគ្រប់សំណួរ រួចចុច «ពិនិត្យចម្លើយ»។ បើត្រឹមត្រូវ ≥ ៨០% មេរៀននេះនឹងត្រូវបានកត់ត្រាថាបានបញ្ចប់ស្វ័យប្រវត្តិ។
- Arduino-ESP32 — LEDC API docs.espressif.comledcAttach, ledcAttachChannel, ledcWrite, ledcWriteTone និង ledcDetach ក្នុង Core 3.x។
- Arduino-ESP32 — Migration from 2.x to 3.0 docs.espressif.comបញ្ជី API ដែលបានផ្លាស់ប្ដូរ រួមទាំង LEDC ពី Channel ទៅ Pin។
- ESP-IDF — LED Control (LEDC) docs.espressif.comពន្យល់ Timer, Channel និងទំនាក់ទំនងប្រេកង់ និង Duty Resolution។