ADC is an analog-to-digital converter is a system that converts an analog signal like a light signal entering a digital camera into a digital signal. An ADC may also provide an isolated measurement such as an electronic device that converts an input analog voltage or current to a digital number representing the magnitude of the voltage or current. In this tutorial “ADC on ESP32” discussing the ADC operations in ESP32.
Not every pin in ESP32 has the ability to do analog to digital conversion. In ESP32 there are some pins for ADC operation. The ESP32 integrates two 12-bit SAR (“Successive Approximation Register”) ADC (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.
There are in total 18 x 12 bits ADC input channels, GPIO ADC Channel
- GPIO 0 => ADC2_CH1
- GPIO 2 => ADC2_CH2
- GPIO 4 => ADC2_CH0
- GPIO 12 => ADC2_CH5
- GPIO 13 => ADC2_CH4
- GPIO 14 => ADC2_CH6
- GPIO 15 => ADC2_CH3
- GPIO 25 => ADC2_CH8
- GPIO 26 => ADC2_CH9
- GPIO 27 => ADC2_CH7
- GPIO 32 => ADC1_CH4
- GPIO 33 => ADC1_CH5
- GPIO 34 => ADC1_CH6
- GPIO 35 => ADC1_CH7
- GPIO 36 => ADC1_CH0
- GPIO 37 => ADC1_CH1
- GPIO 38 => ADC1_CH2
- GPIO 39 => ADC1_CH3
ADC Calculation
ESP32 ADCs have 12bits of resolution, so the total range of ADCs reading go from 0 to 4,095((2^12)-1). The analog to digital conversions is dependant on the system voltage. Because we predominantly use the 12-bit ADC of the ESP32 have a system voltage of 5V, The ADC in the ESP32 controller uses successive approximation to convert the analog voltage to digital.
Most of the ADCs are ratiometric, So we can simplify this equation slightly.
For example, the measured analog voltage will be 2.5V, in 12-bit ADC the resolution will be 4095 and the system voltage is 5V, So the ADC value will be
Example .1: AnalogReadSerial
This example shows you how to read analog input. A potentiometer in the analog board, it is a simple mechanical device that provides a varying amount of resistance when its shaft is turned. By passing voltage through a potentiometer and into an analog input on your board, it is possible to measure the amount of resistance produced by a potentiometer (pot) as an analog value.
Reads an analog input on pin 0, prints the result to the serial monitor.
int sensorValue = analogRead(0);
analogRead(pin)-Reads the value from the specified analog pin
Example.2: ReadAnalogVoltage
This example shows you how to read an analog input on analog pin 0, convert the values from analogRead() into a voltage, and print it out to the serial monitor
int sensorValue = analogRead(0);
Convert the analog reading (which goes from 0 – 4095) to a voltage (0 – 5V):
float voltage = sensorValue * (5.0 / 4095.0);