HOW to Use Potentiometer with ESP32
Overview
The following we will introduce is the Keyestudio rotary potentiometer which is an analog sensor.
The digital IO ports can read the voltage value between 0 and 3.3V and the module only outputs high levels. However, the analog sensor can read the voltage value through 16 ADC analog ports on the ESP32 board. In the experiment, we will display the test results on the Shell.
Working Principle
It uses a 10K adjustable resistor. We can change the resistance by rotating the potentiometer. The signal S can detect the voltage changes(0-3.3V) which are analog quantity.
ADC: The more bits an ADC has, the denser the partitioning of the simulation, the higher the accuracy of the final conversion.
Subsection 1: The analog value within 0V—3.3/4095 V corresponds to the number 0; Subsection 2: The analog value within 3.3/4095V—2*3.3/4095V corresponds to the number 1; ……
The conversion formula is as follows:
DAC: The higher the precision of DAC, the higher the precision of the output voltage value.
The conversion formula is as follows:
ADC on ESP32:
The ESP32 has 16 pins that can be used to measure analog signals. GPIO pin serial numbers and analog pin definitions are shown below:
|
ADC number in ESP32 |
ESP32 GPIO number |
|
---|---|---|---|
|
ADC0 |
GPIO 36 |
|
|
ADC3 |
GPIO 39 |
|
|
ADC4 |
GPIO 32 |
|
|
ADC5 |
GPIO33 |
|
|
ADC6 |
GPIO34 |
|
|
ADC7 |
GPIO 35 |
|
|
ADC10 |
GPIO 4 |
|
|
ADC11 |
GPIO0 |
|
|
ADC12 |
GPIO2 |
|
|
ADC13 |
GPIO15 |
|
|
ADC14 |
GPIO13 |
|
|
ADC15 |
GPIO 12 |
|
|
ADC16 |
GPIO 14 |
|
|
ADC17 |
GPIO27 |
|
|
ADC18 |
GPIO25 |
|
|
ADC19 |
GPIO26 |
|
DAC on ESP32:
The ESP32 has two 8-bit digital-to-analog converters connected to GPIO25 and GPIO26 pins, which are immutable, as shown below :
Simulate pin number |
GPIO number |
---|---|
DAC1 |
GPIO25 |
DAC2 |
GPIO26 |
Components
Connection Diagram
Test Code
//**********************************************************************************
/*
* Filename : Rotary_potentiometer
* Description : Read the basic usage of ADC,DAC and Voltage
* Auther : http//www.keyestudio.com
*/
#define PIN_ANALOG_IN 34 //the pin of the Potentiometer
void setup() {
Serial.begin(9600);
}
//In loop(),the analogRead() function is used to obtain the ADC value, and then the map() function is used to convert the value into an 8-bit precision DAC value.
//The input and output voltage are calculated according to the previous formula, and the information is finally printed out.
void loop() {
int adcVal = analogRead(PIN_ANALOG_IN);
int dacVal = map(adcVal,