yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
ESP32 Core Board📖
1. ESP32 Introduction
  • 1. ESP32S3 Overview
  • 2. Environment Setup Introduction
  • 3. Dev Board Introduction
2. ESP-IDF Development Tutorial
1. Before Development
  • 1. Environment Setup (Windows)
  • 2. Environment Setup (Linux)
  • 3. Build and Flash
2. Basic Peripherals
  • 1. LED Control
  • 2. Button Detection
  • 3. Peripheral Interrupts
  • 4. ESP_TIMER
  • 5. GPTIMER
  • 6. Watchdog Timer
  • 7. DMA
  • 8. ADC
  • 9. PWM Breathing LED
  • 10. RGB LED Effects
3. Serial Communication and Storage System
  • 1. Serial UART Communication Experiment
  • 2. I2C_OLED Display Experiment
  • 3. SPI Bus Communication Experiment
  • 4. PSRAM External Memory Experiment
  • 5. Flash Read and Write Experiment
  • 6. USB Emulated Mass Storage Experiment
4. FreeRTOS operating system
  • 1.FreeRTOS Core Concepts Theory
  • 2.Task Creation and Management
  • 3.Queue
  • 4.Binary Semaphore
  • 5.Mutex
  • 6.Event Groups
  • 7.Software Timer
  • 8.Task Scheduler
  • 9.Low Power Mode
  • 10.Deadlock
  • 11.Priority Inversion
5. Network Protocols and Wireless Communication
  • 1. WIFI Network Basics
  • 2. WIFI STA Mode
  • 3. WIFI AP Mode
  • 4. UDP Client Experiment
  • 5. UDP Server Experiment
  • 6. TCP Client Experiment
  • 7. TCP Server Experiment
  • 8. Socket Programming Basics
  • 9. HTTP Client Experiment
  • 10. HTTP Server Experiment
  • 11. MQTT IoT Communication Protocol
  • 12. WIFI OTA Upgrade
  • 13. Bluetooth Fundamentals
  • 14. Bluetooth Slave Mode
  • 15. Bluetooth Master Mode
6. Audio Applications
  • 1.I2S Microphone Recording and U-Disk Playback
  • 2.I2S Audio Playback Experiment
  • 3.Repeater Experiment
7. Xiaozhi AI Voice
  • 1. Burn Xiaozhi Firmware
  • 2. Network and Configuration
  • 3. AI Agent Settings
  • 4. Program Analysis
  • 5. Free Chat
3. Arduino Development Course
1. Before Development
  • 1. Environment Setup
  • 2. Build and Flash
2. Basic Peripherals
  • 1. LED Control
  • 2. Button Detection
  • 3. Peripheral Interrupts
  • 4. ESP_TIMER
  • 5. GPTIMER
  • 6. Watchdog Timer
  • 7. ADC
  • 8. PWM Breathing LED
  • 9. RGB LED Effects
3. Serial Communication and Storage System
  • 1. Serial UART Communication
  • 2. I2C OLED Display
  • 3. SPI Bus Communication
  • 4. Flash Read and Write
  • 5. USB Emulated Mass Storage
4. Network Protocols and Wireless Communication
  • 1. WIFI Network Basics
  • 2. WIFI STA Mode
  • 3. WIFI AP Mode
  • 4. UDP Server Experiment
  • 5. UDP Client Experiment
  • 6. TCP Client Experiment
  • 7. TCP Server Experiment
  • 8. HTTP Client Experiment
  • 9. HTTP Server Experiment
  • 10. MQTT IoT Communication Protocol
  • 11. WIFI OTA Upgrade
  • 12. Bluetooth Fundamentals
  • 13. Bluetooth Slave Mode
  • 14. Bluetooth Master Mode
5. Audio Applications
  • 1.I2S Microphone Recording and U-Disk Playback
  • 2.I2S Audio Playback Experiment
  • 3.Repeater Experiment
4. MicroPython Development Course
1. Before Development
  • 1. Environment Setup
  • 2. Build and Flash
  • 3. Thonny IDE
2. Basic Peripherals
  • 1. LED Control
  • 2. Button Detection
  • 3. Peripheral Interrupts
  • 4. TIMER_IT
  • 5. Watchdog Timer
  • 6. ADC
  • 7. PWM Breathing LED
  • 8. RGB LED Effects
3. Serial Communication and Storage System
  • 1. Serial UART Communication Experiment
  • 2. I2C_OLED Display Experiment
  • 3. SPI Bus Communication Experiment
  • 4. Flash Read and Write Experiment
  • 5. Print Storage
