Ugenz Academy/IoT Tutorial

Adc Dac And Pwm

12 min read2,274 words

Reading progress

Keep learning

ESP32 — ADC, DAC and PWM

This topic is very important because it explains how the ESP32 works with variable voltages, sensor readings, LED brightness, motor speed, and analog-like control.

We will cover:

  1. ADC
  2. ADC resolution
  3. Reading sensor values
  4. DAC
  5. PWM
  6. Duty cycle
  7. LED brightness
  8. Motor speed
  9. Servo control
  10. ADC vs DAC vs PWM
  11. Common mistakes

1. What is ADC?

ADC = Analog-to-Digital Converter

An ESP32 is a digital computer, but the physical world often gives us analog voltages.

For example:

LDR │ ↓ Variable voltage │ ↓ ESP32 ADC │ ↓ Digital number

The ADC converts the analog voltage into a digital value that the ESP32 program can understand.


2. Why Do We Need ADC?

Imagine an LDR sensor.

Its voltage can continuously change:

0V │ ├── 0.5V ├── 1.0V ├── 1.5V ├── 2.0V ├── 2.5V ├── 3.0V └── 3.3V

The ESP32 cannot directly process "1.73 V" as a normal digital HIGH/LOW signal.

ADC converts it into a number.

For a typical classic ESP32 ADC configuration:

0V → around 0 3.3V → around 4095

So:

Analog Voltage ↓ ADC ↓ Digital Number

3. ADC Resolution

The classic ESP32 commonly uses a 12-bit ADC.

12-bit means:

2¹² = 4096 levels

Therefore:

0 → 4095

There are 4096 possible values.

Visualized:

0 4095 │----------------------------------------│ Dark Bright Low voltage Higher voltage

The exact voltage-to-reading relationship depends on the ESP32 variant, attenuation/configuration, and hardware.


4. analogRead()

Basic syntax:

int value = analogRead(pin);

Example:

#define SENSOR_PIN 34 void setup() { Serial.begin(115200); } void loop() { int value = analogRead(SENSOR_PIN); Serial.println(value); delay(500); }

You might see:

245 512 1024 1850 2500 3200 4095

5. ADC Example with LDR

Suppose an LDR is connected to GPIO 34 through a suitable voltage-divider circuit.

#define LDR_PIN 34 void setup() { Serial.begin(115200); } void loop() { int lightValue = analogRead(LDR_PIN); Serial.print("Light: "); Serial.println(lightValue); delay(500); }

Now you can use that value for automation.

LDR ↓ ADC ↓ Light value ↓ if condition ↓ LED / Relay

6. ADC + Threshold

For example:

#define LDR_PIN 34 #define LED_PIN 2 void setup() { pinMode(LED_PIN, OUTPUT); } void loop() { int light = analogRead(LDR_PIN); if (light < 1500) { digitalWrite(LED_PIN, HIGH); } else { digitalWrite(LED_PIN, LOW); } }

Concept:

LDR ↓ analogRead() ↓ Light Value ↓ ┌─────┴─────┐ ↓ ↓ < 1500 >= 1500 ↓ ↓ LED ON LED OFF

The threshold 1500 is only an example. You should determine the appropriate threshold from your actual sensor readings.


7. Converting ADC Reading to Voltage

A simplified calculation is:

Voltage ≈ ADC value × 3.3 / 4095

For example, if:

ADC = 2048

Then approximately:

Voltage ≈ 2048 × 3.3 / 4095 ≈ 1.65V

Code:

#define ADC_PIN 34 void setup() { Serial.begin(115200); } void loop() { int adcValue = analogRead(ADC_PIN); float voltage = adcValue * 3.3 / 4095.0; Serial.print("ADC: "); Serial.print(adcValue); Serial.print(" Voltage: "); Serial.println(voltage); delay(1000); }

Important

This is a simplified calculation. Real ESP32 ADC measurements can have non-linearity and depend on attenuation, calibration, board design, and the particular ESP32 variant.

For accurate voltage measurement, calibration and an appropriate voltage-divider circuit may be necessary.


8. Never Apply High Voltage Directly

This is extremely important.

Do not connect:

5V → ESP32 ADC GPIO

or:

12V → ESP32 GPIO

directly.

For measuring a higher voltage, use a properly calculated voltage divider or suitable measurement circuit.

For example:

Higher Voltage │ Resistor │ ├──────── ESP32 ADC │ Resistor │ GND

The divider must reduce the voltage to a safe level for the ESP32 ADC input.


9. What is DAC?

DAC = Digital-to-Analog Converter

It does the opposite of ADC.

ADC: Analog → Digital DAC: Digital → Analog

Conceptually:

Sensor voltage ↓ ADC ↓ ESP32 number ↓ Program ↓ DAC ↓ Analog voltage

10. DAC on Classic ESP32

The original ESP32 has two commonly used DAC outputs:

