STANNUM
Open the simulator

Tutorials / Input

Potentiometer

A variable resistor whose wiper divides 3.3 V — the first analog input, read with analogRead().

Parts in the example:
  • Potentiometer
  • SG90 servo
The Potentiometer block as it appears on the canvas.

What it is

A potentiometer is a resistor with a third contact — the wiper — that slides along it. With the two ends on 3.3 V and ground, the wiper's voltage goes from 0 to 3.3 V as you turn the shaft: a voltage divider you can adjust. The chip's ADC turns that voltage into a number, 0 to 4095 on the ESP32.

In the simulator

The block has the wiper position as a slider (0 to 100 %; 0 % puts the wiper on the GND side) and shows what follows from it: the resistance between wiper and ground out of the total, the voltage, and the ADC count the code will read. The total resistance (1 kΩ to 100 kΩ) is a setting — it changes the numbers on the block, not the voltage, which is what a divider does.

Pins

PinWhat it is
3V3one end of the track — to a 3V3 symbol
Sthe wiper — to a GPIO with an ADC (34 in the examples)
GNDthe other end of the track — to a GND symbol

Wiring

The circuit below is the example Servo on a potentiometer from the lab — open it with Project → Open example… and it comes ready to run.

The wired circuit, as the lab draws it.
Potentiometer pinGoes to
3V3a 3V3 symbol
SD34 on the board (GPIO 34)
GNDa GND symbol

Settings

Double-click the block's title bar to open its card, then the Settings tab. Changes apply to the running simulation right away.

The Settings tab of the Potentiometer.
SettingAcceptsDefaultNotes
Total resistance1 kΩ · 5 kΩ · 10 kΩ · 50 kΩ · 100 kΩ10 kΩThe value printed on the body of the part. The wiper splits that resistance.

Code

sketch.cpp
#include <Arduino.h>
#include <ESP32Servo.h>

// The servo does not receive "degrees": it receives a PULSE of ~0.5 to 2.4 ms
// every 20 ms. write(angulo) only translates — the part shows the pulse measured.
Servo servo;
const int POT = 34, SINAL = 18;

void setup() {
  Serial.begin(115200);
  servo.attach(SINAL);
}

void loop() {
  int leitura = analogRead(POT);              // 0..4095
  int angulo = map(leitura, 0, 4095, 0, 180);
  servo.write(angulo);
  Serial.printf("pot=%4d -> %3d degrees\n", leitura, angulo);
  delay(100);
}

analogRead(POT) returns 0–4095; map(value, 0, 4095, 0, 180) turns it into an angle for the servo. No pinMode is needed for an analog input.

Run it

Press Build and run. The first build of a project takes a while; after that, only what changed is rebuilt.

The simulation running: the canvas reacts and the serial monitor shows what the code prints.

Drag the slider: the servo arm follows from 0° to 180° and the serial monitor prints the reading and the angle. Set the range to 10 kΩ or 100 kΩ in Settings and see the block's resistance line change while the ADC stays the same.

How the simulation models it

Try this

  1. Print the reading as a voltage: reading * 3.3 / 4095.
  2. Average 16 readings before printing, and compare the steadiness with a single reading — then imagine the real, noisy sensor.
  3. Move the wiper wire to GPIO 5 and read the message the lab gives.

See also