Skip to content

uPesy only delivers currently in France.

Contents Menu Expand Light mode Dark mode Auto light/dark mode

Measure an analog voltage with the ADC of the ESP32 in MicroPython

On ESP32, some pins are attached to the built-in ADC of the microcontroller. An ADC gives a digital value proportional to the measured input voltage.

Note

ADC stands for Analog to Digital Converter.

Features

On ESP32, pins 36, 39, 34 and 35 can be used as an analog input with MicroPython. The measurement resolution is 12 bits. So values range between 0 and 4095.

Warning

Unlike the Arduino, the ESP32 analog inputs should not exceed a 3.3V voltage. This could damage or even destroy the ADC inputs. Therefore, it is essential to check not to use a 5V supply voltage to measure the value of a sensor. There are solutions to limit the ADC input voltage to 3.3V.

Reading an analog input from ESP32 with MicroPython.

The analog input voltage is measured using the ADC submodule of the machine module. Like with PWM, we associate a physical pin with the object ADC .

from machine import Pin, ADC

# Pin definitions
adc_pin = Pin(36, mode=Pin.IN)
adc = ADC(adc_pin)
adc.atten(ADC.ATTN_11DB)

Remember to import the ADC submodule. To read a voltage on the entire range from 0 to 3.3V, you must also specify an attenuation of 11 dB (with ADC.ATTN_11DB )

You can also create an ADC object by specifying the ADC channel digit you want to use.

from machine import ADC
adc = ADC(0) # Select the ADC_0 ()

Warning

On the ESP32, the ADC channels are allocated a bit out of order to the pins. It is, therefore, better to call them by pin number.

Once you have configured an analog input in MicroPython, you can read the analog value using the read() function.

from machine import Pin, ADC
adc = ADC(Pin(36, mode=Pin.IN))
print(adc.read())

It will be necessary to apply a formula to have value measured in volts:

\[V_{volt} = \frac{3.3V}{4095} \times V_{bits}\]

Project: Control LED brightness using a potentiometer on the ESP32

We will use PWM and ADC with MicroPython to control the LED brightness.

Schematic:

Note

Consider putting a resistor in series with the LED to prevent burning. (330 Ω, for example)

For reading the knob position of the potentiometer only :

from machine import Pin, ADC
import time


# Create an ADC object linked to pin 36
adc = ADC(Pin(36, mode=Pin.IN))

while True:

    # Read ADC and convert to voltage
    val = adc.read()
    val = val * (3.3 / 4095)
    print(round(val, 2), "V") # Keep only 2 digits

    # Wait a bit before taking another reading
    time.sleep_ms(100)

To read knob position and change the brightness:

from machine import Pin, ADC, PWM
import time


def map(x, in_min, in_max, out_min, out_max):
    """ return linear interpolation like map() fonction in Arduino"""
    return (x - in_min) * (out_max - out_min) // (in_max - in_min) + out_min


if __name__ == "__main__":

    # Create a PWM object linked to pin 23
    pwm_led = PWM(Pin(23,mode=Pin.OUT))
    pwm_led.freq(1_000)

    # Create an ADC object linked to pin 36
    adc = ADC(Pin(36, mode=Pin.IN))
    adc.atten(ADC.ATTN_11DB)

    while True:

        val = adc.read()
        val = val * (3.3 / 4095)
        pwm_value = map(x=val, in_min=0, in_max=4095, out_min=0,out_max=1023)
        pwm_led.duty(pwm_value)
        time.sleep_ms(10)

We use cookies to make your visit to the site as pleasant as possible. Privacy Policy