DEV Community

Cover image for ESP32 MicroPython Potentiometer
Arduino ESP32 Raspberry Pi Tutorials
Arduino ESP32 Raspberry Pi Tutorials

Posted on Originally published at newbiely.com

ESP32 MicroPython Potentiometer

A potentiometer is the simplest way to get an analog input into a microcontroller: turn a knob, get a changing voltage. This tutorial wires one up to an ESP32 running MicroPython and reads its position as a number you can use in code, the same trick behind volume knobs, brightness dials, or a speed control for a motor.

The full tutorial, with every detail and the line-by-line code explanation, is on our site: ESP32 MicroPython Potentiometer

What you need

Wiring

ESP32 MicroPython Potentiometer wiring diagram

A potentiometer has three pins. Connect the GND pin to the ESP32's GND and the VCC pin to 5V or 3.3V. The middle pin is the output, called the wiper, and it goes to one of the ESP32's analog input pins, here GPIO36 (ADC0). You can wire this on a breadboard, directly with jumper wires, or using a screw terminal expansion board if you prefer solid connections over breadboard pins.

How it works

Turning the potentiometer's shaft moves it between two extremes: 0 degrees near the GND pin and a maximum angle called ANGLE_MAX near the VCC pin. The voltage on the output pin follows the shaft position, from 0 volts at one end up to the full VCC voltage at the other. For any angle in between, the output voltage is roughly angle times VCC divided by ANGLE_MAX, so the wiper acts like a voltage divider you control by hand.

Code

The sketch sets up GPIO36 as an ADC input, configures it for 12-bit resolution (0-4095) and 11dB attenuation so it can read voltages up to about 3.3V. In the main loop it reads the raw ADC value, converts it into a voltage using a map_value helper that rescales a number from one range to another, and prints both the raw reading and the calculated voltage once per second.

esp32-micropython-potentiometer.py

"""
This ESP32 MicroPython code was developed by newbiely.com
This ESP32 MicroPython code is made available for public use without any restriction
For comprehensive instructions and wiring diagrams, please visit:
https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-potentiometer
"""

from machine import ADC, Pin
import time

# Define a function to map a value from one range to another
def map_value(x, in_min, in_max, out_min, out_max):
    return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min

# Initialize the analog pin for reading
adc = ADC(Pin(36))  # The ESP32 pin GPIO36 (ADC0) connected to the light sensor
# Set the ADC width (resolution) to 12 bits
adc.width(ADC.WIDTH_12BIT)
# Set the attenuation to 11 dB, allowing input range up to ~3.3V
adc.atten(ADC.ATTN_11DB)

# Infinite loop
while True:
    # Retrieve analog value from pin A0:
    adc_value = adc.read()
    # Convert the analog value to a voltage (0-3.3V range):
    voltage = map_value(adc_value, 0, 4095, 0, 3.3)

    # Output the analog value and corresponding voltage to the console:
    print("Analog: {}, Voltage: {:.2f}V".format(adc_value, voltage))

    # Wait for a second before repeating the loop:
    time.sleep(1)
Enter fullscreen mode Exit fullscreen mode

Full tutorial

The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-potentiometer

Top comments (0)