Rotary Encoder Tutorial with Arduino Code

Поделиться
HTML-код
  • Опубликовано: 25 апр 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
  • НаукаНаука

Комментарии • 288

  • @michaelhawthorne8696
    @michaelhawthorne8696 10 лет назад

    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.

  • @samfosteriam
    @samfosteriam 8 лет назад

    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!

  • @avluis86
    @avluis86 9 лет назад

    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!

  • @makinbacon21
    @makinbacon21 2 года назад

    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.

  • @ewncilo
    @ewncilo 5 лет назад

    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

  • @pauljarvey
    @pauljarvey 11 лет назад

    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!

  • @GoChrisGo_xyz
    @GoChrisGo_xyz 6 лет назад

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

  • @danskifpv
    @danskifpv 9 лет назад

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

  • @dannymcc777
    @dannymcc777 11 лет назад

    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!

  • @sicklebrick
    @sicklebrick 9 лет назад

    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 :)

  • @vladstrulev
    @vladstrulev 9 лет назад

    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 9 лет назад +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 😉

  • @LearningToFly77
    @LearningToFly77 9 лет назад +2

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

  • @RicardoPenders
    @RicardoPenders 3 года назад

    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.

  • @17GigaHertz
    @17GigaHertz 8 лет назад

    Man that circuit helped so much! Thank you!

  • @meistervision
    @meistervision 11 лет назад

    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.

  • @Prism019
    @Prism019 9 лет назад

    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!!!!

  • @MrNess2911
    @MrNess2911 5 лет назад

    Very clever way to read encoder states. Great explanation!

  • @SamnissArandeen
    @SamnissArandeen 7 лет назад

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

  • @frankleonardo1291
    @frankleonardo1291 5 лет назад +1

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

    • @geoptus
      @geoptus 3 года назад

      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!) 👍👍

  • @hanklock1994
    @hanklock1994 9 лет назад

    Thank You! for making this very easy to learn.

  • @GaRbAllZ
    @GaRbAllZ 11 лет назад

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

  • @Kevindarrah
    @Kevindarrah  12 лет назад

    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

  • @StoneFlange
    @StoneFlange 9 лет назад

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

  • @michaelmeglasson1428
    @michaelmeglasson1428 9 лет назад

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

  • @cruepprich
    @cruepprich 8 лет назад +3

    Excellent explanation. Thanks!

  • @chrishantharajasekara3235
    @chrishantharajasekara3235 9 лет назад +4

    thanks man...a great explanation with code.

  • @LJTobek
    @LJTobek 10 лет назад +1

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

  • @ronyholm
    @ronyholm 10 лет назад

    This video is king, really helpful! Thanks!

  • @Kevindarrah
    @Kevindarrah  11 лет назад

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

  • @naboulsikhalid7763
    @naboulsikhalid7763 4 года назад

    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.

  • @Kevindarrah
    @Kevindarrah  12 лет назад

    interesting concept! thanks for the info

  • @ctbully
    @ctbully 5 лет назад

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

  • @paulbreen911
    @paulbreen911 10 лет назад +4

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

  • @claudiomoles
    @claudiomoles 7 лет назад

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

  • @Kevindarrah
    @Kevindarrah  11 лет назад

    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

  • @MajorGray3
    @MajorGray3 11 лет назад

    Again, this was very helpful. Thank you.

  • @lopezrubiozajhamdanael4766
    @lopezrubiozajhamdanael4766 7 лет назад

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

  • @davidedemarco.mp3
    @davidedemarco.mp3 2 года назад

    Really helpful, thank you dude!

  • @brianh21858
    @brianh21858 9 лет назад

    Great description, thanks very much

  • @Kevindarrah
    @Kevindarrah  11 лет назад

    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.

  • @Kevindarrah
    @Kevindarrah  11 лет назад

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

  • @javiers.8274
    @javiers.8274 4 года назад

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

  • @RJMaker
    @RJMaker 8 лет назад

    Fantastic video!

  • @Kevindarrah
    @Kevindarrah  11 лет назад

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

  • @CristianGantner
    @CristianGantner 12 лет назад

    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.

  • @otasty7991
    @otasty7991 11 лет назад

    Great explanation. Thanks for the video.

  • @Kevindarrah
    @Kevindarrah  12 лет назад

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

  • @Bob3519
    @Bob3519 10 лет назад

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

  • @Kevindarrah
    @Kevindarrah  12 лет назад

    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  12 лет назад

    thanks!! more coming....

  • @Cmdrlucky8
    @Cmdrlucky8 11 лет назад

    Amazing! Thanks for sharing your knowledge.

  • @Kevindarrah
    @Kevindarrah  11 лет назад

    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

  • @jasio83
    @jasio83 11 лет назад

    Very useful and really well done. Thanks!

  • @Kevindarrah
    @Kevindarrah  11 лет назад

    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

  • @Kevindarrah
    @Kevindarrah  12 лет назад

    yes!! glad it helped!

  • @Kevindarrah
    @Kevindarrah  11 лет назад

    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!

  • @sato4kaiba
    @sato4kaiba 9 лет назад

    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.

  • @pixithegreenpowerranger1690
    @pixithegreenpowerranger1690 4 года назад

    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

  • @dirtdiamond360
    @dirtdiamond360 10 лет назад

    amazing video thank you for sharing this

  • @Kevindarrah
    @Kevindarrah  11 лет назад

    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

  • @lukasmorger
    @lukasmorger 10 лет назад

    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?

  • @swalha1999
    @swalha1999 6 лет назад

    Thanks man great code

  • @iwestbury
    @iwestbury 9 лет назад +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 8 лет назад

      +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.

  • @Kevindarrah
    @Kevindarrah  11 лет назад

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

  • @iFxSnake1
    @iFxSnake1 10 лет назад

    Very informative and helpful, thanks

  • @256k_
    @256k_ 8 лет назад +1

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

  • @Kevindarrah
    @Kevindarrah  11 лет назад

    thank you!!

  • @Kevindarrah
    @Kevindarrah  11 лет назад

    awesome thanks!!

  • @CTTrafo
    @CTTrafo 11 лет назад

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

  • @Kevindarrah
    @Kevindarrah  11 лет назад

    thank you!

  • @gdelospalotes
    @gdelospalotes 9 лет назад

    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  11 лет назад

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

  • @R2D2internet
    @R2D2internet 11 лет назад

    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!

  • @Jhonathanjw
    @Jhonathanjw 11 лет назад

    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

  • @rcpowres
    @rcpowres 4 года назад

    Great !!!! Thumbs up! Subscribed!
    Thanks a lot!!!!

  • @RTROME
    @RTROME 11 лет назад

    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..?

  • @cffellows
    @cffellows 11 лет назад

    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?

  • @zinkws
    @zinkws 12 лет назад

    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....

  • @12325833
    @12325833 11 лет назад

    Thanks! thanks for a great tutorial!

  • @Rodfraeser
    @Rodfraeser 11 лет назад

    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?

  • @Kevindarrah
    @Kevindarrah  11 лет назад

    thanks!! and remember this is just one way to do it

  • @dzolotas
    @dzolotas 11 лет назад

    Small gain indeed, just you can't loose a step at the beginning by using default initial values for the encoder. Thanks for the reply!

  • @szilagyiraul
    @szilagyiraul 11 лет назад

    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?

  • @BuildaMachine87
    @BuildaMachine87 11 лет назад

    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,

  • @Kevindarrah
    @Kevindarrah  12 лет назад

    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

  • @Kevindarrah
    @Kevindarrah  11 лет назад

    yep, a 1u would definitely be too much, but you still made a great observation!

  • @edisongsoares
    @edisongsoares 8 лет назад

    Este texto está em português: Parabéns. Fantástica aula. Muito obrigado!!

  • @violeman
    @violeman 11 лет назад

    Rotary Encoders do the trick for sure!
    A lot of people forget to add their last position to their next, where they want it. So that was awesome that you explained that to people!
    I am just starting to get into arduino & applications like the "Sentry Gun Project" & it is nice that now us Home users, can buy an affordable Micro Controller which has all kinds of info & programs
    Can you program the Rotery Encoder to always return to a zero position or is that just a wast of time?
    Thanks

  • @tom95076
    @tom95076 11 лет назад

    I work on semiconductor equipment that use encoders but the manuals are the worst a/b encoder info. Our track tool basically reverse until the hits a near home and then home flag sensor, then encodes fwd from that point. Thanks for your comment while your instructing. Another great video and great subject coverage. Q? what is the processing program you were showing the sweeping of the line on? I didn't know stuff like that can be done on the Arduino program. Is it a separate program?...

  • @Kevindarrah
    @Kevindarrah  11 лет назад

    Thanks!!

  • @srdjancervenjak8337
    @srdjancervenjak8337 5 лет назад

    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  12 лет назад

    yea, that would've been cool to do for the video!

  • @larrypoorman2300
    @larrypoorman2300 6 лет назад

    Thanks Kevin!!!

  • @jayanandareymon4853
    @jayanandareymon4853 10 лет назад

    it is very clear. thanks you sir

  • @ericpage
    @ericpage 7 лет назад

    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?

  • @jeffbeck6501
    @jeffbeck6501 6 лет назад

    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

  • @mac_uk5464
    @mac_uk5464 5 лет назад

    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 12 лет назад

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

  • @Kevindarrah
    @Kevindarrah  11 лет назад

    exactly, you would have to poll the inputs, which would use timer interrupts. personally, I like the way its done here in the video....