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 with the PiicoDev potentiometer and a Raspberry Pi. 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.

First up, 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.

To follow along, you'll need a Raspberry Pi already set up to run like a desktop computer, a PiicoDev adapter for Raspberry Pi, and a PiicoDev potentiometer. You could be using a slider or a rotary pot today; it doesn't matter which of the potentiometers you're following along with today, they both work the same and I'll show you examples using both anyhow. Finally, you'll need a PiicoDev cable to connect everything together. Mount the adapter onto the Raspberry Pi like so. For a Raspberry Pi 4, there will be an arrow on the adapter that points towards the ethernet connector. Connect your PiicoDev cable to one of the ports in the adapter and connect the other end to your applicable potentiometer.

I'll start with the rotary part. Connect Ethernet and HDMI for video, and finally connect power and boot up your Pi. You're going to be working with Thonny today. First up, open Thonny and make sure you're most up to date with the PiicoDev packages. Go to Tools > Manage Packages, search for Picadev, and install or upgrade as necessary. I'm already up to the latest stable version, so I can proceed.

We're ready to begin working with the pot. Open the article for this tutorial and find the first example reading values and changing the scale. I'm going to copy all of that code and paste it into a new script in Funny. Click Run and I'll save this as read.py and I'll save that to a PiicoDev directory in my home directory. If all goes well, in this shell you should immediately start seeing some numbers appear. And if I turn the knob of the potentiometer, we can see those numbers change. Click View and make sure that you can see the plotter. If I expand that out, turn the potentiometer knob, you can see that graph update in real time. How nice is that? I can set it to about 50. Turn it all the way to the left and it's at zero, and turn it all the way to the right and it's at a hundred.

Let's take a look at the code. And if you happen to be working with this slide pot today, I've stopped the script. I'll just unplug my rotary pot and plug in the slider pot, rerun the script, and you can see that this works exactly the same. I can push it to 100 and pull it down to zero exactly the same.

We start by importing the PiicoDev potentiometer module. We also import a sleep function to create a delay. Next, we call the potentiometer initialization function, and that returns a potentiometer object that we assign to pot. Whenever we refer to pot, we're referring to this PiicoDev potentiometer. We're going to set some of the properties. Now, we set pot.maximum to 100 and pot.minimum to 0.

We can use the potentiometer to control some real hardware. To do this, we set the maximum value to 100 and the minimum value to 0. We then read the pot.value and print it to the shell with a short sleep of 100 milliseconds. We can modify the scale to whatever realistic number we like, for example, we can make it from -1 to 1. When we run the script, the range of the pot has been changed. If we don't set the min or max, the potentiometer will default to 0 to 100.

Now, wouldn't it be nice to control some real hardware using this potentiometer? You often see them used as dimmer or volume knobs. The Raspberry Pi doesn't have any immediately accessible hardware that we can see change when we change the value of the pot. However, this LED that you see on the back can be used. is flickering very slowly

The potentiometer is nominally a power indicator, but we actually do have control over it. We can turn this on and off as we like, and so what I propose is that we change the flashing rate of this LED depending on the location of the slide pot. Since we would otherwise have to wire something else up to the Raspberry Pi to control it, since we're talking about flashing this LED, I'll set up the scale of the pot to be some delay and that will be in milliseconds. That delay will change how fast the LED flashes, so we can go from a maximum of say 50 to a minimum of 500. You'll notice that maximum is smaller than minimum, we'll come back to that in a sec.

Now in our infinite loop, we can probably delete this call to print and instead I'll create a variable called delay, delayMs for milliseconds, and we'll read pot.value like we did before, but I want to use a whole number for my delay, so I'll cast that as an integer.

To control the LED on the potentiometer, we could say pot.LED equals 1 to turn it on, 0 to turn it off. If I want to invert the state of that LED though, I can just say not pot.LED. And so here, what we're basically saying is toggle the state of that LED. This avoids having to first check if it's on and then do some kind of logic depending on whether the LED is on or off. If it's on, turn it off, if it's off, turn it on. Here we're just saying invert the state.

And now for the delay, we can use delayMs. I'll save this script as Flash.py and give that a run. I'm in a very bright studio so I'm covering the LED with my hand so that you can see it there, and you can see that LED is indeed flashing. If I slide the potentiometer all the way up to the maximum, we can see the LED is flickering very quickly, and if I slide the potentiometer all the way down, we can see the LED is flickering very slowly. slider pot with the first ID switch set to one this is a list of four ones

I set the minimum as larger than the maximum because this actually represents the delay but I want to use the knob as a flash speed. So, at the maximum scale we want the flash speed to be fastest and so I have the smallest delay as the maximum. Now it's possible to connect multiple PiicoDev potentiometers together to create more complex projects. All they require is a unique setting for that ID switch that's located on the back of the device.

Here I have my rotary pot connected with a slider pot. The rotary pot I've left in that default configuration at the start of the tutorial, that's where all the switches are off. On the slider pot I have set the first ID switch to a one and these each control a parameter of this RGB LED module. Here you can see we have a blue light. If I move the slider pot that is my dimmer knob so I can turn the brightness down and if I turn my rotary pot that is actually a color selector. So, here we're at about cyan in the middle and as I turn that to the left we go through yellow, orange and red. And if I turn it to the right we go through a deeper blue, through purple and then onto magenta and back to red.

To talk to both potentiometers separately with our code, we're going to need to include those ID arguments in our initialization. Here's what that looks like: we have typical picotiv imports and then we have two instances of PiicoDev potentiometer. Here's one for the color controlling pot and here's one for the brightness controlling pot. We can set the minimum and maximum scale parameters in the initialization but the important part here is the ID argument. This is a list of those ID switch positions and so for the rotary color controlling potentiometer with all its ID switches off, this is a list of four zeros. For the slider pot with the first ID switch set to one, this is a list of four ones.

We have two potentiometers in code and we can refer to them by their names. With the first ID switch on, the first element is set to a one to indicate that switch is in the on position. In the infinite loop, we sample color dot value and assign that to C, sample brightness dot value and assign that to B. Using some of the RGB LED functions, we extract a color and fill the LEDs with that color. We then set the brightness to be that brightness parameter. With just 20-24 lines of code, we have this code that's able to sample two different parts independently and control the brightness and color of some PiicoDev RGB LEDs.

We were able to connect to a PiicoDev potentiometer and read its value, change the scale to more convenient values, and do a little bit of code remix to control some real hardware using the potentiometer. As a way to go from here, a nice RGB LED dimmer and color selector project that you might want to continue with. If you make something cool from these startup projects 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.