How to use rotary encoders

Electronic knobs are everywhere, and today we will learn how they work and how you can use them with a microcontroller. We will even build our own rotary encoder at home. Thanks for watching, and let me know in the comments if you have any questions!
Companion article: www.friendlywire.com/tutorials...
If you enjoy these videos, consider supporting FriendlyWire on Patreon:
/ friendlywire
Email list: www.friendlywire.com/email
Website: www.friendlywire.com
Twitter: / friendlywire
Reddit: / friendlywire
Facebook: / friendlywire
Instagram: / friendlywire
Timestamps:
00:00 Introduction
00:47 How do rotary encoders work, and what is Gray code?
02:57 Inside a rotary encoder
04:22 Homemade rotary encoder
04:58 Idea for schematic
05:34 What you need to build this circuit yourself
05:59 Final schematic
06:43 Construction on the breadboard
08:25 Writing the program for the PIC16F1455
08:49 Writing the program: main ideas
12:39 Flashing the .hex-file onto the PIC16F1455
13:39 Using the homemade rotary encoder
13:52 Final thoughts

Пікірлер: 108

  • @0mdshuvo0
    @0mdshuvo07 ай бұрын

    This channel deserves wayyyy more subscribers

  • @FriendlyWire

    @FriendlyWire

    7 ай бұрын

    Thank you for your kind words, I am so glad you like the videos! :)

  • @kalvo-no7oq

    @kalvo-no7oq

    7 ай бұрын

    Yes this channel really deserves so more subscribers ❤🎉

  • @FriendlyWire

    @FriendlyWire

    7 ай бұрын

    @@kalvo-no7oq Thank you, too :)

  • @thespice311
    @thespice31125 күн бұрын

    This video is fantastic, very clear and efficient presentation with enough technical depth to get a strong understanding of why it works. I love the diy mega encoder!

  • @FriendlyWire

    @FriendlyWire

    24 күн бұрын

    Thank you for your kind words, I am glad you liked the video! And building the mega encoder was fun, too :)

  • @sebve9399
    @sebve93993 ай бұрын

    I love your demonstrations, they are very helpful for understanding!

  • @FriendlyWire

    @FriendlyWire

    3 ай бұрын

    Thank you so much, Sebastien, much appreciated :)

  • @orides5976
    @orides597611 ай бұрын

    Thank you for using PICs! I´m not feeling so alone anymore! 🙂

  • @FriendlyWire

    @FriendlyWire

    11 ай бұрын

    Ha, I'm glad you like them, too :) Which ones do you use?

  • @orides5976

    @orides5976

    11 ай бұрын

    @@FriendlyWire Currently i have been using the 16F1829 and 16F1824.

  • @FriendlyWire

    @FriendlyWire

    11 ай бұрын

    @@orides5976 I see! Any particular reason you like these chips? I quite like the PIC16F1455/59 because of their nice peripherals (as well as the internal 48MHz clock).

  • @usdigitalencoders
    @usdigitalencoders8 ай бұрын

    Great information. Your animations and style is also very fun and entertaining. Keep up the good work!

  • @FriendlyWire

    @FriendlyWire

    8 ай бұрын

    Thank you, glad you liked it! And your user name even has "digital encoders" in it, so it means a lot :)

  • @mohammedshareefalkaraan260
    @mohammedshareefalkaraan26020 күн бұрын

    Well you did it! I am now using a rotary encoder in my Project. Super Video ❤

  • @FriendlyWire

    @FriendlyWire

    15 күн бұрын

    Ha, that's awesome, Mohammed! Thank you for your kind words, and congratulations! :)

  • @DigitGarage
    @DigitGarage Жыл бұрын

    4:06 Thank You!!! A friend of mine has had this trick up their sleeve for a while… When I asked about it originally, they said it’s not worth putting the time and effort into. I completely forgot about it until now… though It all makes sense, they probably removed that copper spring. Once it’s gone though, the clicky steps can still be desired. THe realization at that point just replacing with a new one is probably the best option because the spring is long gone in the trash 😂

  • @FriendlyWire

    @FriendlyWire

    11 ай бұрын

    Haha yeah that spring wears out quickly on the cheaper encoders, and there is indeed some residual "bumpiness" that you can feel with the spring removed.

  • @mirkogiglioli1682
    @mirkogiglioli16823 ай бұрын

    Clever management of unexpected A&B status. As always, smart filters are the success key. Thanks

  • @FriendlyWire

    @FriendlyWire

    3 ай бұрын

    I would love to claim that I came up with the algorithm, but I did some research and found the one I presented in the video online :)

  • @audiofreq
    @audiofreq9 ай бұрын

    This was very helpful and I’ve learned a lot. Thank you!

  • @FriendlyWire

    @FriendlyWire

    9 ай бұрын

    Thank you so much, I am glad you liked the video!

  • @Adhithya2003
    @Adhithya20038 ай бұрын

    Brilliant video, extremely useful information for my projects❤

  • @FriendlyWire

    @FriendlyWire

    8 ай бұрын

    Thank you, Adhithya, glad you find the video useful! :)

  • @maxheadrom3088
    @maxheadrom30883 ай бұрын

    Beautiful wiring on those breadboards, man!

  • @FriendlyWire

    @FriendlyWire

    3 ай бұрын

    Thank you, glad you like it! It's a bit of a puzzle for me for every video to try and make it look neat, and I do enjoy the challenge, but ultimately I hope that it can make the videos more fun and easy to follow :) Have a great weekend!

  • @jerril42
    @jerril42 Жыл бұрын

    Thanks Jens. That was interesting. I really liked your supersized implementation of the encoder, I think that demonstrated the behaviour better than any I've seen. Take care.

  • @FriendlyWire

    @FriendlyWire

    Жыл бұрын

    Thank you, Jerry, glad you like it! :)

  • @user-vw5vq4oe2f

    @user-vw5vq4oe2f

    Жыл бұрын

    @@FriendlyWire Thank you so much for your hard work! thanks to your videos, I wrote my own library for displaying messages on the LED matrix running MAX7219 and it was your video that helped me in this, it was the most detailed and informative, thanks from Russia

  • @FriendlyWire

    @FriendlyWire

    Жыл бұрын

    @@user-vw5vq4oe2f Fantastic, I am glad you find the video helpful! :)

  • @davitberishvili8062
    @davitberishvili80623 ай бұрын

    This is nice explanation. Thank you for this video

  • @FriendlyWire

    @FriendlyWire

    3 ай бұрын

    Thank you for your nice comment, I am glad you found the video helpful! :)

  • @samsara2024
    @samsara2024 Жыл бұрын

    Super cool video! Thanks! :)

  • @FriendlyWire

    @FriendlyWire

    Жыл бұрын

    Thank you, glad you like it! :)

  • @jintopjoy9406
    @jintopjoy9406 Жыл бұрын

    Superb bro,explanation very well.❤

  • @FriendlyWire

    @FriendlyWire

    Жыл бұрын

    Thank you, glad you like it! :)

  • @markgreco1962
    @markgreco1962 Жыл бұрын

    Nice work

  • @FriendlyWire

    @FriendlyWire

    Жыл бұрын

    Thank you, Mark!

  • @ponsaravanan
    @ponsaravanan11 ай бұрын

    Well explained.

  • @FriendlyWire

    @FriendlyWire

    11 ай бұрын

    Thank you, glad you like the video! Have a great day :)

  • @DESX312
    @DESX3128 күн бұрын

    Wow this is really good. Subscribing!

  • @FriendlyWire

    @FriendlyWire

    7 күн бұрын

    Thank you, I am glad you liked it! What kind of video would you like to see next on here?

  • @ihadadream-itsgonenow
    @ihadadream-itsgonenowАй бұрын

    1st electronic "article" I looked at in 18 years. Immediately I new I was listening to a good lecturer. Thanks ! Well done. What I was looking for is a digital readout 0 to 360 degrees. The knob had to be attached to a pointer on a compass rose. I guess one needs to add BCD for 3.5 segment display. Can that pic do it ?

  • @FriendlyWire

    @FriendlyWire

    Ай бұрын

    Thank you, I am glad you liked the video! If you want to resolve the position in an absolute way then you need an "absolute" rotary encoder. These have more outputs than just A and B, of course, because they need to resolve the whole angular span. But I am not sure if that is always necessary: if you don't need the full 360 degrees, you could also use a potentiometer and measure the voltage at the wiper with a good analog-to-digital converter. Alternatively, you could use a gear with a 2:1 reduction to map 360 degrees to 180 degrees on the potentiometer. What is your target resolution for this project?

  • @ihadadream-itsgonenow

    @ihadadream-itsgonenow

    Ай бұрын

    @@FriendlyWire Thanks for answering. I have created a web app. It converts the shadow azimuth obtained from a shadow stick (to be your potentiometer turning "cylindrical knob") at center of a 0 to 360 degrees azimuth dial (azimuth angle is from 0 degrees to shadow) to an associated target azimuth. 0 degrees on the dial does not necessarily meaning north pointer but any required azimuth as I turn the cardboard dial I actually cut out a pointer above the 0 degrees It's an upsidedown marker or teardrop I WANT YOUR IDEA TO DISPLAY THE TARGET AZIMUTH AS YOU CHANGE THE SHADOW WITH THE POTENTIOMETER (OR OTHER SOLUTION OF YOURS) THEN YOU HAVE A SHADOW SUN COMPASS ! (make nice plastic toy with display) I have an improved web app that I call SHARKY SHADOWS for shark spotting (and flares and drowning people etc) I learnt long ago you cannot be a PC OR PHONE programmer AND a microprocessor programmer at the same time, but I can help anyone who attempts it with atleast some javascript guidance. OK, the pic cannot really do all the calculations, I guess.

  • @cornjulio4033
    @cornjulio403311 ай бұрын

    Grandioses Video! Hab endlich effektiv und präzise den Background dazu erlernt. Vielen Dank. PS: Ich schaue seit 15 Jahren solche Videos und hab noch nie diese "flimmernden" Leitungen gesehen (als Hervorhebung). Dafür müsstest Du eine Medallie bekommen ! Ich werde dieses Video als "Mahnung" verlinken, sobald ich von jemand anderem ein Video sehe wo z.B. aus einer schwarzen eine dunkelblaue Leitung wird (gleiche dicke, ohne Animation)......

  • @FriendlyWire

    @FriendlyWire

    10 ай бұрын

    Vielen Dank fuer Deine netten Worte, es freut mich sehr, dass Dir der Stil meiner Videos gefaellt. Es dauert relativ lange, diese Animationen zu zeichnen und dann zu animieren, aber ich glaube, dass es den Inhalt verstaendlicher machen kann. Viele Gruesse aus den USA!

  • @maxpenfold8699
    @maxpenfold86999 ай бұрын

    Sehr gutes Video

  • @FriendlyWire

    @FriendlyWire

    9 ай бұрын

    Vielen Dank! :)

  • @mohsenk5985
    @mohsenk59856 ай бұрын

    Merci à vous excellent...

  • @FriendlyWire

    @FriendlyWire

    6 ай бұрын

    Merci beaucoup! :)

  • @myshticaanimo2169
    @myshticaanimo21693 ай бұрын

    Nothing have to say❤️🔥 thanks🥲❣️ Sir you cleared all confusion

  • @FriendlyWire

    @FriendlyWire

    3 ай бұрын

    Thank you so much, I am glad you liked the video! :)

  • @Sarahbuildsstepsequencers
    @Sarahbuildsstepsequencers Жыл бұрын

    Hi Jens(?). I have a question. I’m wondering if you might consider breadboarding some sound projects? But with discreet components; no coding. I’d enjoy seeing other discrete component circuit projects. You’re doing a great job. So friendly and professional. You’re quite educated!

  • @FriendlyWire

    @FriendlyWire

    Жыл бұрын

    Thank you, Sarah, this is a great suggestion. I enjoy projects with discrete components (like the code lock) but they can get rather complex quickly. Do you have a good suggestion for a sound project? I don't know that much about it! (And thank you for your kind words!)

  • @tanjiro3285
    @tanjiro32859 ай бұрын

    neat explaination !!! instantly SUBSCRIBEEED .... nowadays more and more companies are switching to ARM based controllers so can u make some vids on stm32 with various sensors

  • @FriendlyWire

    @FriendlyWire

    9 ай бұрын

    Thank you so much, glad you liked the video! I may stick with PIC microcontrollers for a bit more, but I am curious about others, too, so: you never know, I may cover them at some point :)

  • @tanjiro3285

    @tanjiro3285

    9 ай бұрын

    @@FriendlyWire 👍

  • @kalvo-no7oq
    @kalvo-no7oq7 ай бұрын

    Thanx for this informative video ... Please can you make a video on hx710b pressure sensor 😅

  • @FriendlyWire

    @FriendlyWire

    7 ай бұрын

    Thank you for your feedback, I will look into the sensor you mentioned. :)

  • @gartmorn
    @gartmorn7 ай бұрын

    Great videos! Are you planning any new content? Your explanations are very user friendly! Hope for some new projects soon!

  • @FriendlyWire

    @FriendlyWire

    7 ай бұрын

    Actually, yes, there is a new video coming net weekend! :) And thank you so much!

  • @jozefsoucik3115
    @jozefsoucik31152 ай бұрын

    10:55 part about decoding direction deserves slower and more repetitions ...because it is the most important part of not understanding principle. But overall great video

  • @FriendlyWire

    @FriendlyWire

    2 ай бұрын

    Thank you for your feedback! :) Yes, it could use more time, I agree with you. Sometimes it's hard to decide for me how much detail to put in the video. For this reason I always write a companion article to each video, which usually has more details. You can check it out here: www.friendlywire.com/tutorials/rotary-encoder/#ch7 Let me know if it makes sense or if you have questions, I am happy to help!

  • @kogstig
    @kogstig Жыл бұрын

    Thank you for your videos and work from Russia!

  • @FriendlyWire

    @FriendlyWire

    Жыл бұрын

    Thank you for your kind words, Konstantin :)

  • @somedudewithakeyboard

    @somedudewithakeyboard

    Жыл бұрын

    @@FriendlyWire #russiaisaterroriststate BTW, thank them again. They just blew up a big hydroelectric power plant in Ukraine, causing a huge disaster - tens of thousands (!) of people will loose everything, some of them will drown or become MIA.

  • @maze4045
    @maze40458 ай бұрын

    What Program are you using to create the schematics?

  • @FriendlyWire

    @FriendlyWire

    8 ай бұрын

    I use Eagle for the schematics, it's free to use, but perhaps not the most user-friendly. I just kind of like the aesthetics :)

  • @irgski
    @irgski5 ай бұрын

    Does a rotary encoder need to be concerned about switch bounce?

  • @FriendlyWire

    @FriendlyWire

    5 ай бұрын

    I talk a little bit about it in the video, but the short answer is: no, because it uses Gray code, so the worst that can ever happen during a bounce is that you are "off by 1" but never more than that.

  • @zame2476
    @zame24763 ай бұрын

    Würde sowas auch ohne MCU funktionieren? Mittels Decoder IC der so

  • @FriendlyWire

    @FriendlyWire

    3 ай бұрын

    Ja, das sollte auch funktionieren, ein paar Flip Flops sollten reichen :)

  • @technixbul
    @technixbul Жыл бұрын

    I want to see 24bit shift register contolling 2x22 leds with 2 grids for stereo VU meter, where each grid correspond ti its analog input.

  • @FriendlyWire

    @FriendlyWire

    Жыл бұрын

    I have not used 24bit shift registers, but you could also use 3x8bit registers, like the TLC5916, for which I do have a tutorial on this channel :)

  • @technixbul

    @technixbul

    Жыл бұрын

    @@FriendlyWire Yup i did the same, I used (for testing) 3x CD4094 in daisy chain mode but I'm havng problems with timings on grids, can't fgure it out. If i manually set output bytes they apear proper but when i add the part with analog inputs, everything is flickering and shows garbage. Both parts work separate, when i use ports directly, analog part works perfect, and when the part with shift registers is alone. The problem comes when i have to send AN0 and AN1 variables through shift registers along with GRID positions. I am sure that many people would like to build a device like this. ;)

  • @FriendlyWire

    @FriendlyWire

    Жыл бұрын

    @@technixbulI am not sure why anything would flicker in this way, it shouldn't. Can you send me your code? My email is at the bottom of www.friendlywire.com :)

  • @technixbul

    @technixbul

    Жыл бұрын

    @@FriendlyWire I will, but firstly i need to gather all in one place, the functionality was expanded a lot and need to add comments.

  • @FriendlyWire

    @FriendlyWire

    Жыл бұрын

    @@technixbul Sounds like a plan! :)

  • @delta-a17
    @delta-a176 ай бұрын

    I don't know what the ISR is used for if this implementation is polling the rotary encoder at rest. Isn't an ISR usually called in response to input?

  • @FriendlyWire

    @FriendlyWire

    6 ай бұрын

    Good question! Lots of things can trigger the interrupt, in this case it's the overflowing timer. This makes sure it's polled at a consistent rate.

  • @delta-a17

    @delta-a17

    6 ай бұрын

    So the hardware timer hits it's maximum count value 3906 times a second? (I had to look up what an overflowing timer was haha) That must be counting extremely fast! Thanks for the response!@@FriendlyWire

  • @FriendlyWire

    @FriendlyWire

    6 ай бұрын

    @@delta-a17 Yes, you got it! I can break it down for you in some more detail. The code runs at 4 MHz, but due to the PIC architecture it means it can only process 1 million instructions per second. The timer is 8 bit wide, which means it can count from 0 to 255. If you divide 1,000,000 by 256 you get 3906. This means that an 8 bit timer, when driven at 1MHz, "flows over" around 3906 times per second :)

  • @delta-a17

    @delta-a17

    6 ай бұрын

    That makes perfect sense, thanks!@@FriendlyWire

  • @FriendlyWire

    @FriendlyWire

    6 ай бұрын

    ​@@delta-a17Glad I could help! :)

  • @tze-ven
    @tze-ven6 ай бұрын

    My advice is not to use MCLR pin as the input for rotary encoder SW. Instead use other free pin. The reason is because when programming the PIC, a high voltage (because you must have disabled LVP to allow MCLR as input) is delivered to MCLR and if you accidentally pressed the SW, it will be shorted to GND and might damage the PICKit3 MCLR output driver.

  • @FriendlyWire

    @FriendlyWire

    6 ай бұрын

    Good point! The MCLR pin is the only one left with an internal pullup resistor, which is why I used it. Since it's connected to the pushbutton, chances that it's pressed while being programmed are almost zero. But if you want to add external pullups you can use other pins too, of course.

  • @tze-ven

    @tze-ven

    6 ай бұрын

    ​@@FriendlyWire In that case I suggest putting a 600 Ohms resistor in between the PICKit3's MCLR connection and the rotary encoder's SW. This value is big enough to limit the current (~15mA max) if it is accidentally shorted when programming and is small enough for PIC to detect a low (

  • @engineershorts6954
    @engineershorts69548 ай бұрын

    Bro my rotary encoder give at both pin high at once cut time than for next cut time it's low why this please give

  • @FriendlyWire

    @FriendlyWire

    8 ай бұрын

    That is strange! So the outputs are either 00 or 11? That is definitely not Gray code. Do you have a link to that encoder? I would suspect that it may be broken.

  • @digital_madness-pp8cw
    @digital_madness-pp8cw Жыл бұрын

    I send you an email, no reply

  • @FriendlyWire

    @FriendlyWire

    Жыл бұрын

    I am sorry about that, I must have missed it. When did you send it? Unfortunately I get a lot of spam mail, and your message may have gone unnoticed.

  • @digital_madness-pp8cw

    @digital_madness-pp8cw

    Жыл бұрын

    @@FriendlyWire The title of the message was: "Help /627A"

  • @FriendlyWire

    @FriendlyWire

    Жыл бұрын

    @@digital_madness-pp8cw Saw the message, it was from 2 days ago. To answer your question: yes, you can change the code so that an input value of 0-5V can be sent to the computer via RS232. But writing such a program from scratch takes time. What kind of timing/resolution requirements do you have?

  • @digital_madness-pp8cw

    @digital_madness-pp8cw

    Жыл бұрын

    @@FriendlyWire The higher the resolution, the better. Please send if you have time, i already gave you my email.

  • @FriendlyWire

    @FriendlyWire

    Жыл бұрын

    @@digital_madness-pp8cw I am sorry, but I still don't understand exactly what you need. Can you explain it in more detail? And what kind of resolution do you need, both in voltage and in time? Some numbers would be good! :)

  • @LabSkaterPussies
    @LabSkaterPussies3 ай бұрын

    How do you know how many stops a rotary encoder has? Is this information on the part's data sheet somewhere? Do they come with different numbers of stops?

  • @LabSkaterPussies

    @LabSkaterPussies

    3 ай бұрын

    Never mind its called detents

  • @FriendlyWire

    @FriendlyWire

    3 ай бұрын

    Yeah they go by different names, and for cheap ones that don't have datasheets it's a bit of a guess, unfortunately. But commonly the situation with the detents/stops is similar to what is shown in the video.