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.

Transcript

A potentiometer or pot is used to create an adjustable signal. They're commonly found as user controls on electronics like dinners and audio amplifiers. I'm going to show you how to get started reading from a PiicoDev potentiometer using a Raspberry Pi Pico. The PiicoDev potentiometer comes in two flavors: rotary and slide. You can follow this tutorial with either of these models; they each behave exactly the same.

Fester a tour of the potentiometer, there are two PiicoDev connectors to allow daisy-chain connections. The ID switch is for connecting multiple modules; setting these switches will select a different address from a pool. For now, leave all these switches in the off position to use the default address. You may need to fit the knob to your PiicoDev potentiometer. For a slide pot, carefully push it onto the stem. For a rotary pot, first center the shaft so its Notch is aligned with the center of the scale, then carefully align the knob onto the shaft and push it down. Turn the knob to each end to check that the knob matches the scale.

Mount the Pico into the expansion board, making sure that the USB connector is on the same end as pin number zero. Connect a PiicoDev cable to the expansion board and connect the other end to your pot. I'll start with a rotary pot, but of course, you could use a slide pot; it'll be the same. Connect to your computer with USB.

Before we get too far ahead of ourselves, if you get stuck at any point and need some help, if you're at a school then ask your teacher, at a Makerspace then ask the staff.

Ask your coordinator if you're at home, else let us know on the forums. We're full-time makers and happy to help. Alright, let's get started. Grab the drivers from the article for this tutorial. Find the download and install section and right click 'Picadev Unified', 'Save Link As' and I'll save that to a 'PiicoDev' directory in my documents. And I'll do the same with 'PiicoDev Potentiometer', 'Save Link As' and I'll save it to my documents. Here's those files we're going to be using Phony to program our Raspberry Pi Pico today. If you've never programmed a Pico with Thonny check the guide for more information.

I've opened Fully connected to my Pico and I'll upload those two files to my Raspberry Pi Pico. You can see them in the file pane here. Return to the article and find the first example reading values. Copy all of that code and paste it into a new script in Thonny and I'll save this as 'main.py' to my Raspberry Fire Pico. Make sure you can see the plotter in the bottom right, you can see that from 'View Plotter'. And once you're ready, run the script. Immediately we should see some values coming up on the shell here, we're at about 49 and if I turn the pot all the way to the left, we can see that value goes to zero and the graph also falls to zero. If I turn it to the right, we can see that line climb and the value goes to 100.

For those following along with a slide pot instead, I'll just unplug my rotary pot, plug in the slide part and rerun the code. And you can see I can slide that and you can see that that works exactly the same. Let's take a look at the code. We start by importing the PiicoDev potentiometer functionality and also a sleep function to create a delay. We call the potentiometer initialization function which returns a potentiometer object and we call that 'pot'. Next, we set the scale of the potentiometer.

We set the pot.maximum property to 100 and the pot.minimum property to zero, and that's what sets our scale from 0 to 100. As we turn the knob, there's an infinite loop and we just print pot.value followed by our sleep.

So we can change the scale of the potentiometer. Let's include some negative numbers; we could go from 1 down to -1. And when we run this script, now when the pot is all the way to the left we read -1 and when it's all the way to the right we read 1. Now these keywords maximum and minimum they refer to the physical ends of travel, so you can actually invert the scale of the potentiometer by setting maximum to -1 and minimum to 1. Now we've reflected the values that come out of the potentiometer. I've got it all the way to the right and I'm reading -1 when I move it to the left it now reads 1. Nice.

Now for a code remix, potentiometers are often used as dimmers and there's an onboard LED on the p-code that we can control using the potentiometer. Let's first import some functions from a machine to control the LED. From machine import pin and pwm. Pin is what we use to interact with the physical pins and PWM is what we'll use to change the brightness of the onboard LED. We can initialize the LED that's onboard; we can say LED and assign PWM pin 25. Pin 25 is the pin that connects to the LED. I'm going to set the maximum to 65534 and the minimum to zero. This is a special number in base two; this is basically the maximum number that we can apply with our PWM to create a fully lit LED. You may be more familiar with 65535 but that will actually create a flickering LED in this case through some quirk of micropython, so I'll use maximum 65534 and zero. Next, we'll create a variable called Duty and assign that the pot.value.

Pot.value returns floating point numbers, so we want to round that to the nearest whole number to use for our PWM value. Then we can call LED.duty and that's an unsigned 16-bit number. We'll pass in our beauty variable. So, we read the potentiometer value, round it to the nearest whole number and call that duty. Then we set the duty cycle of the LED using this duty function or this duty value. I'll remove this print and run the script.

Now, if we look at the LED, we can turn the knob and control the brightness. I have to cover it because the studio lights are very bright in here. When I turn the knob all the way to the right, it's fully on and then I can dim it down and make it turn off. Nice.

And of course, for completeness here it is again with the slide potentiometer. Full brightness and dimmed to off. Nice. Taking this idea further, it's possible to connect to multiple Picadef potentiometers at the same time. As an illustrated example, I have here a color selector. I'm using my right control as a dimmer, so as I slide that dimmer up the LEDs will become brighter. And then I'm using my left control as a color selector, so as I rotate that knob we can select a different color. Here we're starting with a blue color and then as I move to the left we go through yellow, orange and it becomes red. Then as I move to the right it becomes more magenta.

To achieve this, I've left my rotary potentiometer with all of its ID switches off. I've set the first ID switch on my other potentiometer so that I can tell which is which. Then in the code, when we initialize each potentiometer, we initialize them with the ID argument. Here I have my PiicoDev potentiometer that I'm using to select color. It has all its ID switches off, so I pass it the argument with ID all zeros. My slide pot, however, is being used for brightness.

We initialize the LED module and then we read the color selecting potentiometer and the brightness selecting potentiometer. We fill the LEDs with the selected color and we set the LEDs to the selected brightness. And it has the first ID switch on and so I'm passing it the argument ID and the first item in the list is a one meaning that that switch is on.

If you'd like to follow this example you can find it at the bottom of the article. And there you have it, reading a PiicoDev potentiometer both in rotary and slide format, we were able to read simple values, rescale the potentiometer and even connect multiple potentiometers together to create a more complex project.

If you make something cool with the starter project or you just have some questions, let us know on the forums. We're full-time makers and happy to help. Until next time, happy making!

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.