Hans-Petter Halvorsen
In this Tutorial we will show how we can use a Potentiometer for Raspberry Pi Pico. We will use MicroPython. A Potentiometer is basically a variable resistor and potentiometers change their resistance when you turn a dial/knob. A Potentiometer has many applications, we will show some basic examples here.
Raspberry Pi Pico and Potentiometers (YouTube) + PowerPoint (PDF)
What do you need?
A Potentiometer is basically a variable resistor and Potentiometers change their resistance when you turn a dial/knob. A Potentiometer has 3 legs.
Potentiometers are small and cheap and you can buy them everywhere, like Adafruit, Sparkfun, Elfa Distrelec, etc.
Raspberry Pi Pico is a “downscaled” version of the original Raspberry Pi and is more comparable with Arduino compared to the original Raspberry Pi. You also need to use a downscaled version of Python, called MicroPython.
Haven't used Raspberry Pi Pico before? Getting Started with Raspberry Pi Pico
As mentioned, Raspberry Pi Pico uses a downscaled version of Python, called MicroPython, but most of the original Python will work.
Here you will find more Python Resources.
A Potentiometer is basically a variable resistor and Potentiometers change their resistance when you turn a dial/knob. A Potentiometer has 3 legs.
Below you see how you can wire and connect a Potentiometer to Raspberry Pi Pico:
Here you see the Raspberry Pi Pico Pinout.
Basiv MicroPython code example:
from machine import ADC
from time import sleep
adcpin = 26
pot = ADC(adcpin)
while True:
adc_value = pot.read_u16()
print(adc_value)
volt = (3.3/65535)*adc_value
print(round(volt,2))
sleep(1)
Raspberry Pi Pico has 3 Analog Input pins that we can use. Here you see the Raspberry Pi Pico Pinout.
ADC Value to Voltage Value:
The built-in Analog-to-Digital Converter (ADC) on Raspberry Pi Pico has a 16 bit resolution (2^16 = 65536 different levels), producing values from 0 to 65535. The read_u16()
function gives a value between 0 and 65535. It must be converted to a Voltage Signal 0 - 3.3v.
Final Application:
We can improve the code so that we get a value between 0-100%. We can also improve the code structure and quality and put some of the code into functions for metter readability and to make it easier to reuse the code in other applications.
Improved Code::
from machine import ADC
from time import sleep
def ReadPotentiometer():
adcpin = 26
pot = ADC(adcpin)
adc_value = pot.read_u16()
volt = (3.3/65535)*adc_value
percentPot = ScalePercent(volt)
return percentPot
def ScalePercent(volt):
percent = (volt/3.3)*100
return int(percent)
while True:
potvalue = ReadPotentiometer()
print(potvalue)
sleep(1)
This code reads values from the Potentiometer and converts it to a value between 0 and 100%.
2 functions have been made to make the code more structured and reusable.
In the video Raspberry Pi Pico and Potentiometers (YouTube) you see even more examples, e.g., how you can control the brightness of a LED and how fast a LED should blink.
For more Raspberry Pi Pico Resources, Tutorials, Examples and Videos see the main Raspberry Pi Pico page.
Internet of Things
Raspberry Pi resouces