Rotary Encoder Tutorial with Arduino Code

Sdílet
Vložit
  • čas přidán 25. 04. 2012
  • All code up on site and encoder used:
    www.kevindarrah.com/?page_id=1348
    Check out my Tindie store (trigBoard is available) www.tindie.com/stores/kdcircu...
    Thanks to all the Patrons for dropping a few bucks in the tip jar to help make these videos happen!
    / kdarrah
    Twitter: / kdcircuits
    For inquiries or design services:
    www.kdcircuits.com
  • Věda a technologie

Komentáře • 288

  • @frankleonardo1291
    @frankleonardo1291 Před 5 lety +1

    Almost 7 years later and people are still finding this tutorial excellent, Thanks Kevin!

    • @geoptus
      @geoptus Před 3 lety

      Yeah, Kevins tuts are truly awesome. I followed this tut about 6 years ago. It's still my 'go-to' when ppl ask me how to do this stuff (or when I forget!) 👍👍

  • @samfosteriam
    @samfosteriam Před 8 lety

    Really well explained - thank you for putting this up. You go over and over, pointing out the whats and whys, what works, what doesnt, how it works, where the gotchas are. Gold star!

  • @michaelhawthorne8696
    @michaelhawthorne8696 Před 10 lety

    That was a very clear explanation of this device, thank you Kevin.
    I haven't got one in my possession yet so I had trouble visualizing the workings.
    You managed to fit the jigsaw together for me.

  • @avluis86
    @avluis86 Před 9 lety

    This is awesome! I need to tap into an Auto A/C Control panel in my car which uses rotary encoders with an Arduino but I didn't even know where to start.
    Your explanation and examples are the best and really simplifies what I actually need to do to get my project done.
    Thanks you for posting this!

  • @ewncilo
    @ewncilo Před 5 lety

    oh my gosh, you are a beast, never in my life have i seen somebody improvising schematics with this easiness as if they were coding

  • @GoChrisGo_xyz
    @GoChrisGo_xyz Před 6 lety

    I have been looking for a well-explained tutorial on this for a while thank you so much!!! This was exactly what I needed.

  • @pauljarvey
    @pauljarvey Před 11 lety

    Kevin! This is the best explanation of interrupt-based encoder use on an Arduino that I've ever seen. Brilliant work. I used this code to set up a user input system using only a single encoder - and it works fantastically - responsive, quick, and easily customizable. I have the occasional glitch of a freeze-up when I spin the encoder too fast - but I'm sure I'll figure that out shortly! Just wanted to say thanks for the help that this video has been to me. Cheers!

  • @vladstrulev
    @vladstrulev Před 10 lety

    Thank you very much Kevin for such simple, but informative explantaion. I was afraid of touching encoders, thinkig they are very difficult to operate, but with your excellent explanation, I'm not afraid any more and I'm testing them as we speak. Thank you again!

  • @61barrackroad
    @61barrackroad Před 9 lety +1

    Thats really helpful thanks was a bit worried about figuring out how encoders work but your explaination simplies what could easily be quite a complex subject thanks 😉

  • @sicklebrick
    @sicklebrick Před 10 lety

    Thanks for taking the time to explain this so clearly and in such depth. I wasn't looking to use any rotary encoders, but I enjoyed the explanation :)

  • @danskifpv
    @danskifpv Před 9 lety

    Thanks for the video Kevin, very well explained and easy to understand the concepts from the way you put them.

  • @dannymcc777
    @dannymcc777 Před 11 lety

    You are awesome. This is going to help me in my senior project quite a bit. No one is stumping me on an encoder question during my presentation!

  • @LearningToFly77
    @LearningToFly77 Před 9 lety +2

    One of the best CZcamsr in this Universe :-) Thank You!

  • @RicardoPenders
    @RicardoPenders Před 3 lety

    I've got my rotary encoders from Banggood and the encoder is already mounted on a PCB and on the bottom side it contains all the components according to your schematic which is great because I can connect it directly to my Arduino, pretty neat this way because you can't do anything wrong and you don't have to think about the extra components and you don't have to check if it will work with the capacitor values as you had to do, it saves me a lot of time.

  • @paulbreen911
    @paulbreen911 Před 10 lety +4

    Oh the irony, a quick video, that's 26 minutes long, aha I love it!!

  • @Prism019
    @Prism019 Před 9 lety

    Thank you SO MUCH for this video! You were the only video I've seen that actually got through to me! Thank you, thank you, THANK YOU!!!!

  • @robosergTV
    @robosergTV Před 10 lety

    is it worth to buy hall effect encoders? Because the one you are showed is mechanical and will wear and break over time, if I am correct

  • @SamnissArandeen
    @SamnissArandeen Před 7 lety

    This would make the perfect Snake controller. Thanks for the code and schematics, good sir!

  • @Kevindarrah
    @Kevindarrah  Před 12 lety

    awesome question!! I would make some kind of selection circuit that listens for one of the encoders to rotate, then start a timer to 'wait' for that encoder to finish, then resume listening to them all. it would be a fairly complex scheme, guess thats a limitation of the arduino... only 2 interrupts

  • @makinbacon21
    @makinbacon21 Před 2 lety

    99% of the information on the web about rotary encoders is completely worthless to me--you provided an excellent tutorial and schematic that made it way easier and saved my project. Might be an almost-10-year-old video but still excellent.

  • @meistervision
    @meistervision Před 12 lety

    Thanks for a great tutorial... i was pulling my hair out trying to figure out how to work with this rotary encoder. Thanks for your time in putting this vid together.

  • @MrNess2911
    @MrNess2911 Před 6 lety

    Very clever way to read encoder states. Great explanation!

  • @Kevindarrah
    @Kevindarrah  Před 11 lety

    thanks! I soldered little headers to the encoder. Made it bread-board friendly

  • @Kevindarrah
    @Kevindarrah  Před 11 lety

    yep, good point. I could have talked about altering the time constant there a little. When I made this video, I don't think I had a clue about that

  • @17GigaHertz
    @17GigaHertz Před 8 lety

    Man that circuit helped so much! Thank you!

  • @iwestbury
    @iwestbury Před 10 lety +1

    Hi, this was a great tutorial for simplifying the mysteries of how to read encoders. However I was having a problem with my 100 ppr optical encoder. It would lock up when I spun it too fast. I tried everything until I eventually moved the serial write out of the ISR. Now it works brilliantly. Thanks, keep up the good work.

    • @dorianmccarthy7602
      @dorianmccarthy7602 Před 8 lety

      +Ian Westbury This is what i was thinking too. If you move the Serial.println(pulses) to the empty loop function, then using serial shouldnt interfere with the ISRs.

  • @chrishantharajasekara3235

    thanks man...a great explanation with code.

  • @naboulsikhalid7763
    @naboulsikhalid7763 Před 4 lety

    very well explained, through those signal diagrams, but I couldn't well understand the code related to those signals. Sure, I will walk through again and again until I get it. But, the first time I see a relation between the signals and the code and it's surely the best way. Thank you very much for sharing your awesome knowledge.

  • @StoneFlange
    @StoneFlange Před 9 lety

    I'm one step closer to nirvana because of your hardware debounce trick! Thanks :)

  • @Kevindarrah
    @Kevindarrah  Před 12 lety

    thanks!! there's more on the way...

  • @gdelospalotes
    @gdelospalotes Před 9 lety

    Thanks for your great explanation! I have one question though,in the case I have to measure spins and use the encoder just as an incremental one (I know it will only rotate in one direction), would you still recommend me to use this component?

  • @Kevindarrah
    @Kevindarrah  Před 11 lety

    would be very cool, I'd be interested to see how you pull this one off

  • @cruepprich
    @cruepprich Před 9 lety +3

    Excellent explanation. Thanks!

  • @Kevindarrah
    @Kevindarrah  Před 11 lety

    yes, you could attach an encoder to stepper motor. I think the arduino would be fine for a project like that. Teh only problem I see is potentially not having enough interrupt pins for all your encoders. You can always switch to polling your encoders, instead of interrupts

  • @LJTobek
    @LJTobek Před 10 lety +1

    Thx so much! You made a beginner understanding "everything" :D

  • @arayarock1
    @arayarock1 Před 8 lety +2

    how can i use a enconder with a stepper motor?

  • @ctbully
    @ctbully Před 5 lety

    Finally someone that actually gives useful code :-) Thank you.

  • @lukasmorger
    @lukasmorger Před 10 lety

    Thanks a lot, super video!
    I got one question:
    Where is your port definition?
    There is just pulses, and this isn't a declaration for the port, right?

  • @Kevindarrah
    @Kevindarrah  Před 11 lety

    hmmm, now you're talking! I've got a few cool ideas to do this, and now I'm thinking about ordering a few more encoders to experiment with.

  • @GaRbAllZ
    @GaRbAllZ Před 11 lety

    Very well done, you really did a great job explaining this in detail.
    Thanks

  • @michaelmeglasson1428
    @michaelmeglasson1428 Před 9 lety

    Thanks man. Great tutoial. Very clear and easy to understand.

  • @hanklock1994
    @hanklock1994 Před 9 lety

    Thank You! for making this very easy to learn.

  • @srdjancervenjak8337
    @srdjancervenjak8337 Před 6 lety

    can this also be done on a ATmega32U4? as i tried plugging the encoder pins in all of the arduinos pins and it did not register any encoder movement. I tried just the encoder with no resistors or capacitors and also the version with the capacitors and resistors and no reaction on.

  • @Kevindarrah
    @Kevindarrah  Před 12 lety

    interesting concept! thanks for the info

  • @Kevindarrah
    @Kevindarrah  Před 11 lety

    You will need a free running timer like millis() to compare the position to, from that you can determine rate of change, which will give you RPM

  • @Kevindarrah
    @Kevindarrah  Před 11 lety

    process the numbers on the arduino, then send up the distance. If you know how many degrees each step is, and you know your circumference, you can easily calc the distance

  • @256k_
    @256k_ Před 8 lety +1

    amazing video... thank you for explaining this so well! now i just need to know what are interupts :P

  • @Kevindarrah
    @Kevindarrah  Před 12 lety

    thanks!! more coming....

  • @ericpage
    @ericpage Před 7 lety

    Will this work is you run A and B at different kohm resistance so when you turn right it sends a different Kohm then when you turn left?

  • @ronyholm
    @ronyholm Před 10 lety

    This video is king, really helpful! Thanks!

  • @mac_uk5464
    @mac_uk5464 Před 5 lety

    Could you connect an encoder to a small chip like a tinny, & have that calculate position & then send the position to your main proc an uno etc.

  • @CristianGantner
    @CristianGantner Před 12 lety

    2 interrupts are the limit, but theoretically you could add more to the arduino. the chip supports more. You also could add pin a of one encoder ti int0 and the pin a of the second encoder to int1, so if int is changes you check pin b on the encoder and then you know in which direction you go. This would also need a debounce circuit.

  • @Kevindarrah
    @Kevindarrah  Před 11 lety

    very interesting... I would be very interested in this, I may try this out one day

  • @DanielsGameVault
    @DanielsGameVault Před 6 lety

    Why do you detach the interrupt immediately after reading it ?

  • @claudiomoles
    @claudiomoles Před 7 lety

    excellent, very good and objective explanation.
    Claudio from Brazil.

  • @javiers.8274
    @javiers.8274 Před 4 lety

    Have no idea what you did! But worked for me too, thanks!!

  • @sato4kaiba
    @sato4kaiba Před 9 lety

    In your diagram for the wiring of the encoder's A and B, adding the addition resistor (10K) and capacitor(0.1uF), you create an RC filter that attenuates the bouncing signals, aka switch bouncing.
    In your code, concerning the interrupts, there is little or no chance your going to miss a pulse. I can happen if the power supplied to the micro-controller begins to fall below the required voltage in cases where a battery may be used. The other method is Polling. In polling, which is using code to periodically check the output signals of the encoder, you can miss a pulse. The coding method use is a preferred way.

  • @lopezrubiozajhamdanael4766

    Your Video is amazing, thanks for shared this!!
    I have a project, and your program was help me.

  • @Kevindarrah
    @Kevindarrah  Před 12 lety

    that's awesome, I'm happy it helped!

  • @MajorGray3
    @MajorGray3 Před 11 lety

    Again, this was very helpful. Thank you.

  • @Kevindarrah
    @Kevindarrah  Před 12 lety

    hmmm, I would use mills() or micros() to to measure pulse widths to get velocity, and maybe even average the velocity... you just need to mark the time from mills() and the next transition time, and bingo! you're done! Glad the code worked for you!

  • @Kevindarrah
    @Kevindarrah  Před 12 lety

    haha, I know. sometimes it takes that long for me to get inspired to do anything, or in this case, I was waiting for my shopping cart at sparkfun to get filled up before checking out... always hate spending all that money for shipping on a part that cost 5 cents... I guess the geiger tube pushed it over

  • @heshamemara6490
    @heshamemara6490 Před 7 lety

    what is the software used to simulate the direction of the rotary encoder?

  • @szilagyiraul
    @szilagyiraul Před 11 lety

    Thanks for your video. I like your programing style but if you want 2 or 3 encoders and you only have 2 interupts on the board how do you proceed?

  • @Jhonathanjw
    @Jhonathanjw Před 11 lety

    Great!Thank you so much!!
    what do you think was wrong with mine? i put the correct capacitors and resistors and i tested that so the correct voltage 0 and 5v is on the wire

  • @jeffbeck6501
    @jeffbeck6501 Před 6 lety

    Also, I had to mod the code in my link below to only increment or decrement after ever 2 clicks and not one because the encoder's ridges make the encoder want to stop at every 2 clicks. It feels a lot better and way more natural

  • @otasty7991
    @otasty7991 Před 11 lety

    Great explanation. Thanks for the video.

  • @davidedemarco.mp3
    @davidedemarco.mp3 Před 2 lety

    Really helpful, thank you dude!

  • @cataex2418
    @cataex2418 Před 7 lety

    i have a question, when i have the arduino connected to the pc , when i run the processing code i get an error that com3 is already in use , could you give any tips on how to solve this

  • @Kevindarrah
    @Kevindarrah  Před 11 lety

    yes it is a separate program called 'processing' which is similar to arduino, but more on the software side of things. It is really cool, and free!

  • @JoanMelchor
    @JoanMelchor Před 7 lety

    Hi, how would you do the same but for a 360 pulse encoder?

  • @brianh21858
    @brianh21858 Před 9 lety

    Great description, thanks very much

  • @pixithegreenpowerranger1690

    Dude ur a freakin legend!
    What a great video, and thank you so much for explaining it all in a way that dummies like me can understand. :D

  • @Rodfraeser
    @Rodfraeser Před 11 lety

    Hi, Good tutorial you made.
    Is it possible to use a small stepper motor instead of the rotary encoder?
    I thinking to use a old tiny steppermotor for handwheel for my cnc machine,
    do you suggest to use Arduino for the project? or?

  • @RTROME
    @RTROME Před 11 lety

    Great Video, Thanks, explained a lot I was wondering about. I had One very basic question on your code; I do seem to see where you Identify the A and B encoder pins called out for the Arduino (what IO) Pins did you use..?

  • @Bob3519
    @Bob3519 Před 10 lety

    Great job. I'm working on a project and I'm going to use an encoder. Thanks for the info.

  • @CameronMoss
    @CameronMoss Před 10 lety

    Is there any way of sending control changes using usbMIDI? I am using encoders with the Teensy3.0 and trying to program encoders for a DIY MIDI controller. How would I implement this with your code?

  • @jimmysalgado7001
    @jimmysalgado7001 Před 6 lety

    HI, I can't seem to be able to adapt your code for a esp32, have you tried it in that device? great video btw.

  • @RJMaker
    @RJMaker Před 8 lety

    Fantastic video!

  • @BuildaMachine87
    @BuildaMachine87 Před 11 lety

    Great and easily understandable tutorial, though I am wondering, how did you secure the encoder to your breadboard? was it like an IC mount, or something?
    Thanks,

  • @johnlongs836
    @johnlongs836 Před 7 lety

    can i use this pot. for making steering wheel ??

  • @CTTrafo
    @CTTrafo Před 11 lety

    Muchas gracias, era lo que buscaba por meses. You are amazing!!!.

  • @cffellows
    @cffellows Před 11 lety

    Thanks for your videos, Kevin, very helpful. Quick question. Does the larger capacitor of 0.1 uf have an adverse affect on frequency? Suppose I'm using an Encoder with 4096 Pulses Per Revolution and I'm running it at 2,000 RPM?

  • @yamenhazimi2947
    @yamenhazimi2947 Před 6 lety

    how can i use encoder to determine angular speed??
    any idea?? thanks

  • @zinkws
    @zinkws Před 12 lety

    Very nice presentation. I really enjoy this video. The aproach that you used in your code is very good. I am porting it right now to another C compiler because I play with PICs, not the arduino.
    But I have one question. First the scenario: I have a box with water inside. This box have a wheel that spin write (just a line) on a paper the level of the fluid inside.
    So I need to digitalize this box/machine. First I thought about encoders and came here and watch your video. OK.....
    BUT @24:29....

  • @ifrashable
    @ifrashable Před 9 lety

    how does this thing work on mouse wheel?, i used to disassemble some mouse, some have a IR transmitter and receiver, and a wheel with tiny gaps. its seem have only encode one signal. how does it determine direction of rotation?

    • @AdrianStaicu82
      @AdrianStaicu82 Před 9 lety +1

      The receiver has two areas where it can detect the light. While moving the wheel, light and shadow created by the tiny gaps, will create a sequence identical to AB encoding diagram.

  • @matthewanderson6070
    @matthewanderson6070 Před 11 lety

    what is the max value a rotary encoder can go up to? I am trying to use a rotary encoder to measure a lot of distance.

  • @Cmdrlucky8
    @Cmdrlucky8 Před 11 lety

    Amazing! Thanks for sharing your knowledge.

  • @Kevindarrah
    @Kevindarrah  Před 11 lety

    ha!! now that would be cool! I was thinking about making little daughter boards that work with these encoders?

  • @Kevindarrah
    @Kevindarrah  Před 11 lety

    yea, you could, not sure what that buys you, the encoder is still incremental, since it could have turned while power was off

  • @CristianGantner
    @CristianGantner Před 12 lety

    Hi, very informative. What if i need 2 or 3 encoders? How do i write the code for the interrupts in arduino? Thank you.

  • @mikezter1
    @mikezter1 Před 10 lety

    Is it really necessary to constantly detach and reattach the interrupt triggers? Why not register them all in setup()

  • @tejsw
    @tejsw Před 11 lety

    Thanks for the wonderfull info but wy are u not incorporating Z - pulse...can u help me in analyzing that

  • @R2D2internet
    @R2D2internet Před 11 lety

    With two D type flip-flops you can get a single pulse on one wire if you turned one step CW, and one pulse on other wire if you turned one step CCW.
    This saves code and time I think!

  • @Kevindarrah
    @Kevindarrah  Před 11 lety

    I beleive those are the two interrupt pins. Doesn't matter which. Think it is arduino 2 and 3?

  • @ixamraxi
    @ixamraxi Před 9 lety

    Seems like you could say any time a state changes so that the outputs become the same (1,1 or 0,0) , you are traveling in one direction, and whenever a state changea so that the outputs become different (1, 0 or 0, 1), you are traveling in the opposite direction.

  • @jasio83
    @jasio83 Před 12 lety

    Very useful and really well done. Thanks!

  • @LHP-Engineering
    @LHP-Engineering Před 9 lety

    sorry i need an info...does this encoder do strong resistance or is it easy turnable?

  • @coffee24seven
    @coffee24seven Před 10 lety

    Awesome, really appreciate this. I do have one question though. I got your code running fine, but the program freezes up when I spin the rotary encoder too fast. Any idea on why that is?

  • @vitalin.8581
    @vitalin.8581 Před 7 lety

    I found out that in a small program it works pefectly, but when I try to use encoder in a bigger program, where micro delays happen to be, then the encoder just lags. I made it to show a number of encoder into monitor when it changes the state. But when I rotate it, it shows a new line in serial monitor, but the number of it does not change (it wanted to change but it did not). however if I rotate it very slowly it works and changes. But I do not use any interrupts. Would the interrupts solve this problem? I am sorry I am newbie and I am trying to understand the interrupts logics and rules and it is hard yet.

    • @Kevindarrah
      @Kevindarrah  Před 7 lety +1

      yea, I think I may revisit this old project... I think if I were to do it again, I would have a timer running that sampled the pins