Skip to content

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 ​

Typetype ValueDescription
GPIO_ANALOG_INPUT15GPIO analog input (simple mode)
ADC26ADC dedicated mode (with attenuation/resolution parameters)

ADC Parameter Description ​

ParameterDescriptionOptions
attenuationAttenuation coefficient0=0dB(1.1V), 1=2.5dB(1.5V), 2=6dB(2.2V), 3=11dB(3.3V)
resolutionResolution (bits)9, 10, 11, 12
sampleRateSample rate0=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 ​

SensorOutput RangeDescription
Photoresistor0~3.3VLower voltage with stronger light
MQ-2 Smoke0~3.3VHigher voltage with higher concentration
Flame Sensor0~3.3VLower voltage when flame is closer
Rain Drop Sensor0~3.3VLower voltage with more rain
Sound Sensor0~3.3VHigher voltage with louder sound
Soil Moisture0~3.3VLower voltage with higher moisture

Configuration ​

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 ​

  1. Open a browser and navigate to the ESP32 IP address
  2. After logging in, click Peripheral Configuration in the left menu

Step 2: Add ADC Analog Input Peripheral ​

  1. Click the Add Peripheral button

  2. Fill in the configuration:

    FieldValueDescription
    Peripheral IDlight_sensor or smoke_sensorUnique identifier
    NameLight Intensity or MQ-2 Smoke SensorDisplay name
    Peripheral TypeGPIO Analog Input (type: 15) or ADC (type: 26)Simple mode or dedicated mode
    Pin Configuration36 or 34Must use ADC1 pins
    Attenuation33=11dB(0-3.3V full scale)
    Resolution1212-bit=0-4095
  3. Click Save

Step 3: Verify Configuration ​

  1. Find the newly added peripheral in the peripheral list
  2. Click the Enable toggle
  3. 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 ​

json
{
  "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) ​

json
{
  "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 ​

json
{
  "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):

json
{
  "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:

json
{
  "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:

Engineering Units and Quality ​

Industrial AI (analog input) can perform conversion through persistent parameters such as signalMode, conversionEnabled, calibratedVoltage, inputMin/inputMax, and engineeringMin/engineeringMax. The default configuration preserves raw ADC behavior; front-end parameters must match the engineering-unit range, and readings whose quality indicates conversion failure, disconnection, or out-of-range must not be treated as a normal zero value.

Conversion Fields ​

After selecting 0-10V, 0-20mA, 4-20mA, or custom linear mode, the Web UI automatically enables engineering-unit conversion and fills in common input ranges; advanced calibration items are collapsed by default, and usually only the engineering-unit lower and upper limits need to be filled in.

FieldDefaultDescription
signalMode00=raw value, 1=0-10V, 2=0-20mA, 3=4-20mA, 4=custom linear
conversionEnabledfalseWhether to output engineering units; when off, raw ADC reporting is kept
inputMin / inputMaxDerived from signal typeInput signal range; 4-20mA defaults to 4/20
engineeringMin / engineeringMax0/100Engineering-unit range corresponding to the sensor nameplate
engineeringOffset0Field calibration offset of the conversion result
clampEnabledfalseClamp when out of range; when off, the current reading is invalid and reporting stops
vRef3.3ADC reference voltage
voltageRatio1Divider restoration ratio for the 0-10V front end
shuntResistance165Current-loop shunt resistor, in Ω
inputGain / inputOffset1/0Analog front-end gain and voltage offset

The conversion formula is EU=(INPUT-INPUTmin)/(INPUTmax-INPUTmin)×(EUmax-EUmin)+EUmin+engineeringOffset. In 4-20mA mode, the ADC voltage is first converted to mA using the shunt resistor and front-end gain; in 0-10V mode, it is first converted to the input voltage using the divider restoration ratio. ESP32 pins cannot directly accept 10V or a current loop; a suitable divider, isolation, or shunt front end must be used.

The new fields are backward compatible: old configurations lacking the new fields use safe default values and keep their original behavior.

Acquisition Quality and Reporting ​

After engineering-unit conversion is enabled, the value and the acquisition quality are handled separately:

qualityqualityCodeMeaning
valid0Input is within the configured range
low1Input is below range but has not reached the 4-20mA disconnection threshold
high2Input is above range but has not reached the 4-20mA short/overdrive threshold
disconnected34-20mA input is less than inputMin - 1mA; for the standard range, less than 3mA
short44-20mA input is greater than inputMax + 1mA; for the standard range, greater than 21mA
invalid_config5Signal type, range, engineering-unit range, or other configuration is invalid

GET /api/peripherals/read?id=<peripheral ID> returns rawValue, value, valueValid, quality, and qualityCode together. When clamping is off, an abnormal reading has a value of null and valueValid=false; when clamping is on, the original abnormal quality is still kept and only the value is limited to the engineering-unit endpoints. When conversion is enabled and the mode is not raw, periodic MQTT additionally reports a <peripheral ID>_quality property, e.g. ai_soil_temp_quality=disconnected; when abnormal and not clamped, the value property is omitted but the quality property is still reported.

A plain analog read in Peripheral Execution directly reuses the conversion result of the peripheral configuration, so range and calibration parameters are maintained in a single place; the action's own scaleFactor/offset can still be used for a final secondary adjustment.

The legacy read categories for voltage-divider and current sensors have separate action calibration parameters — see Voltage and Current — to avoid double conversion; for NTC thermistor temperature measurement, see NTC Thermistor. For ADC resource lifecycle, sampling failures, and multi-consumer behavior, see the ADC lifecycle document.

Save a disabled draft first, complete the wiring/resource review, then enable the peripheral, read, verify quality, and finally enable the periodic rule. Calibrate using actual measurements from a multimeter or a standard source; raw * 3.3 / 4095 must not be treated as a measured voltage.

Notes ​

  1. WiFi and ADC2 Conflict: ADC2 is not available when WiFi is enabled; production environments must use ADC1 pins
  2. 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
  3. Reference Voltage: ESP32 internal reference voltage is approximately 1.1V, expanded range through attenuation coefficient
  4. Multi-Channel Collection: Same ADC type peripheral can configure multiple pins, system polls sequentially
  5. Sampling Noise: Recommend multiple sample averaging for ADC readings (system has built-in 10-time averaging for current/voltage sensors)

FastBee | fastbee.cn