4. Network Protocols and Wireless Communication
  • 1. WIFI Network Basics
  • 2. WIFI STA Mode
  • 3. WIFI AP Mode
  • 4. UDP Client Experiment
  • 5. UDP Server Experiment
  • 6. TCP Client Experiment
  • 7. TCP Server Experiment
  • 8. HTTP Client Experiment
  • 9. HTTP Server Experiment
  • 10. MQTT
  • 11. Bluetooth Fundamentals
  • 12. Bluetooth Slave Mode
  • 13. Bluetooth Master Mode
5. Audio Applications
  • 1. I2S Audio Playback Experiment
  • 2. Repeater Experiment
Download
  • Code
  • Hardware_Info
  • 3D_Model_File
  • Software_Tools
  • VMware
  • VM_File
Download ZIP

Welcome to ESP32 Core Board repository

6. ADC

ADC


ADC1. ADC Overview2. Core Concepts2.1 How the SAR ADC Works2.2 ADC Attenuation2.3 ADC Reading and Voltage Conversion2.4 Averaging Filter2.5 Core API3. Hardware Dependencies4. Project Architecture and Flow5. Source Code Details5.1 Parameter Configuration and Initialization5.2 Averaged Sampling and Voltage Conversion6. Operation Procedure6.1 Flashing and Running6.2 Normal Phenomenon6.3 Verification Methods7. Common Problems


1. ADC Overview

ADC (Analog-to-Digital Converter) converts an analog voltage into a digital value. This experiment uses the SAR ADC of the ESP32-S3 to read the voltage on the GPIO4 pin, taking 16 consecutive samples per batch and averaging them, then printing the raw ADC value and the converted voltage (mV) via serial. Averaging filtering effectively suppresses the random noise of a single sample.

Typical application scenarios:

Application TypeExample
Sensor readingPotentiometers, photoresistors, thermistors, current sensors
Battery level detectionLithium battery voltage monitoring
Audio captureMicrophone analog signal (with DMA)
Touch detectionCapacitive touch (dedicated touch sensors)

2. Core Concepts

2.1 How the SAR ADC Works

The ESP32-S3 uses a SAR ADC (Successive Approximation Register type). The core idea is binary-search approximation: comparing the input voltage with the DAC output voltage bit by bit from the MSB to the LSB, with 12-bit resolution requiring 12 clock cycles to complete one conversion.

2.2 ADC Attenuation

By default, the ADC of the ESP32-S3 can only measure 0~1.1V. The range can be extended by setting the attenuator:

AttenuationADC ConstantRangeUse Case
0 dBADC.ATTN_0DB0 ~ 1.1VSmall signals
2.5 dBADC.ATTN_2_5DB0 ~ 1.5V—
6 dBADC.ATTN_6DB0 ~ 2.2V—
11 dBADC.ATTN_11DB0 ~ 3.3VGeneral (most commonly used)

Choose ATTN_11DB to cover the full 0~3.3V range, which suits most cases.

2.3 ADC Reading and Voltage Conversion

adc.read() returns a 12-bit raw value (0~4095). The conversion formula is:

Voltage formula without attenuation:

Vdata = (data / (2^bitwidth - 1)) × Vref

Voltage formula without attenuation

Voltage formula with attenuation:

xxxxxxxxxx
Vdata = (data / (2^bitwidth - 1)) × Vref / k

Voltage formula with attenuation

The effective precision of the ESP32-S3 ADC is about 11-12 bits; noise in the low bits is normal. This experiment averages 16 samples to reduce noise.

2.4 Averaging Filter

xxxxxxxxxx
sum_val = 0
for i in range(SAMPLE_COUNT):
    sum_val += adc.read()      # 16 consecutive samples
avg_raw = sum_val // SAMPLE_COUNT   # average

Averaging filtering is the simplest digital filter. Averaging 16 samples reduces the standard deviation of random noise to 1/4 of its original value.

2.5 Core API

​x
from machine import ADC, Pin

adc = ADC(Pin(4))            # create the ADC object
adc.atten(ADC.ATTN_11DB)     # set attenuation, range 0~3.3V
val = adc.read()              # read a 12-bit value (0~4095)

3. Hardware Dependencies

GPIOPeripheralModeDescription
4ADC1_CH3ADCAnalog input pin, connect the voltage to be measured

In this experiment, you can leave GPIO4 floating to observe the fluctuating reading, or connect an external potentiometer to verify the voltage change.


4. Project Architecture and Flow

