Raspberry Pi Pico · Potentiometer

Raspberry Pi Pico Potentiometer Simulator: Wiring, Code and a Live Circuit

This is a 10 k potentiometer wired to a Raspberry Pi Pico, 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(OUT) and GND and the wiper to GP26 (physical pin 31).

  • 3.3 V logic
  • No library: analogRead()
  • Wiper on GP26 (physical pin 31)
  • 0 to 1023
Raspberry Pi Pico · 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 Raspberry Pi Pico

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

Potentiometer pinRaspberry Pi Pico pinWhy
Pin 1 (one end)3V3(OUT)The top of the ADC’s range is 3.3 V, so the pot spans 0 to 3.3 V.
Pin 2 (wiper)GP26 (physical pin 31)The 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 VBUS (5 V from USB): turned to the top, the wiper would put 5 V on an analog input rated for 3.3 V. GP26 to GP28 are the ADC inputs (A0 to A2): the converter is twelve bits against the 3.3 V rail, and analogRead() returns ten bits, 0 to 1023, as arduino-pico does; analogReadResolution(12) gives 0 to 4095.

Pins to leave alone on the Raspberry Pi Pico

The numbers in the code are GP numbers, not the physical pin numbers printed beside the header: GP15 is pin 20. GP23 to GP25 are used on the board itself, and GP25 is the LED.

On a real bench

Solder headers on the Pico and press it into the breadboard across the center channel; the Potentiometer goes in beside it. GND pins sit every fifth pin down both sides (pins 3, 8, 13, 18 and so on), so there is always one close. The first upload needs BOOTSEL held while plugging in.

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 Raspberry Pi Pico GP26 (physical pin 31)
const int POT_PIN = 26;   // the wiper, GP26 (physical pin 31)

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

void loop() {
  int raw = analogRead(POT_PIN);            // 0 to 1023
  int percent = map(raw, 0, 1023, 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. GP26 to GP28 are the ADC inputs (A0 to A2): the converter is twelve bits against the 3.3 V rail, and analogRead() returns ten bits, 0 to 1023, as arduino-pico does; analogReadResolution(12) gives 0 to 4095.

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

Serial.begin(115200) opens the serial monitor. Serial is the USB port at 115200 baud in this sketch; on a real Pico it appears once the board has enumerated.

Common mistakes with the Potentiometer on the Raspberry Pi Pico

Reading a digital pin

analogRead() needs one of A0 to A5. On a digital pin it reads nothing useful.

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 VBUS (5 V from USB)

At the top of the travel the pin would see 5 V, above the RP2040’s rating. The pot belongs on 3V3.

Questions and answers

Can I simulate a potentiometer with a Raspberry Pi Pico?

Yes. The circuit at the top of this page is a Raspberry Pi Pico with a potentiometer, running in your browser on a simulated RP2040. 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 Raspberry Pi Pico?

GP26 to GP28 are the ADC inputs (A0 to A2): the converter is twelve bits against the 3.3 V rail, and analogRead() returns ten bits, 0 to 1023, as arduino-pico does; analogReadResolution(12) gives 0 to 4095.

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, 1023, 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.