STANNUM
Open the simulator

Tutorials / Power & measuring

Voltage divider and the voltmeter

A potentiometer as a divider on an ADC pin, with a voltmeter on the wiper: what the code reads, what the block shows, and what the meter can and cannot see.

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

What it is

A potentiometer is a resistor with a third terminal — the wiper — that slides along it. Wire the two ends to 3V3 and GND and the wiper is a voltage divider: at 50 % of the travel it sits at half the supply, 1.65 V. The ADC turns that voltage into a number from 0 to 4095, which is how every knob, slider and analog sensor reaches the code.

In the simulator

The potentiometer block shows the divider's arithmetic on its face: the wiper's position, the resistance on each side, the voltage at the wiper and the ADC count the code will read. The Voltmeter block sits across the wiper and ground. It is an instrument, not a part of the circuit: it shows the difference between its two probes and, in Settings, a Range — automatic, volts or millivolts, like a bench multimeter.

Pins

PinWhat it is
+the + probe — to the point you want to measure
the − probe — to the reference, usually a GND symbol

Potentiometer

The Potentiometer block on the canvas.
PinWhat it is
3V3the top of the divider — to a 3V3 symbol
Sthe wiper, where the divided voltage comes out — to a GPIO with an ADC
GNDthe bottom of the divider — to a GND symbol

Wiring

The circuit below is the example Voltage divider with a voltmeter from the lab — open it with Project → Open example… and it comes ready to run.

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

The potentiometer's V and G go to a 3V3 and a GND symbol; its wiper S goes to GPIO 34, an input-only pin with an ADC. The voltmeter's + shares the wiper's net and its − goes to a 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 Voltmeter.
SettingAcceptsDefaultNotes
RangeAutomatic · Volts · MillivoltsAutomaticOn the mV range, 3.3 V shows up as 3300 mV — handy to read small drops.

Potentiometer

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>

const int ENTRADA = 34;            // GPIO 34: input only, with ADC
const float VREF = 3.3;

void setup() {
  Serial.begin(115200);
  delay(300);
  Serial.println("move the potentiometer wiper and compare with the voltmeter");
}

void loop() {
  int bruto = analogRead(ENTRADA);          // 0..4095
  float volts = bruto * VREF / 4095.0;
  float porcento = bruto * 100.0 / 4095.0;

  Serial.printf("ADC=%4d  %.2f V  (%.0f%% of travel)\n", bruto, volts, porcento);
  delay(400);
}

analogRead(34) gives 0 to 4095; multiplied by 3.3 and divided by 4095 it becomes volts, and by 100 it becomes the percentage of the travel. The sketch prints the three every 400 ms so you can compare them with the block's face.

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 wiper: the ADC count, the volts and the percentage in the serial monitor follow the numbers on the potentiometer's face. The voltmeter, though, reads 0 V — hover it and the tooltip says its + probe is floating. That is not a bug; see below.

The voltmeter across the wiper while the simulation runs: the wiper's net has no source of its own.

How the simulation models it

Try this

  1. Move the voltmeter's + probe to the potentiometer's V pin (the 3V3 side): it reads 3.3 V. Switch the Range to mV.
  2. Change VREF in the code to 3.0 and watch the printed volts stop matching the block — the mistake of assuming a supply that is not there.
  3. Wire the wiper to GPIO 22 instead of 34 and build: the lab tells you the pin has no ADC.

See also