How To Use A Rotary Encoder With Raspberry Pi Pico

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
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 picker 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 are turned together 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 [Music] 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 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 this 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 labeled differently instead of S1 your PIN might be labeled a instead of S2 it might be labeled b instead of ground it might be labeled at C and instead of key it might be labeled 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 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 encoder ground pin do the same for the power run the S1 or a pin on the encoder to the gpio pin 15 on the Picker run the S2 or B pin to gpio 14 on the Big O finally run the key or SW pin to gpio12 on the Pico note 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 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 micro python 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 funny and open that folder in the Sony file manager connect your pico to the computer and upload the files to the Picker in funny head to the first example and copy the code make a new file in funny 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 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 micro python rotary then 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 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 and 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 and 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 pick or 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 times after we create the rotary instance we create a new button we give it the pin number for the button on the module and then inside our while loop you may notice that the rotary code is still exactly the same but before it we've inserted a few new lines what we're doing is 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 as mentioned we do a little sleep to prevent it from firing repeatedly and that's it for the button the reason it works is because it's right before the next rotary value read so if the rotary is at say 250 at the start of this Loop we reset it to zero the new value gets red at zero and then it immediately fires current value which was 250 is no longer zero change print out zero so where to from here rotary encoders are used everywhere you'll find them in mice you'll find them in microwaves you'll find them everywhere so why don't you 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 [Music]
Info
Channel: Core Electronics
Views: 17,360
Rating: undefined out of 5
Keywords: How To Use A Rotary Encoder, What Is A Rotary Encoder, Rotary Encoder, Using A Rotary Encoder With Raspberry Pi Pico, How To Choose A Rotary Encoder, Alternative To Potentiometer, Difference Between Rotary Encoder And Potentiometer
Id: ixf6pT90ev0
Channel Id: undefined
Length: 9min 58sec (598 seconds)
Published: Mon Apr 03 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.