In this guide, we'll learn what a Rotary Encoder is, when and why we might use a Rotary Encoder, and how to use a Rotary Encoder with a Raspberry Pi Pico.


Rotary encoders are a great device for giving human control to hardware. They work by outputting pulse signals when a shaft is rotated, enabling us to measure rotary movements using a microcontroller like the Raspberry Pi Pico, they can be rotated infinitely and often have a tactile feedback, that's the feeling of those pulse signals being generated. They also come with a built-in push button, making them useful for select and submit controls, just like this hardware-based menu.

As you can see, as I rotate this knob, the menu items move up and down. Pressing the button enters the menu. This is a very common control device found on many electrical appliances.

I like to think of the rotary encoder as the underdog of things that turn. Nearly everyone that has played with electronics is probably familiar with the trusty potentiometer, but in many cases the rotary encoder can be a superior choice. I've already shown that they are great for building interactive menus, but they are also handy for other things like if you need fine-grained control, you want to change volume or brightness, or if you just want your project to be fancy with that bumpy feedback and even RGB lighting.

As cool as rotary encoders are though, there are still times when you might turn to something else such as the potentiometer. For example, if you're building an analog circuit, if you need to directly vary resistance, that is what a variable resistor is for after all, or you just don't need all those bells and whistles. Let's face it, sometimes a simpler option will do just fine.

But if you're like me, you're probably still watching this because you want to play with something cool. So how do you go about choosing a rotary encoder? The most basic rotary encoder will have at minimum three pins and the fundamental function of encoding the rotation of its shaft. From there, you can decide what features you need.

They can include a combination of different features, some of which will add more pins. You can get them with or without push buttons. If you're building a menu, make sure you get one with a button. They can have built-in lighting, which is useful for adding a visual element to the status of your controls. You can also find encoders that do not have the bumpy feedback feel. You might want a super smooth experience and they come in different resolutions, which is how many steps per rotation the encoder can detect. A higher resolution will give you a finer grain control, but will likely come with a higher price tag.

On top of that, if you're new to rotary encoders, I recommend getting one that comes on a breakout board or as a module on their own. Rotary encoders are unfortunately not very breadboard friendly, so we've picked the right rotary encoder for our needs. Let's look at how easy it is to use with the Picker. To follow along, all you'll need is a Raspberry Pi Pico, a rotary encoder, and a way to connect them together. I'm going to use a breadboard and some hookup wire. I'm also going to use this rotary encoder module, which is linked in the guide if you want to get one yourself.

Open up the guide, which is linked in the description, and scroll to the section on how to connect the encoder to the Picker. If you're using a different encoder than this module, the pins might be labelled differently. Instead of S1, your pin might be labelled A. Instead of S2, it might be labelled B. Instead of ground, it might be labelled C. And instead of key, it might be labelled SW for switch.

Place the Raspberry Pi Picker in the breadboard, making sure that the Gully is between the pins so that they do not short. Next, place the rotary encoder on the breadboard. I'm placing it in the top left corner so that I have plenty of room to run some wires. Echo ground pin and a breadboard to the rotary.

We need to connect the encoder to the Pico. Run the S1 or A pin on the encoder to the GPIO pin 15 on the Pico. Run the S2 or B pin to GPIO 14 on the Big O. Finally, run the key or SW pin to GPIO 12 on the Pico. Note that any of the GPIO pins on the Pico will work; I've just picked these three for easy breadboard placement. All going well, your breadboard should look something like this.

Now let's plug the Pico in and move on to the next section. Note that the order of the A and B or SW and S2 pins isn't strict; swapping them around will just mean that the direction of change gets inverted in the code.

Now that we've got everything connected, we need to download the MicroPython rotary Library. Unfortunately, this Library isn't available in Phony's package manager, but I've attached the files to the guide, so head there and download them. Download the files and extract them somewhere safe on your computer, and then open up Thonny and open that folder in the Sony file manager. Connect your Pico to the computer and upload the files to the Pico in Funny. Head to the first example and copy the code. Make a new file in Thonny and save the file to your Pico. Make sure you can see the console output and hit run.

Now let's turn that encoder. We look at that we're getting values; you can see as I turn the encoder clockwise the values go up, and when I turn it anti-clockwise the values go down. You may also notice that each time you feel that thump inside the encoder, a new value appears; that's the pulse inside the encoder sending out a signal to the microcontroller that a new Step has occurred.

If you're using a different product and you get weird results, you may need to look at tweaking the D bouncing parameters of the rotary Library. Check the resources in the guide and let's have a look at how the code works. First, we import the rotary Library. Note that the class name is Rotary.

Arq, even though the library's name is MicroPython Rotary, we instantiate a new instance of the rotary glass. We pass it the two pins that our A and B or SW and S2 pins are connected to. Then, we create a new tracking variable. We're going to use this to track the last known value of the encoder. This will also help prevent from spamming the console as the microcontroller loops really quickly.

Then, we start a new loop and start reading the rotary value at the start of every loop. In the next line, we're checking if the new value is the same as the last one. If it's the same as the last loop, then we don't do anything. But if it's different, we do something with it which I'm doing right here. I'm printing the new value to the shell. And then, as a final step, we update our tracking variable with the new variable so that the next time round this if statement has new data to work against.

That's it. It's pretty simple. Now let's remix this and get the push button working. Note, it's already wired in. We wired it in at the beginning of the guide. Head over to the remix section of the guide and copy the code in that example. Let's create a new file in funny and save it to the Picker. Don't forget to stop your picker if it's still running or you won't be able to save.

In this remix, we're going to use the push button to reset the encoder values. So let's run the script and then turn the encoder. Now press the button. We look at that. Let's reset the encoder down to zero. Now that was pretty simple.

Let's have a look at how it's working in the code. This example is built on the previous example, so much of the code is the same. We've imported pin from machine because now we're going to be reading from one of the pins. And we import the time library as well because we need to add a small delay after we press the button to stop it from firing multiple.

After we create the rotary instance, we create a new button. We give it the pin number for the button on the module. Inside our while loop, the rotary code is still exactly the same. However, before it, we've inserted a few new lines. We're checking if the button has been pressed. If it hasn't, this part gets skipped. If it has, we run the rotary.reset function. This resets the encoder's counter to zero and then we do a little sleep to prevent it from firing repeatedly.

The reason it works is because it's right before the next rotary value read. If the rotary is at say 250 at the start of this Loop, we reset it to zero. The new value gets read at zero and then it immediately fires current value which was 250 is no longer zero change print out zero.

Rotary encoders are used everywhere. You'll find them in mice, microwaves, and everywhere else. Have a think about where you could use the next in your next project or if you have any questions, head over to the forums and let us know. Until next time, happy mangy!



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.