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 dimmers and audio amplifiers. I'm going to show you how to get started with a PiicoDev potentiometer and a Micro:bit.

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 of course need a Micro:bit V2, a PiicoDev potentiometer, either the rotary or the slide, they're both the same, a PiicoDev adapter for micro bit, and a PiicoDev cable. Plug the Micro:bit into the adapter, making sure the buttons are facing up. Connect the PiicoDev cable to one of the connections, and connect the other end to your potentiometer. I'm going to use the rotary one for now. And then connect to your computer with a USB cable.

We need to grab the driver files. In the download section for the article, right click PiicoDev Unified and Save Link As. And do the same for PiicoDev Potentiometer. Save Link As. And I'm saving these to a PiicoDev directory in My Documents. And here's those files. We're ready to go. the script again and now we can see that the values are ranging from negative one to one.

We are going to use Thonny to program our Micro:bit today. If you have never programmed a Micro:bit using Thonny before, refer to the article for more help. I have opened Thonny, connected to my micro bit, and found the two files that I have downloaded. Select each file and upload them to the micro bit. You should then see them in the file pane.

Return to the guide and find the first example, reading values and changing the scale. Copy all of that code and paste it into a new script in Thonny. I will save this script to my Micro:bit as main.py. Let's give the script a run. Click the green run button and we ought to see some values being printed into the shell. Here I am printing about 93. As I turn the knob on the pot, we can see that value and the plot is changing, corresponding to the position of the knob. In fact, all the way to the left is zero and all the way to the right is 100.

Now if you are working with the slider pot instead, I will unplug my rotary pot, plug in the slider, and when we rerun the script, we ought to see that it works in exactly the same way. Cool. Let's take a look at the code. We start by importing the PiicoDev potentiometer driver and a sleep function to create a delay. We call the initialization function, which returns a potentiometer object, and we assign that to pot. Next, we set the maximum and minimum properties. This is what defines the values at the ends of travel for our potentiometer. Here we are setting maximum to 100 and minimum to zero.

Then in an infinite loop, we are reading pot dot value, which returns the number and we are printing that to the shell. Finally, there is a short delay. Since we have access to these properties, we can change the value that the potentiometer will return. We can change the scale. I can set this to negative one and one. We will run the script again and now we can see that the values are ranging from negative one to one.

We've changed our scale to between negative one and one. When the potentiometer is all the way to the left, we're reading positive one and all the way to the right, we're reading negative one. These names maximum and minimum don't refer to the value, they refer to the value that's returned at the minimum and maximum end of the mechanical scale. So since maximum is negative one, we're returning negative one when the knob is turned all the way to the right. We get a linear scale all the way down to the minimum of the travel where we return positive one.

We often see potentiometers on electronic musical equipment, and the Micro:bit is no different. It has an onboard buzzer that we can create a tone with. We can modify the code to control the pitch of the buzzer using the potentiometer. We'll import music to give us capabilities to drive the buzzer. We'll use the potentiometer as a tone knob. We'll make the maximum frequency 440 Hz and use the knob to change the pitch of the buzzer. For the minimum, we'll pass it a value of negative one. This will make the buzzer silent. We have to sample the pot to get the frequency. We'll create a variable called "frequency" and set it equal to the pot value multiplied by 440. This will give us a range of frequencies from negative one to 440. other that controls brightness.

We can sample the potentiometer for a frequency between zero and 440 hertz. To do this, we call music.pitch and pass in the frequency. We round the frequency to the nearest whole number and print the frequency variable with the Hz unit. We can also read from two potentiometers at once. I've created a simple lighting project with two potentiometers and a PiicoDev RGB LED module. The right potentiometer controls the brightness and the rotary potentiometer is controlling the color. To achieve this, we need to give the second potentiometer a unique ID. In our code, we have two instances of PiicoDev potentiometer - one that controls color and the other that controls brightness.

We can use a potentiometer to control the brightness of an LED. In this case, I left the potentiometer in the default ID configuration and passed it an ID with all zeros. This means that all the switches are off. For the brightness controlling potentiometer, its first ID switch is on. Therefore, in the ID argument, we set the first entry to a one to indicate that switch is on.

Now we have two separate potentiometers that we can talk to independently to get two different values. We sample the color value, we sample the brightness value, and then we fill the LEDs with the color that we sampled and set the brightness to the brightness that we sampled. This is how we can combine two potentiometers to create a more complex project, such as a color and brightness selector.

We were able to connect a PiicoDev potentiometer and read a value from it. We were also able to change the scale so that we can get more convenient data from the potentiometer. We even did a code remix where we made a simple musical instrument or a color fader using multiple potentiometers.

If you make something cool from these starter projects or just have some questions, let us know on the forums. We're full-time makers and happy to help. 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.