ESP32 MicroPython Potentiometer

This tutorial shows you how to use a potentiometer with ESP32 and MicroPython. In detail, We will learn:

ESP32 MicroPython potentiometer

Hardware Preparation

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Potentiometer
1×(Alternative) Potentiometer Kit
1×(Alternative) Potentiometer Module with Knob
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Expansion Board for ESP32
1×(Recommended) Power Splitter For ESP32

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Potentiometer

A rotary potentiometer, which is also called a rotary angle sensor, is used to adjust settings manually like stereo volume, lamp brightness, or how much you zoom in on an oscilloscope.

Potentiometer Pinout

Pinout

A potentiometer usually has three pins.

  • GND pin: connect to GND (0 volts)
  • VCC pin: connect to VCC (5 volts or 3.3 volts)
  • Output pin: sends voltage to ESP32's input pin.
Potentiometer Pinout

※ NOTE THAT:

You can switch the positions of the GND pin and the VCC pin.

How It Works

The potentiometer's shaft turns from 0 degrees, near the GND, to a maximum position near the VCC pin, called ANGLE_MAX.

The voltage at the output pin ranges from the ground (GND) voltage to the supply voltage (VCC). As you rotate the shaft, the output voltage changes accordingly.

  • If the angle is 0 degrees, there is no voltage (0 volts) at the output pin.
  • If the angle is ANGLE_MAX, the output pin's voltage is the same as VCC’s voltage.
  • For angles between 0 degrees and ANGLE_MAX, the output voltage is calculated as: angle × VCC / ANGLE_MAX.

※ NOTE THAT:

The value of ANGLE_MAX changes depending on the manufacturer. Normally, we do not focus on the value of ANGLE_MAX unless we need to calculate the rotation angle (see the use cases section).

How Potentiometer Works

ESP32 - Rotary Potentiometer

The ESP32's ADC pins can function as analog inputs, converting a voltage range from 0 volts to VCC into whole numbers between 0 and 4095. These numbers are referred to as ADC values or analog values.

You can connect the output pin of a potentiometer to one of the ESP32's analog input pins, enabling the ESP32 to read the ADC value and convert it into a usable number.

The number obtained by the ESP32 is not an angle or a direct voltage but rather a whole number between 0 and 4095. This value can then be mapped or converted to a different range as needed for specific applications.

Use Cases

  • Changing the ADC value into an angle.
  • Changing the ADC value into voltage.
  • Changing the ADC value to a value you can control (like the loudness of a stereo, how bright something is, or the speed of a motor). This is often the most common use.

Rescale Range

FROM TO
Anglerotated by userANGLE_MAX
Voltagefrom potentiometer's pin 0V3.3V
ADC valueread by ESP32 04095
Other valueconverted by ESP32 VALUE_MINVALUE_MAX

Wiring Diagram

  • How to connect ESP32 and potentiometer using breadboard
The wiring diagram between ESP32 MicroPython Potentiometer

This image is created using Fritzing. Click to enlarge image

How to connect ESP32 and potentiometer

ESP32 MicroPython Code

""" 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)

Detailed Instructions

Here’s instructions on how to set up and run your MicroPython code on the ESP32 using Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Confirm that MicroPython firmware is loaded on your ESP32 board.
  • If this is your first time using an ESP32 with MicroPython, check out the ESP32 MicroPython Getting Started guide for step-by-step instructions.
  • Connect the ESP32 board to the potentiometer according to the provided diagram.
  • Connect the ESP32 board to your computer with a USB cable.
  • Open Thonny IDE on your computer.
  • In Thonny IDE, go to Tools Options.
  • Under the Interpreter tab, choose MicroPython (ESP32) from the dropdown menu.
  • Make sure the correct port is selected. Thonny IDE usually detects it automatically, but you might need to select it manually (like COM12 on Windows or /dev/ttyACM0 on Linux).
  • Copy the provided MicroPython code and paste it into Thonny's editor.
  • Save the code to your ESP32 by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device.
    • Name the file main.py.
  • Click the green Run button (or press F5) to execute the script.
  • Adjust the potentiometer
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot Analog: 0, Voltage: 0.00V Analog: 0, Voltage: 0.00V Analog: 504, Voltage: 0.41V Analog: 1124, Voltage: 0.91V Analog: 2068, Voltage: 1.67V Analog: 3016, Voltage: 2.43V Analog: 3624, Voltage: 2.92V Analog: 4092, Voltage: 3.30V Analog: 4092, Voltage: 3.30V
MicroPython (ESP32) • CP2102 USB To UART Bridge Controller @ COM12 ≡

※ NOTE THAT:

This tutorial demonstrates how to use the adc.read() function to read values from an ADC (Analog-to-Digital Converter) connected to a potentiometer. The ESP32's ADC is suitable for projects that do not require high precision. However, if your project needs accurate measurements, keep the following in mind:

  • The ESP32 ADC is not perfectly accurate and may require calibration for precise results. Each ESP32 board may vary slightly, so calibration is necessary for each individual board.
  • Calibration can be challenging, especially for beginners, and might not always yield the exact results you desire.

For projects requiring high precision, consider using an external ADC (e.g., ADS1115) with the ESP32 or opt for an Arduino, which has a more reliable ADC. If you still wish to calibrate the ESP32 ADC, refer to the ESP32 ADC Calibration Driver.

Video Tutorial

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!