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.
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:
intvalue=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.
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 defaultintmv=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:
longsum=0;for(inti=0;i<16;i++)sum+=analogRead(POT_PIN);intvalue=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).
intx=analogRead(JOYSTICK_X);// 0 at one end stop → 4095 at the otherinty=analogRead(JOYSTICK_Y);// the direction depends on how the module is mountedboolbtn=!digitalRead(JOYSTICK_BTN);// internal pull-up → LOW = pressed
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.
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:
Calibrate: at start-up, with the stick released, measure the real centre (average of 16 readings) instead of assuming 2048.
Dead zone: an offset from the centre smaller than ~300 returns 0.
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.
floatnormalize(intraw,intcenter){intoffset=raw-center;if(abs(offset)<300)return0.0f;// dead zoneinttravel=(offset>0)?(4095-center):center;returnconstrain((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:
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:
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.
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 resolutionanalogWrite(LED_PIN,128);// ~50% → half brightnessanalogWrite(LED_PIN,0);// offanalogWrite(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
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.