Analog to Digital Converter

Overview

ESP32 integrates two 12-bit SAR (“Successive Approximation Register”) ADCs (Analog to Digital Converters) and supports measurements on 18 channels (analog enabled pins). Some of these pins can be used to build a programmable gain amplifier which is used for the measurement of small analog signals.

The ADC driver API currently only supports ADC1 (9 channels, attached to GPIOs 32-39).

Taking an ADC reading involves configuring the ADC with the desired precision and attentuation settings, and then calling adc1_get_voltage() to read the channel.

It is also possible to read the internal hall effect sensor via ADC1.

Application Example

Reading voltage on ADC1 channel 0 (GPIO 36):

#include <driver/adc.h>

...

    adc1_config_width(ADC_WIDTH_12Bit);
    adc1_config_channel_atten(ADC1_CHANNEL_0,ADC_ATTEN_0db);
    int val = adc1_get_voltage(ADC1_CHANNEL_0);

Reading the internal hall effect sensor:

#include <driver/adc.h>

...

    adc1_config_width(ADC_WIDTH_12Bit);
    int val = hall_sensor_read();

The value read in both these examples is 12 bits wide (range 0-4095).

API Reference

Header Files

  • components/driver/include/driver/adc.h

Enumerations

enum adc1_channel_t

Values:

ADC1_CHANNEL_0 = 0

ADC1 channel 0 is GPIO36

ADC1_CHANNEL_1

ADC1 channel 1 is GPIO37

ADC1_CHANNEL_2

ADC1 channel 2 is GPIO38

ADC1_CHANNEL_3

ADC1 channel 3 is GPIO39

ADC1_CHANNEL_4

ADC1 channel 4 is GPIO32

ADC1_CHANNEL_5

ADC1 channel 5 is GPIO33

ADC1_CHANNEL_6

ADC1 channel 6 is GPIO34

ADC1_CHANNEL_7

ADC1 channel 7 is GPIO35

ADC1_CHANNEL_MAX
enum adc_atten_t

Values:

ADC_ATTEN_0db = 0

The input voltage of ADC will be reduced to about 1/1

ADC_ATTEN_2_5db = 1

The input voltage of ADC will be reduced to about 1/1.34

ADC_ATTEN_6db = 2

The input voltage of ADC will be reduced to about 1/2

ADC_ATTEN_11db = 3

The input voltage of ADC will be reduced to about 1/3.6

enum adc_bits_width_t

Values:

ADC_WIDTH_9Bit = 0

ADC capture width is 9Bit

ADC_WIDTH_10Bit = 1

ADC capture width is 10Bit

ADC_WIDTH_11Bit = 2

ADC capture width is 11Bit

ADC_WIDTH_12Bit = 3

ADC capture width is 12Bit

Functions

esp_err_t adc1_config_width(adc_bits_width_t width_bit)

Configure ADC1 capture width.

The configuration is for all channels of ADC1

Return
  • ESP_OK success
  • ESP_ERR_INVALID_ARG Parameter error
Parameters
  • width_bit: Bit capture width for ADC1

esp_err_t adc1_config_channel_atten(adc1_channel_t channel, adc_atten_t atten)

Configure the ADC1 channel, including setting attenuation.

The default ADC full-scale voltage is 1.1V. To read higher voltages (up to the pin maximum voltage, usually 3.3V) requires setting >0dB signal attenuation for that ADC channel.

Note
This function also configures the input GPIO pin mux to connect it to the ADC1 channel. It must be called before calling adc1_get_voltage() for this channel.

When VDD_A is 3.3V:

  • 0dB attenuaton (ADC_ATTEN_0db) gives full-scale voltage 1.1V
  • 2.5dB attenuation (ADC_ATTEN_2_5db) gives full-scale voltage 1.5V
  • 6dB attenuation (ADC_ATTEN_6db) gives full-scale voltage 2.2V
  • 11dB attenuation (ADC_ATTEN_11db) gives full-scale voltage 3.9V (see note below)

Note
The full-scale voltage is the voltage corresponding to a maximum reading (depending on ADC1 configured bit width, this value is: 4095 for 12-bits, 2047 for 11-bits, 1023 for 10-bits, 511 for 9 bits.)
Note
At 11dB attenuation the maximum voltage is limited by VDD_A, not the full scale voltage.
Return
  • ESP_OK success
  • ESP_ERR_INVALID_ARG Parameter error
Parameters
  • channel: ADC1 channel to configure
  • atten: Attenuation level

int adc1_get_voltage(adc1_channel_t channel)

Take an ADC1 reading on a single channel.

Note
Call adc1_config_width() before the first time this function is called.
Note
For a given channel, adc1_config_channel_atten(channel) must be called before the first time this function is called.
Return
  • -1: Parameter error
  • Other: ADC1 channel reading.
Parameters
  • channel: ADC1 channel to read

int hall_sensor_read()

Read Hall Sensor.

Note
The Hall Sensor uses channels 0 and 3 of ADC1. Do not configure these channels for use as ADC channels.
Note
The ADC1 module must be enabled by calling adc1_config_width() before calling hall_sensor_read(). ADC1 should be configured for 12 bit readings, as the hall sensor readings are low values and do not cover the full range of the ADC.
Return
The hall sensor reading.