মূল বিষয়বস্তুতে যান
Learn/ESP32 essentials/ESP32 ADC and LEDC PWM
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
Open in Lab
ESP32-WROOM-32: The metal can holds the ESP32 chip, 4 MB flash and the 40 MHz crystal. The top part is the PCB antenna — keep it clear of metal.ESP32-WROOM-32Antenna: Printed Wi-Fi/Bluetooth antenna.Micro-USB: Upload sketches, open the Serial Monitor and power the board (5 V).USB-UART: CP2102 or CH340 chip that turns USB into the ESP32's serial port.EN: Reset button (pulls EN low).BOOT: Holds GPIO0 low; press it while uploading if the upload tool can't enter boot mode.LED (GPIO2): Built-in blue LED on GPIO2 on most DevKit V1 boards.3.3 V regulator: AMS1117 regulator: 5 V → 3.3 V.40%220 Ω+
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

1What is the maximum analogRead() value on an ESP32 at default settings?