ESP32-C3 · Potentiometer

ESP32-C3 Potentiometer Simulator: Wiring, Code and a Live Circuit

This is a 10 k potentiometer wired to an ESP32-C3 DevKitM-1, read live: the serial monitor prints the raw reading and a percent five times a second. Open it in the editor and turn the knob to see both change.

The ends go to 3V3 and GND and the wiper to GPIO 1.

  • 3.3 V logic
  • No library: analogRead()
  • Wiper on GPIO 1
  • 0 to 4095
ESP32-C3 · Potentiometerlive0.000 s 0.00x
Click to open it in the editor
The circuit itself, running on the simulator with the sketch below. Press what can be pressed; click anything else to open it in the editor.

Wiring the Potentiometer to the ESP32-C3

Every connection in the circuit above, with the board’s own pin names.

Potentiometer pinESP32-C3 pinWhy
Pin 1 (one end)3V3The top of the ADC’s range is 3.3 V, so the pot spans 0 to 3.3 V.
Pin 2 (wiper)GPIO 1The middle leg: a voltage between the two ends, set by the shaft.
Pin 3 (other end)GNDThe bottom of the range.

Run the pot from 3V3, never from 5V: turned to the top, the wiper would put 5 V on an analog input rated for 3.3 V. GPIO 0 to 4 are the ADC1 inputs: twelve bits, 0 to 4095, and at the default attenuation the whole 3.3 V rail spans the range.

Pins to leave alone on the ESP32-C3

GPIO 8 carries the onboard LED and GPIO 9 is the BOOT button, and both are also the default I2C pins. GPIO 2, 8 and 9 are strapping pins, and only GPIO 0 to 4 can read an analog voltage.

On a real bench

The ESP32-C3 DevKitM-1 is small enough to sit on one breadboard with room for the Potentiometer beside it. Its USB port goes through a USB-to-serial chip; the cheaper SuperMini boards use the C3’s own USB, where Serial needs USB CDC on boot turned on in the IDE.

The code

The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.

Reading a potentiometer on ESP32-C3 GPIO 1
const int POT_PIN = 1;   // the wiper, GPIO 1

void setup() {
  Serial.begin(115200);
}

void loop() {
  int raw = analogRead(POT_PIN);            // 0 to 4095
  int percent = map(raw, 0, 4095, 0, 100);
  Serial.print("Raw: ");
  Serial.print(raw);
  Serial.print("  Percent: ");
  Serial.println(percent);
  delay(200);
}

analogRead(POT_PIN) converts the wiper’s voltage to a number. GPIO 0 to 4 are the ADC1 inputs: twelve bits, 0 to 4095, and at the default attenuation the whole 3.3 V rail spans the range.

map(raw, 0, 4095, 0, 100) rescales it to a percent with whole-number arithmetic. For a PWM duty use map(raw, 0, 4095, 0, 255), or divide.

Serial.begin(115200) opens the serial monitor. Serial is UART0, at 115200 baud in this sketch.

Common mistakes with the Potentiometer on the ESP32-C3

Using a pin without an ADC

Only GPIO 0 to 4 reach ADC1 on the ESP32-C3. analogRead() on any other pin returns 0, which looks like a broken pot.

Wiring the wiper to an end

The middle leg is the wiper. With an end leg on the analog pin the reading never moves, or jumps from 0 to the top at one end of the travel.

Running the pot from 5V

At the top of the travel the pin would see 5 V, above the ESP32-C3 (RISC-V)’s rating. The pot belongs on 3V3.

Questions and answers

Can I simulate a potentiometer with an ESP32-C3 DevKitM-1?

Yes. The circuit at the top of this page is an ESP32-C3 DevKitM-1 with a potentiometer, running in your browser on a simulated ESP32-C3 (RISC-V). Drag the knob and the analog pin sees the divided voltage, converted the way the board’s own ADC converts it. Press Run it in the editor to change the wiring or the code; it is free and needs no account.

What range does analogRead() give on the ESP32-C3?

GPIO 0 to 4 are the ADC1 inputs: twelve bits, 0 to 4095, and at the default attenuation the whole 3.3 V rail spans the range.

Which way should the pot turn?

Swap the two end wires to reverse it. The wiper stays in the middle either way.

How do I dim an LED with the pot?

Read the pot, then analogWrite(ledPin, map(raw, 0, 4095, 0, 255)) on any output pin.

Build your own Potentiometer project

Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.