This guide will help you get started with a PiicoDev® Potentiometer - an intuitive input device that allows you to easily interact with your project. PiicoDev Potentiometers are available in two flavours: Rotary and Slider. We'll walk through some examples to read from the potentiometer and look at some extra features like changing the output scale.
Potentiometers or "pots" are a ubiquitous user interface - you'll see them on audio amplifiers, mixing desks, lab gear and power supplies, and plenty of other equipment where an analogue control is required.
To get started - select your dev. board below.
Hardware and Connections
To follow along you'll need:
- A Raspberry Pi Pico (with Soldered Headers) or Pico W (with Soldered Headers)
- A PiicoDev Potentiometer or PiicoDev Slide Potentiometer
- A PiicoDev Expansion Board for Raspberry Pi Pico
- A PiicoDev Cable
- (Optional) A PiicoDev Platform will keep everything mounted securely.
Plug your Pico into the expansion board. Make sure it is plugged in the correct orientation - Pin 0 on the expansion board should be to the left of the Pico's USB connector.
Connect your Pot to the expansion board with a PiicoDev Cable.
To follow along you'll need:
- A Raspberry Pi single board computer (Pictured: Raspberry Pi 4 Model B)
- A PiicoDev Potentiometer or PiicoDev Slide Potentiometer
- A PiicoDev Adapter for Raspberry Pi
- A PiicoDev Cable (100mm or longer is best for Raspberry Pi)
- (Optional) A PiicoDev Platform will keep everything mounted securely.
Mount the Adapter onto your Pi's GPIO. Make sure it is plugged in the correct orientation - An arrow on the Adapter will point to the Pi's Ethernet connector (on a Pi 3 the Ethernet connector and USB ports are swapped.)
Connect your Pot to the Adapter with a PiicoDev Cable.
To follow along you'll need:
- A micro:bit v2
- A PiicoDev Potentiometer or PiicoDev Slide Potentiometer
- A PiicoDev Adapter for micro:bit
- A PiicoDev Cable
- (Optional) A PiicoDev Platform will keep everything mounted securely.
Plug your micro:bit into the Adapter, making sure the buttons on the micro:bit are facing up.
Connect your Pot to the Adapter with a PiicoDev Cable.
Setup Thonny
Download / Install PiicoDev Modules
To work with PiicoDev hardware, we need to download some drivers. The drivers provide all the functions to easily connect and communicate with PiicoDev hardware. Select your dev board from the options above.
We will need these files to easily drive the PiicoDev Potentiometer:
- Save the following files to your preferred coding directory - In this tutorial, we save to My Documents > PiicoDev.
- Download the PiicoDev Unified Library: PiicoDev_Unified.py (right-click, "save link as").
- Download the device module: PiicoDev_Potentiometer.py (right-click, "save link as")
- Upload the files to your Pico. This process was covered in the Setup Thonny section.
The PiicoDev Unified Library is responsible for communicating with PiicoDev hardware, and the device module contains functions for driving specific PiicoDev devices.
We will need these files to easily drive the PiicoDev Potentiometer:
- Save the following files to your preferred coding directory - In this tutorial, we save to My Documents > PiicoDev.
- Download the PiicoDev Unified Library: PiicoDev_Unified.py (right-click, "save link as").
- Download the device module: PiicoDev_Potentiometer.py (right-click, "save link as")
- Upload the files to your micro:bit. This process was covered in the Setup Thonny section.
The PiicoDev Unified Library is responsible for communicating with PiicoDev hardware, and the device module contains functions for driving specific PiicoDev devices.
Reading Values & Changing the Scale
We're ready to begin reading from our PiicoDev Potentiometer! The following example prints the value of the pot to the shell.
# Read the PiicoDev Potentiometer value from PiicoDev_Potentiometer import PiicoDev_Potentiometer from PiicoDev_Unified import sleep_ms pot = PiicoDev_Potentiometer() # Initialise the potentiometer pot.maximum = 100 # set the range of output values pot.minimum = 0 # if minimum or maximum are ommitted, they will default to 0 and 100 respectively while True: print(pot.value) # read the pot and print the result sleep_ms(100)
By default, the PiicoDev Potentiometer outputs a value between 0.0 (at the minimum of the scale) and 100.0 at the maximum of the scale. These limits are easy to change by setting the .minimum and .maximum properties. You can put whatever values you want in these! The nomenclature minimum and maximum refer to the pot's end-of-travel, so you can invert the direction by setting .minimum=100 and .maximum=0. You can even use negative numbers if you wish! Try configuring the potentiometer to output values between -1 and 1.
Code Remix
We'll use a pot to control the brightness of the Pico's on-board LED. This example shows how easy it is to control real, physical hardware with a Pot. By rescaling the pot using minimum and maximum, we don't have to do any extra processing to turn the Pot's value into an appropriate PWM value (for duty_u16()).
According to the MicroPython Documentation, a values between 0-65535 correspond to 0-100% duty cycle. We found that a value of 65535 produces flickering, so we chose 65534.
# Remix: Control the brightness of the Pico's LED with a PiicoDev Potentiometer from PiicoDev_Potentiometer import PiicoDev_Potentiometer from PiicoDev_Unified import sleep_ms from machine import Pin, PWM led = PWM(Pin(25)) # Initialise the pin connected to the onboard LED as a PWM channel pot = PiicoDev_Potentiometer(minimum=0, maximum=65534) # Scale the pot to the duty cycle range for a Raspberry Pi Pico while True: duty = round(pot.value) led.duty_u16( duty ) sleep_ms(20)
We'll now use the PiicoDev Potentiometer to control some hardware. The Raspberry Pi doesn't have any onboard hardware that we can control for this example, but we do have control of the power LED on the PiicoDev Potentiometer. The LED will always turn on at power-up, but we can control it afterwards with code. In this example, we'll use the pot to control how fast the LED flashes. To do this, we'll rescale the pot to create a delay between 300 and 100 milliseconds
# Use the potentiometer as a control input - change the flash speed of the on-board LED from PiicoDev_Potentiometer import PiicoDev_Potentiometer from PiicoDev_Unified import sleep_ms # Initialise the module - here the range corresponds to millisecond delays pot = PiicoDev_Potentiometer(minimum=500, maximum=50) while True: delay_ms = int(pot.value) # sample the pot, we know the value is already useful as milliseconds pot.led = not pot.led # toggle the LED hz = round(1000/delay_ms/2, 1) # convert delay [ms] to frequency [Hz] print('Flash rate ' + str(hz) + ' Hz') sleep_ms(delay_ms) # wait for the delay time
The micro:bit features an onboard buzzer - let's rescale the PiicoDev Potentiometer to work like a tone knob, changing the frequency of a tone. To do this, we'll need to import music, and call the music.pitch() method to generate a tone. The argument we provide music.pitch() will be a frequency. We can rescale the pot to give an audible range of frequencies. Thanks to a quirk of how the pitch() function works, no tone is created if we pass it a negative frequency. We can take advantage of this by scaling our Pot from -1 to 440 Hz. This means that when the pot is at the minimum end of the scale (-1) the buzzer will turn off.
# Use a PiicoDev Potentiometer to control the tone of a micro:bit's buzzer from PiicoDev_Potentiometer import PiicoDev_Potentiometer from PiicoDev_Unified import sleep_ms import music pot = PiicoDev_Potentiometer() # Initialise the potentiometer as a frequency-control pot.maximum = 440 # Hz pot.minimum = -1 # micro:bit will constantly "tick" if using 0Hz, so we'll use -1 for 'no sound' while True: frequency = int(pot.value) # pitch() accepts whole-numbers only! music.pitch(frequency) print(frequency, 'Hz') # read the pot and print the result sleep_ms(100)
Multiple Potentiometers
It is possible to connect to more than one PiicoDev Potentiometer at the same time. The following example uses two PiicoDev Potentiometers and a PiicoDev RGB LED module to create a lighting controller. One Pot controls Colour (hue) and the other controls Brightness. We will set up the colour-controlling pot to have all its ID switches OFF. The Brightness-controlling pot will have its ID #1 switch set to ON. Referring to the example code, we can see each Pot is initialised with an id argument which encodes the positions of the ID switches (1=ON, 0=OFF).
# Lighting controller example # One Pot controls the Hue (0 -> 1), one pot controls Brightness (0 -> 255) from PiicoDev_Potentiometer import PiicoDev_Potentiometer from PiicoDev_RGB import PiicoDev_RGB, wheel from PiicoDev_Unified import sleep_ms colour = PiicoDev_Potentiometer(minimum=0, maximum=1, id=[0,0,0,0]) brightness = PiicoDev_Potentiometer(minimum=0, maximum=255 ,id=[1,0,0,0]) leds = PiicoDev_RGB(id=[0,1,0,0]) # all devices with 'id' switches require a unique 'id' switch setting - even if they're a different type of module. while True: # Sample the pots for colour and brightness c = colour.value b = int(brightness.value) print(c*255, b) # print a line for each pot (scale up the colour) # Set the LED colour and brightness leds.fill( wheel(c) ) leds.setBrightness(b) sleep_ms(50)
Conclusion & Resources
We can now user PiicoDev Potentiometers as intuitive control inputs for our projects. Re-scaling the pot (changing its maximum and minimum) is convenient since we don't need to write additional code to modify the Pot's output value. Connecting multiple Pots is as simple as assigning them unique IDs. We've seen how all these features come together to create a simple lighting controller project, where a different pot controls properties of a multicoloured light.
If you're looking for some inspiration, check out the PiicoDev Sketcher Project - use two Pots and a Display to build a creative sketching toy!
For additional resources on how to use the PiicoDev Potentiometer API, read the Documentation.