Welcome to ESP32 Core Board repository
7. ADC
ADC
ADC1. ADC Overview2. Core Concepts2.1 ADC Resolution and Reference Voltage2.2 analogRead() Internal Flow2.3 Input Precautions3. Hardware Dependencies4. Project Architecture and Flow5. Source Code5.1 Pin Macro Definition5.2 setup() — Initialization5.3 loop() — Sampling and Conversion6. Operation Flow6.1 Build and Flash6.2 Normal Behavior6.3 Verification Method7. Common Issues
1. ADC Overview
ADC (Analog-to-Digital Converter) converts analog voltage into digital values, allowing the MCU to sense the continuous physical world. This experiment reads analog voltage on GPIO4 and prints the ADC raw value (0~4095) and the calculated actual voltage via serial port. Simply use analogRead() to complete the entire process from sampling to quantization.
Typical Application Scenarios:
| Application Type | Example |
|---|---|
| Power Monitoring | Battery level detection, USB power voltage monitoring |
| Environmental Sensing | Photoresistor, NTC temperature sensor, soil moisture |
| Human-Computer Interaction | Potentiometer knob, joystick, touch slider |
| Industrial Measurement | Current sampling (shunt resistor), pressure sensor, liquid level detection |
2. Core Concepts
2.1 ADC Resolution and Reference Voltage
ESP32-S3 SAR ADC has 12-bit resolution:
Output Range: 0 ~ 4095 (2^12 - 1)Reference Voltage: 3.3V (Vref)
Voltage Conversion Formula:
xvoltage = (ADC_value / 4095) × 3.3VExample: ADC = 2047 → 2047/4095 × 3.3 ≈ 1.65V (half scale)
2.2 analogRead() Internal Flow
Select ADC1 controller and corresponding channel
Configure sampling time (default ~2.5μs)
Start SAR (Successive Approximation Register) ADC conversion
Wait for conversion to complete (blocking, ~tens of microseconds)
Return 12-bit result from 0~4095
2.3 Input Precautions
Input range 0 ~ 3.3V, exceeding will damage the GPIO
To measure higher voltages (such as 5V, 12V), an external resistor divider circuit is required
Floating ADC pins read random noise - this is normal, not a bug
3. Hardware Dependencies
| Pin | Peripheral | Configuration | Description |
|---|---|---|---|
| GPIO4 | Analog signal source | INPUT | ADC1 channel 3, input range 0~3.3V |
For testing, leave it floating to observe noise values, or use a Dupont wire to connect GPIO4 to 3.3V/GND to observe full scale/zero.


4. Project Architecture and Flow
xxxxxxxxxxsetup()├─ Serial.begin(115200) // Initialize serial port└─ pinMode(4, INPUT) // Set GPIO4 as analog inputloop()├─ adc_value = analogRead(4) // Read ADC value (0~4095)├─ voltage = adc_value × 3.3 / 4095 // Calculate voltage├─ Serial.printf(...) // Print results└─ delay(1000) // Sample once per second
5. Source Code
Complete source: Source Code → Arduino → 1.Basic Peripherals → 7.ADC → 7.ADC.ino
5.1 Pin Macro Definition
pinMode(ADC_PIN, INPUT) disables digital output driver and configures as high-impedance analog input.
xxxxxxxxxx5.2 setup() — Initialization
Only need to initialize serial port and pin mode. ADC peripheral is automatically configured by analogRead.
xxxxxxxxxxvoid setup() { Serial.begin(115200); pinMode(ADC_PIN, INPUT);}Arduino's
analogReadautomatically completes ADC clock, resolution, and reference voltage configuration on the first call, no manual initialization required.
5.3 loop() — Sampling and Conversion
Read the raw value first, then convert to voltage using the formula. The f suffix in 3.3f indicates float type, avoiding unnecessary double precision overhead.
xxxxxxxxxxvoid loop() { int adc_value = analogRead(ADC_PIN); float voltage = (adc_value * 3.3f) / 4095.0f;
Serial.printf("ADC Value: %4d | Voltage: %.2f V\n", adc_value, voltage); delay(1000);}
%4dis 4-digit right-aligned formatting for neat numeric column alignment.%.2fkeeps two decimal places.
6. Operation Flow
6.1 Build and Flash
Arduino flash process: See Arduino → 1. Before Development → 2. Build and Flash.
6.2 Normal Behavior
Open the Serial Monitor (115200 bps), one line printed per second:
xxxxxxxxxx========================================ESP32-S3 ADC Experiment Started========================================ADC Value: 841 | Voltage: 0.68 VADC Value: 237 | Voltage: 0.19 VADC Value: 233 | Voltage: 0.19 VADC Value: 377 | Voltage: 0.30 VADC Value: 157 | Voltage: 0.13 VADC Value: 987 | Voltage: 0.80 VADC Value: 0 | Voltage: 0.00 VADC Value: 0 | Voltage: 0.00 V

Connected to GND state:

Connected to 3V3 state:

Floating state:

6.3 Verification Method
| Operation | Expected Result |
|---|---|
| GPIO4 floating | Reads random noise value (normal) |
| GPIO4 connected to 3.3V | ADC ≈ 4095, Voltage ≈ 3.30V |
| GPIO4 connected to GND | ADC ≈ 0, Voltage ≈ 0.00V |
Change delay(1000) to delay(100) | 10 samples per second, faster value update |
7. Common Issues
| Symptom | Cause | Solution |
|---|---|---|
| Readings jump randomly when floating | ADC input is high impedance, pin acts like an antenna | Normal behavior, just connect a signal source |
| Large voltage deviation | Uncalibrated or reference voltage fluctuation | Measure actual Vref with a multimeter and substitute into formula |
| Readings always close to 0 or 4095 | Pin shorted to GND/VDD | Check wiring |