ESP32 essentials · Lesson
12 min read
ESP32 ADC and LEDC PWM
12-bit readings (0–4095) and flexible PWM with ledcAttach().
What You Will Learn
- Read 0–4095 from an ADC1 pin
- Set up LEDC PWM
- Know the ADC's non-linearity
analogRead() on the ESP32 returns 0–4095 for 0–3.3 V. The real ADC isn't perfectly linear near 0 V and 3.3 V, so expect some error at the extremes (the simulator uses an ideal ADC).
PWM uses the LEDC peripheral. In ESP32 Arduino core 3.x: ledcAttach(pin, frequency, resolutionBits), then ledcWrite(pin, duty). Older core 2.x code uses ledcSetup(channel, …) and ledcAttachPin(pin, channel).
Arduino C++
ledcAttach(18, 5000, 8); // 5 kHz, 8-bit ledcWrite(18, 128); // 50 %
ESP32: knob to LED with LEDCesp32-devkit-v1
Note: The potentiometer is powered from 3V3 — never 5 V on an ESP32 analog pin.
Check Your Understanding
Test what you learned in this lesson