keyestudio
Share your creative ideas with us here
By ran | 25 February 2025 | 0 Comments

HOW to Use Potentiometer on Raspberry Pi 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.

Section 1: 0V – 3.3/4095 V analog quantity corresponding to digital 0;

Section 2: Analog quantities in the range 3.3/4095V – 2* 3.3/4095V correspond to digital 1;

The conversion formula is as follows:

Img

DAC: The higher the precision of DAC, the higher the precision of the output voltage value.

The conversion formula is as follows:

img

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

wps97

wps98

wps67

ESP32 Board*1

ESP32 Expansion Board*1

Keyestudio Rotary Potentiometer*1

image-20230602113648311

wps100

 

3P Dupont Wire*1

Micro USB Cable*1

 

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, 0, 4095, 0, 255);
  double voltage = adcVal / 4095.0 * 3.3;
  Serial.printf("ADC Val: %d, \t DAC Val: %d, \t Voltage: %.2fV\n", adcVal, dacVal, voltage);
  delay(200);
}
//**********************************************************************************

Code Explanation

Leave a Reply

Your email address will not be published.Required fields are marked. *
Name
E-mail
Content
Verification code