ADC & PWM

Understand the ADC (analog-to-digital conversion) and PWM (pulse width modulation) on the ESP32-S3.

Difficulty :

Prerequisites:

Documentation completed: 90% 90
Created by:  Alban Petit

Beyond all-or-nothing

The previous concept, GPIO, only knew two values: on or off. But the real world is full of nuances: a joystick is not just “left” or “right”, it can be pushed more or less far; an LED can shine at half power; a motor can turn slowly.

To handle these intermediate values, the microcontroller has two tools, both on the menu of this concept: the ADC to read a quantity that varies continuously, and PWM to simulate one as an output.

By the end of this concept, you will know how to:

  • convert an analog voltage into a number with the ADC;
  • simulate a variable output voltage with PWM;
  • scale a reading with map().

From continuous to digital: the ADC

The physical world is analog: a temperature, a joystick position, a brightness produce voltages that vary continuously between 0 V and 3.3 V. The microcontroller, on the other hand, only understands integers.

The analog-to-digital converter (ADC) turns this continuous voltage into an integer.

Quantisation: a continuous voltage from 0 to 3.3 V is converted into a staircase value; here 8 steps for readability, 4096 on the ESP32-S3

Resolution: 12 bits on the ESP32-S3

The ESP32-S3’s ADC has a resolution of 12 bits, which gives $2^{12} = 4096$ possible values:

$$ \text{value} = \frac{V_{in}}{V_{ref}} \times (2^{12} - 1) = \frac{V_{in}}{3.3} \times 4095 $$
Voltage ADC value
0 V 0
1.65 V ≈ 2048
3.3 V 4095
int value = analogRead(PIN_JOYSTICK_X);  // returns 0 to 4095
  The ADC is not perfectly linear

The formula above is a teaching approximation. In reality, the ESP32’s ADC is slightly non-linear (especially near 0 V and 3.3 V) and its full scale depends on the configured attenuation. For an accurate measurement the ADC is calibrated; to read a joystick, this approximation is more than enough.

Curve of the real ADC compared with the ideal ADC: the value stays at 0 below about 0.1 V and saturates at 4095 from about 3.1 V

The measuring range: attenuation

To cover a range close to 0–3.3 V, Arduino-ESP32 applies the maximum attenuation (11 dB) to analogRead() by default: this is why it “works” without any setting. On the ESP32-S3, the range that can actually be measured then goes from about 0 to 3.1 V; beyond that, the reading saturates at 4095. This behaviour can be adjusted and, above all, you can directly get a value in millivolts that is already calibrated:

analogSetAttenuation(ADC_11db);           // extended range (~0–3.1 V), the default
int mv = analogReadMilliVolts(POT_PIN);   // voltage in mV, factory-corrected

analogReadMilliVolts() is more reliable than a manual conversion with the formula above: it takes into account the calibration specific to each chip.

Sampling

The ADC does not read the signal continuously: it takes samples at regular intervals. Between two readings, whatever the signal does is ignored. This is enough for a joystick (slow variation), but not for a high-frequency audio signal.

Smoothing a noisy reading

A single ADC reading “jitters” by a few units because of electrical noise. For a stable value, average several readings:

long sum = 0;
for (int i = 0; i < 16; i++) sum += analogRead(POT_PIN);
int value = sum / 16;   // average of 16 readings

The more you average, the more stable the value, but the longer the reading takes. 8 to 16 samples are a good compromise for a potentiometer or a joystick.

The ADC2 + Wi-Fi trap on the ESP32

The ESP32 has two ADC banks:

Bank Pins Note
ADC1 GPIO 1–10 Always available ✅
ADC2 GPIO 11–20 Unusable when Wi-Fi is active ⚠️

On the course’s XIAO ESP32S3, all the exposed analog pins (D0 to D5 and D8 to D10, i.e. GPIO1 to GPIO9) are on ADC1: the trap cannot happen with this board, but it lies in wait as soon as you move to another ESP32 board.

  Always use ADC1 for the joystick

In this project, Wi-Fi is used for multiplayer. Connecting the joystick to ADC2 pins (GPIO 11–20) will give erratic readings or lock up the ADC. You must use ADC1 pins (GPIO 1–10): this is the case for D0 and D1 on the XIAO.

Reading a joystick

An analog joystick contains two potentiometers (adjustable resistors that deliver a voltage depending on their position; one per axis, X and Y) and a button (pressing the stick).

int x = analogRead(JOYSTICK_X);  // 0 at one end stop → 4095 at the other
int y = analogRead(JOYSTICK_Y);  // the direction depends on how the module is mounted
bool btn = !digitalRead(JOYSTICK_BTN);  // internal pull-up → LOW = pressed

Detecting directions with thresholds:

if (x < 1500)       direction = LEFT;
else if (x > 2500)  direction = RIGHT;
else if (y < 1500)  direction = DOWN;
else if (y > 2500)  direction = UP;
else                direction = CENTER;

The 1500-2500 range is a dead zone: at rest, the value is never exactly 2048 and jitters by a few units, and these small variations must not count as a movement.

