Example 13: ADC Analog-to-Digital Conversion
About 2 min
Example 13: ADC Analog-to-Digital Conversion
Experiment Overview
Read analog voltage values via ADC (Analog-to-Digital Converter), converting 0~3.3V analog signals to digital values. ESP32 has two ADC modules (ADC1: 8 channels, ADC2: 10 channels), with resolution up to 12 bits (0~4095).
Hardware Wiring
| Board Label | GPIO Pin | Connected Device |
|---|---|---|
| ADC | GPIO34 | Potentiometer/Analog sensor output |
| ADC | GPIO36 | Analog signal input |
GPIO34-39 are input-only pins, best suited for ADC acquisition. ADC2 is unavailable when WiFi is active.
JSON Configuration Example
{
"peripherals": [
{
"id": "adc_01",
"name": "ADC Analog Input",
"type": 15,
"enabled": false,
"pins": [34],
"params": {}
}
]
}You can also use a dedicated ADC type for more precise configuration:
{
"id": "adc_precise",
"name": "ADC Precision Acquisition",
"type": 26,
"enabled": false,
"pins": [36],
"params": {
"attenuation": 3,
"resolution": 12,
"sampleRate": 0
}
}ADC Attenuation Parameters
| attenuation | Range | Precision |
|---|---|---|
| 0 | 0~1.1V | Highest |
| 1 | 0~1.5V | High |
| 2 | 0~2.2V | Medium |
| 3 | 0~3.3V | Lower |
Peripheral Execution Linkage
Scenario: Voltage Monitoring Alarm (Poll Trigger)
Function: Detect ADC voltage every second, trigger buzzer alarm when voltage exceeds threshold (3000mV)
Web Interface Configuration Steps
Step 1: Ensure buzzer peripheral is configured
- Peripheral ID:
buzzer_gpio - Type: GPIO Output
Step 2: Create Rule
- Click left menu Peripheral Configuration → Switch to Peripheral Execution Management tab
- Click New Rule button
- Fill in basic configuration:
- Rule Name:
Over-Voltage Alarm - Report Data: ✅ Enable
- Enabled: ✅ Enable
- Rule Name:
Step 3: Configure Trigger (Poll Trigger)
Click Add Trigger button
Fill in trigger configuration:
Field Value Description Trigger Type Select Poll Trigger Periodically detect condition Target Peripheral Select adc_01ADC analog input Poll Interval 10001000ms = 1 second Condition Expression value > 3000Voltage greater than 3000mV
Step 4: Configure Action
Click Add Action button
Fill in:
- Action Type: Select HIGH Level
- Target Peripheral: Select
buzzer_gpio
Click Save button
Execution Flow:
Poll every 1 second
↓
Read ADC value (0-4095)
↓
Check: value > 3000?
↓ Yes
Buzzer alarmADC Attenuation Parameter Reference
| attenuation | Range | Precision | Use Case |
|---|---|---|---|
| 0 | 0~1.1V | Highest | Precision small voltage measurement |
| 1 | 0~1.5V | High | Small voltage measurement |
| 2 | 0~2.2V | Medium | Medium voltage measurement |
| 3 | 0~3.3V | Lower | Full range measurement (default) |
Notes
- ADC2 Limitation: ADC2 (GPIO0,2,4,12-15,25-27) is unavailable when WiFi is enabled
- Input Range: Exceeding 3.3V will damage the pin, voltage divider circuit needed
- Precision: ESP32 ADC has non-linear errors, calibration needed for precise measurements
- Input Impedance: ADC input impedance is high, but source impedance should not exceed 10kΩ
- Sample Rate: Default single sample, sampleRate=0 means read on demand