GPIO 25 → DAC GPIO 26 → DAC

These are associated with:

DAC Channel 1 → GPIO 25 DAC Channel 2 → GPIO 26

This is specific to the original ESP32 family; other ESP32 variants can have different peripheral capabilities.


11. DAC Example

On Arduino-ESP32 environments that support dacWrite():

#define DAC_PIN 25 void setup() { } void loop() { dacWrite(DAC_PIN, 128); }

The DAC value is typically:

0 → 255

So:

0 → minimum 128 → approximately middle 255 → maximum

12. DAC Ramp Example

You can gradually increase the DAC output:

#define DAC_PIN 25 void setup() { } void loop() { for (int value = 0; value <= 255; value++) { dacWrite(DAC_PIN, value); delay(10); } }

Then gradually decrease:

for (int value = 255; value >= 0; value--) { dacWrite(DAC_PIN, value); delay(10); }

This creates a gradual analog output change.


13. What is PWM?

PWM = Pulse Width Modulation

PWM is extremely important in ESP32 projects.

It allows us to control things like:

  • LED brightness
  • Motor speed
  • Buzzer tones
  • Servo control signals
  • Power delivered to some loads through suitable drivers

PWM rapidly switches a digital output:

HIGH ────┐ ┌────┐ ┌──── │ │ │ │ LOW └────┘ └────┘

The trick is to control how long the signal stays HIGH.


14. Duty Cycle

Duty cycle is the percentage of time the PWM signal is HIGH during one cycle.

0%

________________

Output effectively OFF.

50%

████____████____

HIGH for half the time.

100%

████████████████

Always HIGH.


15. PWM Values

For example, suppose we use 8-bit PWM:

0 → 255

Then:

0 → 0% 128 → ~50% 255 → 100%

So we can control an LED:

PWM 0 → OFF PWM 50 → Dim PWM 128 → Medium PWM 255 → Full brightness

16. LED Brightness with PWM

A PWM-capable GPIO can be connected to an LED through an appropriate resistor.

Conceptually:

ESP32 GPIO │ PWM │ Resistor │ LED │ GND

On current Arduino-ESP32 versions, you can use the LEDC PWM API.

Example:

#define LED_PIN 2 void setup() { ledcAttach(LED_PIN, 5000, 8); } void loop() { ledcWrite(LED_PIN, 128); }

Here:

Frequency = 5000 Hz Resolution = 8-bit Duty = 128

So the LED is approximately half brightness.


17. LED Fading

Now let's make the LED gradually brighter and darker.

#define LED_PIN 2 void setup() { ledcAttach(LED_PIN, 5000, 8); } void loop() { for (int brightness = 0; brightness <= 255; brightness++) { ledcWrite(LED_PIN, brightness); delay(10); } for (int brightness = 255; brightness >= 0; brightness--) { ledcWrite(LED_PIN, brightness); delay(10); } }

The result:

OFF ↓ Dim ↓ Medium ↓ Bright ↓ Maximum ↓ Medium ↓ Dim ↓ OFF

18. PWM Frequency

PWM has two important properties:

Frequency Duty Cycle

Frequency tells us how quickly the PWM waveform repeats.

For example:

500 Hz 1000 Hz 5000 Hz

Duty cycle controls the percentage of time the signal is HIGH.

Frequency → How fast it repeats Duty → How much of each cycle is HIGH

19. PWM for Motor Speed

This is very useful for your ESP32 + L298N projects.

Don't connect a motor directly to an ESP32 GPIO.

Instead:

ESP32 │ │ PWM/control ↓ Motor Driver │ ↓ Motor

For an L298N:

ESP32 PWM ↓ ENA / ENB ↓ L298N ↓ Motor

The PWM duty cycle can control the motor driver's effective motor power.

Example concept:

PWM 50 ↓ Slow PWM 150 ↓ Medium PWM 255 ↓ Fast

Actual motor speed depends on the motor, load, supply voltage, driver losses, and PWM frequency.


20. PWM + L298N Example

For example:

#define ENA 18 #define IN1 19 #define IN2 21 void setup() { pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); ledcAttach(ENA, 5000, 8); } void loop() { // Forward digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); // Speed ledcWrite(ENA, 180); }

Here:

IN1 + IN2 ↓ Direction ENA ↓ PWM ↓ Speed

The exact GPIO mapping depends on your wiring.


21. PWM vs DAC

This is a very important difference.

DAC

Produces an actual analog voltage from a DAC peripheral.

Digital value ↓ DAC ↓ Analog voltage

PWM

Produces a rapidly switching digital waveform.

Digital value ↓ PWM ↓ HIGH/LOW pulses

For an LED, PWM looks like brightness control because the switching is fast enough for our eyes to perceive an average brightness.


22. ADC vs DAC vs PWM

