Raspberry Pi Pico - Potentiometer

This guide shows you how to use a Raspberry Pi Pico with a potentiometer. We will learn:

Raspberry Pi Pico potentiometer

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico (Alternatively)
1×Micro USB Cable
1×Potentiometer
1×(Alternative) Potentiometer Kit
1×(Alternative) Potentiometer Module with Knob
1×Breadboard
1×Jumper Wires
1×(Optional) Screw Terminal Expansion Board for Raspberry Pi Pico

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Potentiometer

A rotary potentiometer, which is also called a rotary angle sensor, is used to adjust settings manually like stereo volume, lamp brightness, or how much you zoom in on an oscilloscope.

Potentiometer Pinout

Pinout

A potentiometer usually has three pins.

  • GND pin: connect to GND (0 volts)
  • VCC pin: connect to VCC (5 volts or 3.3 volts)
  • Output pin: sends voltage to Raspberry Pi Pico's input pin.
Potentiometer Pinout

※ NOTE THAT:

You can switch the positions of the GND pin and the VCC pin.

How It Works

The potentiometer's shaft turns from 0 degrees, near the GND, to a maximum position near the VCC pin, called ANGLE_MAX.

The voltage at the output pin ranges from the ground (GND) voltage to the supply voltage (VCC). As you rotate the shaft, the output voltage changes accordingly.

  • If the angle is 0 degrees, there is no voltage (0 volts) at the output pin.
  • If the angle is ANGLE_MAX, the output pin's voltage is the same as VCC’s voltage.
  • For angles between 0 degrees and ANGLE_MAX, the output voltage is calculated as: angle × VCC / ANGLE_MAX.

※ NOTE THAT:

The value of ANGLE_MAX changes depending on the manufacturer. Normally, we do not focus on the value of ANGLE_MAX unless we need to calculate the rotation angle (see the use cases section).

How Potentiometer Works

Raspberry Pi Pico - Rotary Potentiometer

The ADC pins on the Raspberry Pi Pico can be used as analog inputs. These pins convert the voltage, which varies from 0 volts to VCC, into whole numbers from 0 to 4095. These numbers are known as ADC values or analog values.

You can connect the output pin of a potentiometer to an analog input pin on the Raspberry Pi Pico. This allows the Raspberry Pi Pico to read the ADC value and turn it into a number that you can use.

The number that the Raspberry Pi Pico gets is not an angle or voltage; it is a whole number between 0 and 4095.

We change the number from the analog input pin to a different number. Now, let's see its use.

Use Cases

  • Changing the ADC value into an angle.
  • Changing the ADC value into voltage.
  • Changing the ADC value to a value you can control (like the loudness of a stereo, how bright something is, or the speed of a motor). This is often the most common use.

Rescale Range

FROM TO
Anglerotated by userANGLE_MAX
Voltagefrom potentiometer's pin 0V3.3V
ADC valueread by Raspberry Pi Pico 04095
Other valueconverted by Raspberry Pi Pico VALUE_MINVALUE_MAX

Wiring Diagram

The wiring diagram between Raspberry Pi and Pico Potentiometer

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Pico Code

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-potentiometer """ from machine import ADC, Pin import time # Define a function to map a value from one range to another def map_value(x, in_min, in_max, out_min, out_max): return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min # Initialize the analog pin for reading adc = ADC(Pin(26)) # Corresponds to GP26, which is ADC0 on the Pico # Infinite loop while True: # Retrieve analog value from pin A0: adc_value = adc.read_u16() >> 4 # Convert 16-bit to 12-bit by right shifting 4 bits # Convert the analog value to a voltage (0-3.3V range): voltage = map_value(adc_value, 0, 4095, 0, 3.3) # Output the analog value and corresponding voltage to the serial monitor: print("Analog: {}, Voltage: {:.2f}V".format(adc_value, voltage)) # Wait for a second before repeating the loop: time.sleep(1)

Detailed Instructions

Please follow these instructions step by step:

  • Ensure that Thonny IDE is installed on your computer.
  • Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
  • If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
  • Wire the Raspberry Pi Pico to the potentiometer according to the provided diagram.
  • Connect the Raspberry Pi Pico to your computer using a USB cable.
  • Launch the Thonny IDE on your computer.
  • On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
  • In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
  • Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico by:
    • Click the Save button, or use Ctrl+S keys.
    • In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
    • Save the file as main.py
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Adjust the potentiometer
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot Analog: 0, Voltage: 0.00V Analog: 0, Voltage: 0.00V Analog: 504, Voltage: 0.41V Analog: 1124, Voltage: 0.91V Analog: 2068, Voltage: 1.67V Analog: 3016, Voltage: 2.43V Analog: 3624, Voltage: 2.92V Analog: 4092, Voltage: 3.30V Analog: 4092, Voltage: 3.30V
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

If you name your script main.py and save it to the root directory of the Raspberry Pi Pico, it will automatically run each time the Pico is powered on or reset. This is useful for standalone applications that need to start running immediately upon power-up. If you name your script another name other than main.py, you will need to manually run it from Thonnys's Shell.

Video Tutorial

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!