HOW to Adjust Potentiometer through 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
|
|
|
|
|
---|---|---|---|---|
ESP32 Board*1 |
ESP32 Expansion Board*1 |
Keyestudio Rotary Potentiometer*1 |
3P Dupont Wire*1 |
Micro USB Cable*1 |
Connection Diagram
Test Code
# Import Pin, ADC and DAC modules.
from machine import ADC,Pin,DAC
import time
# Turn on and configure the ADC with the range of 0-3.3V
adc=ADC(Pin(34))
adc.atten(ADC.ATTN_11DB)
adc.width(ADC.WIDTH_12BIT)
# Read ADC value once every 0.1seconds, convert ADC value to DAC value and output it,
# and print these data to “Shell”.
try:
while True:
adcVal=adc.read()
dacVal=adcVal//16
voltage = adcVal / 4095.0 * 3.3
print("ADC Val:",adcVal,"DACVal:",dacVal,"Voltage:",voltage,"V")
time.sleep(0.1)
except:
pass
Code Explanation
1). In the experiment, add “From Machine import ADC” to the top of your Python file every time you use the ACD module, the same goes for DAC modules.
2). machine.ADC(pin): Create an ADC object associated with the given pin. 3). pin: The available pins are Pin(36)、Pin(39)、Pin(34)、Pin(35)、Pin(32)、Pin(33). DAC(pin). Create an DAC object associated with the given pin.
4). machine.ADC(pin): The available pins are pin (25) 、pin (26).
5). ADC. Read():Read ADC value and return ADC value.
6).ADC.atten(db): Set attenuation ration (that is, the full range voltage, such as the voltage of 11db full range is 3.3V)
db:attenuation ratio
ADC.ATTIN_0DB —full range of 1.2V
ADC.ATTN_2_5_DB —full range of 1.5V
ADC.ATTN_6DB —full range of 2.0 V
ADC.ATTN_11DB —full range of 3.3V
ADC.width(bit): Set data width.
bit:data bit
ADC.WIDTH_9BIT —9 data width
ADC.WIDTH_10BIT — 10 data width
ADC.WIDTH_11BIT — 11 data width
ADC.WIDTH_12BIT — 12 data width
7). The read()method reads the ADCvalue,rang is 0~4095,the adc.read() reads the ADC value input by the ADC(Pin(34)) Pin and assigns it to a variable named adcVal.
8). DAC.write(value):Output the voltage value, the data rang : 0-255,the corresponding output voltage is 0-3.3V.
Test Result
Connect the wires according to the experimental wiring diagram and poweron. Click “Run current script”, the code starts executing. The “Shell” window prints and displays the potentiometer ADC value, DAC value and voltage value. Rotating the potentiometer handle, the ADC value, DAC value and voltage value will change. Press “Ctrl+C”or click
“Stop/Restart backend”to exit the program.