Arduino Nano ESP32 - Potentiometer
This tutorial provides instructions on how to use Arduino Nano ESP32 with the potentiometer. In detail, we will learn:
- How the potentiometer works
- How to interface Arduino Nano ESP32 with a potentiometer
- How to program Arduino Nano ESP32 to read the value from a potentiometer
- How to convert the value read from a potentiometer to the useful value
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
Overview of Potentiometer
The potentiometer (also known as rotary angle sensor) is used to change the settings (e.g. the speaker's volume, room's temperature, lamp's brightness...)
Potentiometer Pinout
A potentiometer usually has 3 pins:
- VCC pin: connect this pin to VCC (5V or 3.3v)
- GND pin: connect this pin to GND (0V)
- Output pin: outputs the voltage to ESP32's input pin.
※ NOTE THAT:
The GND pin and VCC pin are interchangeable
How Potentiometer Works
The potentiometer's shaft can be rotated from 0° (closest to GND pin) to an angle (closest to VCC pin), called ANGLE_MAX.
The voltage in the output pin is in direct proportion to the angle position of the shaft, varing from 0 to VCC. The below table shows the relation between the angle and the voltage on the output pin:
Angle | Voltage |
---|---|
0° | 0v |
ANGLE_MAX° | VCC |
angle° | angle° × VCC / ANGLE_MAX° |
※ NOTE THAT:
The ANGLE_MAX value is depended on manufacturers.
Arduino Nano ESP32 - Rotary Potentiometer
The ESP32's analog input pin converts the voltage (between 0v and 3.3V) into integer values (between 0 and 4095), called ADC value or analog value.
We can connect the potentiometer's output pin to an ESP32's analog input pin, and then read the analog value from the pin.
The analog value from input pin can be rescaled into another value. Let's see the use cases:
- Rescale the analog value to the potentiometer's angle.
- Rescale the analog value to the potentiometer's voltage.
- Rescale the analog value to the the setting value (e.g. the speaker's volume, room's temperature, lamp's brightness...)
Rescale Range
FROM | TO | |||
---|---|---|---|---|
Angle | rotated by user | 0° | → | ANGLE_MAX° |
Voltage | from potentiometer's pin | 0V | → | 3.3V |
ADC value | read by ESP32 | 0 | → | 4095 |
Setting value | converted by ESP32 | VALUE_MIN | → | VALUE_MAX |
Wiring Diagram between Potentiometer and Arduino Nano ESP32
This image is created using Fritzing. Click to enlarge image
How To Program Potentiometer
- Read the value from an input pin, which connected to the output pin of the potentiometer by using analogRead() function.
- Rescale to the potentiometer's angle by using map() function.
- Rescale to the potentiometer's voltage:
- Rescale to the controllable value (e.g volume of stereo, brightness, speed of DC motor... )
- For example, rescaling to the brightness of LED. As mentioned in this tutorial, the brightness of LED can be controlled by using PWM value from 0 (always OFF) to 255 (always ON). Therefore, we can map the analog value to the brightness of LED (from OFF to the brightest) as follows:
If you want to dim LED from the nightlight to the brightest,
※ NOTE THAT:
The map() function can only be used to rescale the analog value to the int or long type value. If the controllable value is float type, you need to use the floatMap() function instead of the map() function.
floatMap() function:
Arduino Nano ESP32 Code
Detailed Instructions
- If this is the first time you use Arduino Nano ESP32, see how to setup environment for Arduino Nano ESP32 on Arduino IDE.
- Copy the above code and paste it to Arduino IDE.
- Compile and upload code to Arduino Nano ESP32 board by clicking Upload button on Arduino IDE
- Open Serial Monitor on Arduino IDE
- Rotate the potentiometer
- Check out the result on the Serial Monitor. It looks like the below:
※ NOTE THAT:
This tutorial uses the analogRead() function to read values from an ADC (Analog-to-Digital Converter) connected to a sensor or component. The Arduino Nano ESP32's ADC is suitable for projects that do not require high accuracy. However, for projects needing precise measurements, keep the following in mind:
- The Arduino Nano ESP32's ADC is not perfectly accurate and might require calibration for correct results. Each Arduino Nano ESP32 board can 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 want.
For projects requiring high precision, consider using an external ADC (e.g ADS1115) with the Arduino Nano ESP32 or using another Arduino, such as the Arduino Uno R4 WiFi, which has a more reliable ADC. If you still want to calibrate the Arduino Nano ESP32's ADC, refer to the ESP32 ADC Calibration Driver.