The map of joystick values, from 0 to 4095 on each axis: LEFT below 1500 and RIGHT above 2500 over the full height, UP and DOWN in the central column, CENTER in the dead zone square

Calibrating and normalising

For a game, you often want more than a direction: how far the stick is pushed. The reading is then converted into a value between -1 (low end stop) and +1 (high end stop), which is 0 at rest:

  1. Calibrate: at start-up, with the stick released, measure the real centre (average of 16 readings) instead of assuming 2048.
  2. Dead zone: an offset from the centre smaller than ~300 returns 0.
  3. Normalise: divide the offset from the centre by the travel available on the side where the stick is (4095 - center on one side, center on the other), so that each end stop really gives -1 or +1.
float normalize(int raw, int center) {
  int offset = raw - center;
  if (abs(offset) < 300) return 0.0f;                   // dead zone
  int travel = (offset > 0) ? (4095 - center) : center;
  return constrain((float)offset / travel, -1.0f, 1.0f);
}

A paddle can then move at a speed proportional to this value, and directions can be derived with readable thresholds (x < -0.5 for LEFT), whatever the joystick’s real centre.

Scaling a reading with map()

An ADC reading (0–4095) often needs to be converted to another range: a servo angle (0–180°), a PWM value (0–255), a percentage… The map() function does this rule of three in one go:

int reading = analogRead(POT_PIN);            // 0 – 4095
int angle   = map(reading, 0, 4095, 0, 180);  // 0 – 180°
int pwm     = map(reading, 0, 4095, 0, 255);  // 0 – 255

map() matches the range 0 to 4095 to the range 0 to 255: 0 gives 0, 2048 gives 127, 4095 gives 255

map(value, in_min, in_max, out_min, out_max) assumes a linear relationship and does not clamp the result: if the reading can go beyond the expected range, bound it with constrain(pwm, 0, 255).

From digital to analog: PWM

A digital GPIO can only produce 0 V or 3.3 V. To simulate an intermediate voltage (set the brightness of an LED, the speed of a motor, the position of a servo), we use PWM (Pulse Width Modulation).

Principle

PWM switches rapidly between HIGH and LOW at high frequency. The proportion of time spent HIGH is called the duty cycle:

Three PWM signals with the same frequency, at 25, 50 and 75 % duty cycle, with their average voltage of 0.8 V, 1.65 V and 2.5 V

Duty cycle Perceived effect
0 % Always off
50 % Half brightness
100 % Full brightness

The frequency is high enough (typically 1–50 kHz) for the eye (or a motor) not to perceive the blinking, only the average value.

$$ V_{avg} = V_{max} \times \frac{\text{duty cycle}}{100} $$

PWM demonstrator

Average voltage: 1.65 V out of 3.3 V  ·  analogWrite: 128 / 255

PWM on the ESP32-S3 with Arduino

// Value from 0 (0%) to 255 (100%) at 8-bit resolution
analogWrite(LED_PIN, 128);   // ~50% → half brightness
analogWrite(LED_PIN, 0);     // off
analogWrite(LED_PIN, 255);   // full brightness

The ESP32-S3 has 8 independent LEDC channels (LED Control, the ESP32’s hardware PWM generator), with configurable frequency and resolution (up to 14 bits on the ESP32-S3). analogWrite() uses the LEDC layer of Arduino-ESP32 behind the scenes.

Applications of PWM

Application Principle
Dimmable LED Duty cycle ∝ brightness
Servo motor 1–2 ms pulse every 20 ms (angular position)
DC motor Duty cycle ∝ speed
Buzzer Audio frequency → tone

Driving a servo motor: one pulse every 20 ms, 1 ms for 0°, 1.5 ms for 90° and 2 ms for 180°

Quick quiz

1. What does analogRead() return at 12-bit resolution?

Show the answerAn integer from 0 to 4095 (i.e. 2¹² = 4096 values).

2. How do you convert a 0–4095 reading into a 0–180° angle?

Show the answerWith `map(reading, 0, 4095, 0, 180)`, and `constrain()` if the reading can go out of range.

3. Does PWM produce a real analog voltage?

Show the answerNo: it switches very quickly between 0 V and 3.3 V. It is the average value (set by the duty cycle) that simulates an intermediate voltage.

Going further

Summary

  • The ADC converts a voltage (0–3.3 V) into an integer (0–4095) on 12 bits.
  • ADC1 only (GPIO 1–10) when Wi-Fi is active: ADC2 is unusable.
  • A joystick = 2 potentiometers (ADC) + 1 button (GPIO).
  • map() converts a reading from one range to another (0–4095 → 0–180°, 0–255…); constrain() clamps it.
  • analogReadMilliVolts() gives a calibrated voltage, more reliable than the theoretical formula.
  • Averaging 8 to 16 readings smooths the noise of a “jittery” ADC.
  • PWM simulates a variable voltage by varying the HIGH/LOW ratio.
  • analogWrite(pin, 0–255) to set brightness, speed or position.
Need help?