ADC Analog Input
ADC Analog Input
Overview
ADC (Analog-to-Digital Converter) is used to collect analog signals such as light intensity, smoke concentration, flame intensity, soil moisture, and other analog sensor output voltages. ESP32 has a built-in 12-bit ADC that can convert 0~3.3V analog voltage to 0~4095 digital values.
Supported Peripheral Types
| Type | type Value | Description |
|---|---|---|
| GPIO_ANALOG_INPUT | 15 | GPIO analog input (simple mode) |
| ADC | 26 | ADC dedicated mode (with attenuation/resolution parameters) |
ADC Parameter Description
| Parameter | Description | Options |
|---|---|---|
| attenuation | Attenuation coefficient | 0=0dB(1.1V), 1=2.5dB(1.5V), 2=6dB(2.2V), 3=11dB(3.3V) |
| resolution | Resolution (bits) | 9, 10, 11, 12 |
| sampleRate | Sample rate | 0=Default |
Common configuration:
attenuation=3(full scale 3.3V) +resolution=12(0~4095)
Before connecting ADC, verify input voltage, attenuation, resolution, and WiFi impact on ADC2; when troubleshooting on-site, it is recommended to save raw values, converted values, and sampling intervals simultaneously.
Available Pins
- ESP32 ADC1: GPIO 32-39 (recommended, still available when WiFi is enabled)
- ESP32 ADC2: GPIO 0, 2, 4, 12-15, 25-27 (not available when WiFi is enabled)
- ESP32-C3: GPIO 0-4 (ADC1)
- ESP32-S3: GPIO 1-10 (ADC1), GPIO 11-20 (ADC2)
Important: When using WiFi, only ADC1 channel pins can be used!
Typical Sensor Applications
| Sensor | Output Range | Description |
|---|---|---|
| Photoresistor | 0~3.3V | Lower voltage with stronger light |
| MQ-2 Smoke | 0~3.3V | Higher voltage with higher concentration |
| Flame Sensor | 0~3.3V | Lower voltage when flame is closer |
| Rain Drop Sensor | 0~3.3V | Lower voltage with more rain |
| Sound Sensor | 0~3.3V | Higher voltage with louder sound |
| Soil Moisture | 0~3.3V | Lower voltage with higher moisture |
Configuration
Method 1: Web Interface Configuration (Recommended)
Before saving ADC peripherals, verify input pins, range conversion parameters, and whether voltage divider protection is in place.
Step 1: Navigate to Peripheral Management Page
- Open a browser and navigate to the ESP32 IP address
- After logging in, click Peripheral Configuration in the left menu
Step 2: Add ADC Analog Input Peripheral
Click the Add Peripheral button
Fill in the configuration:
Field Value Description Peripheral ID light_sensororsmoke_sensorUnique identifier Name Light IntensityorMQ-2 Smoke SensorDisplay name Peripheral Type GPIO Analog Input (type: 15) or ADC (type: 26) Simple mode or dedicated mode Pin Configuration 36or34Must use ADC1 pins Attenuation 33=11dB(0-3.3V full scale) Resolution 1212-bit=0-4095 Click Save
Step 3: Verify Configuration
- Find the newly added peripheral in the peripheral list
- Click the Enable toggle
- Check real-time ADC values
⚠️ Important: ADC1 pins (GPIO 32-39) must be used when WiFi is enabled
Method 2: JSON Configuration File Import
Add the following configuration to the peripherals array in data/config/peripherals.json:
Light Sensor
{
"id": "light_sensor",
"name": "Light Intensity",
"type": 15,
"enabled": false,
"pins": [36],
"params": {
"initialState": 0,
"pwmChannel": 0,
"pwmFrequency": 1000,
"pwmResolution": 8,
"defaultDuty": 0
}
}ADC Dedicated Configuration (Smoke Sensor)
{
"id": "smoke_sensor",
"name": "MQ-2 Smoke Sensor",
"type": 26,
"enabled": false,
"pins": [34],
"params": {
"attenuation": 3,
"resolution": 12,
"sampleRate": 0
}
}Multi-Channel ADC Collection
{
"id": "adc_multi",
"name": "Environment Monitoring ADC",
"type": 26,
"enabled": false,
"pins": [36, 39, 34, 35],
"params": {
"attenuation": 3,
"resolution": 12,
"sampleRate": 0
}
}Peripheral Execution Integration
As Poll Trigger Data Source
ADC values can be periodically collected by peripheral execution rules via ACTION_SENSOR_READ(19):
{
"targetPeriphId": "smoke_sensor",
"actionType": 19,
"actionValue": "",
"useReceivedValue": false,
"syncDelayMs": 0,
"execMode": 0
}As Platform Trigger Condition
Collected ADC values are reported via MQTT and can be used for platform trigger conditions:
{
"triggerType": 0,
"triggerPeriphId": "smoke_sensor",
"operatorType": 2,
"compareValue": "2000"
}Meaning: Trigger rule when smoke sensor ADC value > 2000
Voltage/Current Sensors
For scenarios requiring ADC value conversion to actual physical quantities, refer to:
- Current Sensor — ACS712 and other current measurements
- Voltage Sensor — Voltage divider measurements
Notes
- WiFi and ADC2 Conflict: ADC2 is not available when WiFi is enabled; production environments must use ADC1 pins
- ESP32 ADC Non-linearity: ESP32 native ADC has non-linearity at both ends (near 0V and 3.3V), with better accuracy in the middle range
- Reference Voltage: ESP32 internal reference voltage is approximately 1.1V, expanded range through attenuation coefficient
- Multi-Channel Collection: Same ADC type peripheral can configure multiple pins, system polls sequentially
- Sampling Noise: Recommend multiple sample averaging for ADC readings (system has built-in 10-time averaging for current/voltage sensors)