FeatureADCDACPWM
Full nameAnalog to Digital ConverterDigital to Analog ConverterPulse Width Modulation
DirectionAnalog → DigitalDigital → AnalogDigital pulses
Main useSensor readingAnalog outputControl
ExampleLDRAnalog waveformLED brightness
Common value0–40950–255Depends on resolution
Common projectTemperature/light measurementAnalog outputMotor/LED control

23. Real Smart-Home Example

Imagine your smart room:

ESP32 │ ┌─────────┼─────────┐ │ │ │ ADC PWM Digital │ │ │ ↓ ↓ ↓ LDR LED PIR │ │ │ │ │ │ Light level Brightness Motion

The program could do:

PIR detects person ↓ Check LDR ↓ Room is dark ↓ Turn LED ON ↓ Use PWM ↓ Set brightness to 70%

That is already a basic real-world IoT automation system.


24. ADC + PWM Together

You can use one analog sensor to control an LED.

For example:

LDR ↓ ADC ↓ 0–4095 ↓ Convert to 0–255 ↓ PWM ↓ LED brightness

Code:

#define LDR_PIN 34 #define LED_PIN 2 void setup() { Serial.begin(115200); ledcAttach(LED_PIN, 5000, 8); } void loop() { int sensorValue = analogRead(LDR_PIN); int brightness = map(sensorValue, 0, 4095, 0, 255); ledcWrite(LED_PIN, brightness); Serial.print("Sensor: "); Serial.print(sensorValue); Serial.print(" Brightness: "); Serial.println(brightness); delay(20); }

25. What is map()?

map() converts a value from one range to another.

Syntax:

map(value, fromLow, fromHigh, toLow, toHigh);

Example:

int brightness = map(sensorValue, 0, 4095, 0, 255);

Meaning:

0 → 0 1024 → ~64 2048 → ~128 3072 → ~191 4095 → 255

So:

ADC range 0 ───────────── 4095 ↓ map() ↓ PWM range 0 ───────────── 255

26. PWM Resolution

PWM resolution determines how many duty-cycle levels are available.

For 8-bit:

2⁸ = 256 levels 0–255

For 10-bit:

2¹⁰ = 1024 levels 0–1023

For 12-bit:

2¹² = 4096 levels 0–4095

Higher resolution gives finer control, though the available resolution also depends on PWM frequency and the ESP32 hardware/peripheral configuration.


27. PWM for Servo Motors

A servo does not simply use "brightness-style" PWM.

Servo control generally uses a periodic pulse signal where the pulse width represents the desired position.

Conceptually:

Servo signal |‾‾|____________|‾‾|____________ ↑ Pulse width

Different pulse widths correspond to different servo positions.

For example, approximately:

Short pulse → 0° Middle pulse → 90° Long pulse → 180°

The exact pulse widths and supported range depend on the servo.

For ESP32 projects, a servo library can handle the required signal generation.


28. Important Safety Rule

Do not power these directly from ESP32 GPIO:

❌ DC Motor ❌ Large Fan ❌ Pump ❌ High-power LED ❌ Relay coil without appropriate interface

Use:

ESP32 ↓ Driver / Relay / MOSFET ↓ External Power Supply ↓ Load

For example:

ESP32 ──→ L298N ──→ Motor ↑ External Supply

29. Common Mistakes

Mistake 1 — Sending 5V to ESP32 ADC

❌ Don't directly connect 5V.

Use a suitable voltage divider or level-shifting/measurement circuit.


Mistake 2 — Connecting motor directly to GPIO

❌ Don't do:

GPIO → Motor

Use a motor driver.


Mistake 3 — Confusing ADC and PWM

Remember:

ADC = READ analog information PWM = CONTROL using pulses

Mistake 4 — Assuming every ESP32 has the same peripherals

ESP32 family members differ.

For example:

ESP32 ESP32-S2 ESP32-S3 ESP32-C3 ESP32-C6

can have different ADC, DAC, PWM, GPIO, and peripheral capabilities.

Always check your exact board/chip when using a special peripheral.


30. The Complete Concept

PHYSICAL WORLD │ ┌─────────┴─────────┐ ↓ ↑ ANALOG CONTROL │ │ ↓ │ ADC PWM │ │ ↓ │ ESP32 DATA LED / MOTOR │ ↓ PROGRAM │ ↓ DAC │ ↓ ANALOG OUTPUT

🧠 Quick Revision

ADC

analogRead(pin);

Analog → Digital

Used for:

LDR Potentiometer Analog sensors Voltage measurement

DAC

dacWrite(pin, value);

Digital → Analog

On classic ESP32, commonly:

GPIO 25 GPIO 26

PWM

ledcAttach(pin, frequency, resolution); ledcWrite(pin, duty);

Used for:

LED brightness Motor speed Buzzer/control signals Servo signaling

The easiest way to remember

ADC → Read analog DAC → Create analog PWM → Control with pulses

Keep learning. Keep building. Keep growing.

© 2026 Ugenz Academy