xxxxxxxxxx
Script execution (single-file .py)
  │
  ├─ Parameter config: ADC_PIN=4, SAMPLE_COUNT=16, VREF=3300mV
  ├─ adc = ADC(Pin(4))                          // GPIO4 → ADC
  ├─ adc.atten(ADC.ATTN_11DB)                   // range 0~3.3V
  │
  └─ while True:                                // sampling loop (once per second)
       ├─ sum_val = 0                            // clear the accumulator
       ├─ for i in 0..15:                        // take 16 consecutive samples
       │    └─ sum_val += adc.read()             // accumulate the 12-bit values
       ├─ avg_raw = sum_val // 16               // take the average
       ├─ avg_mv = avg_raw * 3300 // 4095      // convert to mV
       ├─ print("ADC_Value=..., Voltage=...mV") // serial output
       └─ sleep_ms(1000)                         // 1s interval

5. Source Code Details

Full source code: 5.Source Code → MicroPython → 1.Basic Peripherals → 6.ADC → 6.ADC.py

5.1 Parameter Configuration and Initialization

xxxxxxxxxx
from machine import ADC, Pin  # ADC and Pin classes
import time                    # delay

# Configuration parameters
ADC_PIN       = 4              # ADC pin (ADC1 CH3)
SAMPLE_COUNT  = 16             # samples per batch
ADC_VREF_MV   = 3300           # reference voltage (mV)
ADC_MAX_COUNT = 4095           # 12-bit full scale

# Init ADC (11dB → 0~3.3V, 12-bit)
adc = ADC(Pin(ADC_PIN))
adc.atten(ADC.ATTN_11DB)

ATTN_11DB extends the range to cover 0~3.3V. adc.read() returns a 12-bit raw value (0~4095).

5.2 Averaged Sampling and Voltage Conversion

xxxxxxxxxx
while True:
    sum_val = 0

    for i in range(SAMPLE_COUNT):
        sum_val += adc.read()       # 12-bit raw value

    avg_raw = sum_val // SAMPLE_COUNT
    avg_mv  = (avg_raw * ADC_VREF_MV) // ADC_MAX_COUNT

    # ADC raw value + voltage (mV)
    print("ADC_Value=%4d, Voltage=%4d mV" % (avg_raw, avg_mv))

    time.sleep_ms(1000)

6. Operation Procedure

6.1 Flashing and Running

Thonny IDE steps: See the 1. Before Development → 3.Thonny IDE chapter.

6.2 Normal Phenomenon

The Thonny Shell prints one line per second:

xxxxxxxxxx
ADC_Value= 636, Voltage= 512 mV
ADC_Value=  83, Voltage=  66 mV
ADC_Value=  75, Voltage=  60 mV
ADC_Value=  71, Voltage=  57 mV
ADC_Value=  77, Voltage=  62 mV
ADC_Value=  85, Voltage=  68 mV
ADC_Value=  95, Voltage=  76 mV
ADC_Value= 102, Voltage=  82 mV
ADC_Value= 103, Voltage=  83 mV
ADC_Value= 101, Voltage=  81 mV
...
  • When GPIO4 is floating, the reading fluctuates (normal, pin floating)

    Floating serial output

  • Connect 3.3V → the reading is close to 4095, the voltage about 3300mV

3.3V serial output

Wiring method:

3.3V peak wiring

  • Connect GND → the reading is close to 0

GND serial output

Wiring method:

0 value wiring

6.3 Verification Methods

OperationExpected Result
GPIO4 floatingReading fluctuates randomly
GPIO4 connected to 3.3VReading ~4095, voltage ~3300mV
GPIO4 connected to GNDReading ~0, voltage ~0mV
Connect the middle pin of a potentiometerRotating the potentiometer continuously changes the reading and voltage
Change SAMPLE_COUNT=16 to SAMPLE_COUNT=1Output jitter increases noticeably

7. Common Problems

SymptomCauseSolution
Reading always close to 0Pin floating + no internal pull-upConnect an external test voltage or potentiometer to GPIO4
Reading fluctuates wildlyADC noise is a normal phenomenonIncrease SAMPLE_COUNT or use a hardware filter capacitor
Converted voltage always too smallADC reference or attenuation errorMeasure with a multimeter and do a linear calibration
Pin(4) errors or abnormal readingsGPIO4 conflicts with strapping pinsUse another ADC1 pin
Some pin readings wrong after WiFi is enabledADC2 channel is usedSwitch to an ADC1 channel (GPIO1~9)
Watchdog Timer
PWM Breathing LED

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文