ESP32 with Potentiometer (HW-040)

undefined

Introduction to Potentiometer

A potentiometer is a three-terminal resistor with a sliding contact (wiper) that acts as a voltage divider. When connected to the ESP32's analog input pin, it provides variable analog values that can be read and processed. These values can be used to control brightness, volume, or motor speed. In this experiment, we will read analog values from a potentiometer using the ESP32.

Components Required

We will need a potentiometer (HW-040), an ESP32 development board, and a few jumper wires. The middle terminal (wiper) of the potentiometer will be connected to an analog input pin on the ESP32.

  • Connect potentiometer VCC to ESP32 3.3V.
  • Connect potentiometer GND to ESP32 GND.
  • Connect potentiometer OUT (middle pin) to ESP32 GPIO 34 (or any analog input pin).
  • Open the Arduino IDE on your computer.
  • Go to Tools > Board > Boards Manager.
  • Search for 'ESP32' and install the ESP32 package.
  • Select 'ESP32 Dev Module' from the Board menu.
  • Connect ESP32 to your computer via USB cable.
  • Go to Tools > Port and select the correct COM port.
  • Go to Sketch > Include Library > Manage Libraries.
  • Search for 'Adafruit Unified Sensor' and install it (if required).
  • Paste the provided Arduino code into a new sketch.
  • Click Upload (right arrow icon) to upload code to ESP32.
  • Wait for the code to compile and upload.
  • Open Serial Monitor (Ctrl + Shift + M) and set baud rate to 115200.
  • Observe analog values and voltage readings on Serial Monitor.

Formula: Voltage = (analogRead(POT_PIN) * 3.3) / 4095

undefined

Components List

  • 1 × ESP32 Development Board
  • 1 × Potentiometer (HW-040)
  • 1 × Breadboard
  • 1 × Set of Jumper Wires
  • 1 × USB Cable for ESP32
  • 1 × Computer with Arduino IDE installed

Pin Configuration

  • VCC: Connect to ESP32 3.3V
  • GND: Connect to ESP32 GND
  • OUT (middle terminal): Connect to ESP32 GPIO 34 (analog input)

Make sure to connect the middle terminal (wiper) of the potentiometer to an analog input pin of the ESP32. GPIO 34 is a good option as it is input-only and supports ADC.

undefined - Pin Diagram

Wiring and Connections

  • VCC -> 3.3V
  • GND -> GND
  • OUT -> GPIO 34
undefined - Circuit Diagram

Code for ESP32 and Potentiometer

1#define POT_PIN 34
2
3void setup() {
4  Serial.begin(115200);
5}
6
7void loop() {
8  int potValue = analogRead(POT_PIN);
9  float voltage = (potValue * 3.3) / 4095.0;
10
11  Serial.print("Raw Value: ");
12  Serial.print(potValue);
13  Serial.print(" | Voltage: ");
14  Serial.print(voltage);
15  Serial.println(" V");
16  delay(500);
17}

Code Explanation

  • #define POT_PIN 34: Defines the GPIO pin connected to the potentiometer's output.
  • analogRead(POT_PIN): Reads the analog value from the potentiometer (range: 0-4095).
  • float voltage = (potValue * 3.3) / 4095.0;: Converts the raw ADC value into actual voltage (0 to 3.3V).
  • Serial.print(...): Displays the raw and voltage values on the Serial Monitor.

Applications

  • Volume and brightness controls
  • Analog sensor calibration
  • User-controlled input for DIY projects
  • Servo angle adjustment based on potentiometer rotation