How to use encoders (Optical, Hall Effect, Quadrature)

Sdílet
Vložit
  • čas přidán 20. 06. 2024
  • An explanation of how optical, Hall Effect, single-phase, and quadrature encoders operate along with sample code to count the number of pulses, RPM, and direction of rotation.
    Support the creation of more content like this: www.buymeacoffee.com/WillDona...
    Components used in this video (affiliate links):
    Yellow TT motors and L298N motor driver: amzn.to/3zj1E5X
    Encoder disks for TT motor: amzn.to/32RZ7Ua
    N20 gear motor: amzn.to/3qEuiLd (there are different gear ratios available)
    Optical quadrature encoder: amzn.to/3JAjaHG (not the exact same part as in the video, but almost functionally identical)
    Infrared emitter and receiver: amzn.to/32R2JGj (not the exact same part as in the video, but almost functionally identical)
    Breadboard: amzn.to/3qCuLxu
    Jumper wires: amzn.to/3zoMvAo
    Arduino Uno: amzn.to/30WTweL
    Programs used in this video:
    github.com/WillDonaldson/Samp...
    Products I use:
    Arduino Uno: amzn.to/30WTweL
    Raspberry Pi: amzn.to/3qI5ouf
    Arduino Nano: amzn.to/3mwClIy
    Follow me:
    Instagram: @_willdonaldson_
    Website: www.willdonaldson.io
    GitHub: www.github.com/WillDonaldson
    Chapters:
    0:00 Encoder Overview
    3:04 Single Channel Optical Encoder
    8:00 Quadrature Hall Effect Encoder
    15:38 Quadrature Optical Encoder
    Disclaimer:
    Links in this description may be affiliate links. If you purchase a product or service through the links I provide I may receive a small commission. This helps support the channel and enables me to pursue bigger and better projects. Affiliate links include no additional charge to you.
  • Věda a technologie

Komentáře • 105

  • @BredihinKA
    @BredihinKA Před rokem +3

    Good day.
    Can you please tell me why we need to use 2 interrupt inputs per 2 phase quadrature encoder? Can't we just use one interrupt? One phase is connected to the interrupt input, the second phase is connected to any other input. When the interrupt is triggered, we read the state of the second input, and determine the direction.
    And in this case, for an optical encoder with 3 phases, we can use two interrupts. One for one of the main phases, and a second interrupt for phase Z.
    PS: As far as I remember, Atmega 328p has only two hardware interrupt inputs.

    • @WillDonaldson
      @WillDonaldson  Před rokem +1

      yes, this would work and probably makes more sense to do it this way

    • @BredihinKA
      @BredihinKA Před rokem

      @@WillDonaldson Use two interrupts per encoder, it may make sense if, as you described in the comments, we want to force the encoder resolution to increase.

    • @nomannosher8928
      @nomannosher8928 Před 2 měsíci

      this will also change the Mode of the interrupt from "HIGH" to "CHANGE", otherwise it will skip the pulses.

  • @de3bis
    @de3bis Před 4 lety +4

    I really like all your projects and explanations. Keep it up you're awesome!!

  • @alexanderscott5856
    @alexanderscott5856 Před rokem +1

    I'm not mechanically inclined, at all.
    I have no background in STEM whatsoever.
    I'm more arts and language-minded.
    And this STILL made sense to me.
    That's the mark of a good tutorial.
    Thank you.

  • @ryanbrown172
    @ryanbrown172 Před 3 lety +2

    Thank you, Will. Great explanations.

  • @Skateroo
    @Skateroo Před 2 lety

    I had to get an optical quadrature encoder working without knowing how it works, and I have tried 5 different libraries and tons of example code. This was the only one that actually worked for me, so thanks for the video!

    • @WillDonaldson
      @WillDonaldson  Před 2 lety +1

      Thanks for the positive feedback, glad to help!

  • @RimstarOrg
    @RimstarOrg Před 2 lety +2

    Thanks, Will. Very useful information.

  • @Frankx520
    @Frankx520 Před 3 lety +1

    This is very helpful. Thank you Sir!

  • @malgailany
    @malgailany Před 4 lety

    Very nice explanation!
    Thank you.

  • @alexgillespie4419
    @alexgillespie4419 Před 2 lety

    this video is GOATED, best video I could find on the topic

  • @ghost69492
    @ghost69492 Před 3 lety +2

    Thanks man. You have helped me a lot!

  • @hosseinashkboos5188
    @hosseinashkboos5188 Před 2 lety

    Very Helpful work, Thank you.

  • @xiaoyuhu307
    @xiaoyuhu307 Před 2 lety

    It's very clear to me, thannks! Especially for the CODE part!

  • @1607rosie
    @1607rosie Před 3 lety

    Good explanation,nice job.

  • @justinhall8042
    @justinhall8042 Před 2 lety

    We use the quad optical encoders in oil and gas. We attach them to mechanical footage counters that attach to a wheel. The wheel moves against tubing which turns the mechanical counter then rotates the encoder via a lovejoy connection

  • @andyroom4563
    @andyroom4563 Před 2 lety

    very good ,Thank you for solving my doubts

  • @djtaylor813
    @djtaylor813 Před 2 lety

    Will!!!!!!!!!!!!!!! thank you for talking about "output Z"; I watched a few other encoder vid's but no-one seem to mention what "output Z" was for.

  • @solosailor222
    @solosailor222 Před 3 lety +3

    Very good explanations.
    How to calculate the maximum RPM the Arduino code can process reliably? (motor might be 50,000 rpm for example)

  • @quangan3861
    @quangan3861 Před 3 lety +1

    Great video, very helpful, thank you very much

  • @Bianchi77
    @Bianchi77 Před 8 měsíci

    Nice info, thanks for sharing with us, well done :)

  • @ideacafe07
    @ideacafe07 Před 3 lety +2

    Great video!
    Super Thanks!

  • @electronicshut
    @electronicshut Před rokem

    Your videos are amazing ❤️❤️❤️❤️

  • @qbitsday3438
    @qbitsday3438 Před 3 lety

    Beautiful !

  • @krishnakumar-pv1hb
    @krishnakumar-pv1hb Před 3 lety

    Nice explanation...

  • @ArmRehabHands
    @ArmRehabHands Před 3 lety +2

    the best video I can found on the subject. My question is, is it possible to use same pin for different hall effect sensors. For example if you are using 4 motors, it will take 8 pins this way.

    • @WillDonaldson
      @WillDonaldson  Před 3 lety +1

      Yes, different pins are needed. Although one work around to halve the number of pins is to connect only one of the two outputs from each encoder (i.e. connect channel A to the Arduino and leave channel B disconnected). Since you know the direction that the motors are spinning in (given that you write the direction to the motor controller) you don't need both channel A and B to determine the direction. One caveat is that this only works if the motors are not backdriven.

  • @henry321yu
    @henry321yu Před rokem

    nice video great job bro

  • @angelsscroll
    @angelsscroll Před 2 lety

    Thank you sir!!

  • @zerrouninassim7566
    @zerrouninassim7566 Před rokem

    Thank you so much

  • @amaanahmad9702
    @amaanahmad9702 Před 3 lety

    Im having a little trouble setting up the circuit for the hall effect sensor as when the power to the encoders is disconnected is when I get any pulses and when it is connected I get groups of peaks and then nothing continuously. Im using a very similar motor to you but can't seem to get it working. Any help would be great!

  • @chosenone4335
    @chosenone4335 Před 3 lety

    top top// long time i search for that

  • @sumandriasumandria2253

    Would be very appreciated if u would be kind enough to let me know whether a rotary encoder module also can be uaed for this project or not (in order to got a more cheaper than the rotary encoder u used)?how is the acciration of used rotary encoder module?thanks.GBU

  • @arturoaragon8335
    @arturoaragon8335 Před 4 lety

    Nice video. What is you arduino ide font?

  • @Sharedbook
    @Sharedbook Před 2 lety +1

    great video!
    You've promised to add timestamps in the description and forgot to do that :)
    It'll also be great e if you add the name/model of the diff encoders in the description.
    Keep creating!!!

    • @WillDonaldson
      @WillDonaldson  Před 2 lety +1

      Thanks for the reminder! I have now updated the video description with the timestamps as well as a list of components. I don't have the exact part numbers for all components but the new information I added to the description will direct to encoders that are functionally identical

  • @beladjinedjameleddine6837

    Thank you for your good explanation, I just wondering how to calculate the speed of a motor (RPM) using your code ?

    • @mustafaelbasyouni7129
      @mustafaelbasyouni7129 Před 3 lety

      did you?

    • @akshaysai1109
      @akshaysai1109 Před 3 lety

      You can use the same code that he showed in the first example, with the following modification:
      Suppose your encoder has 30 slots. Create a variable, say 'r' and add 1 to it, each time the count variable hits 30, or a multiple of 30. r is the number of wheel revolutions.
      Use 'long int x = millis () ;' function (for example) in the beginning of the loop. At the end of the loop, if x is greater than or equal to 60000, stop adding 1 to the r variable. Print r variable on serial monitor.
      This will display the rpm of the motor on the serial monitor

    • @HamzanMartinez
      @HamzanMartinez Před 2 lety

      @@akshaysai1109 And what about the case of using a hall effect magnetic encoder?

  • @leon320gb
    @leon320gb Před 3 lety

    How did you attach the hall sensor to the motor

  • @chitranshaherwar9438
    @chitranshaherwar9438 Před 3 lety

    Which is best for diy steering wheel

  • @andrewwolf4430
    @andrewwolf4430 Před 8 měsíci

    Any videos on linear encoders?

  • @krishnakumar-pv1hb
    @krishnakumar-pv1hb Před 3 lety

    How can we convert sensor pulses in angular position?
    Because I want to use controller that takes error as an input, and error depends on angular position of motor shaft! Can you please help?

  • @justchill4886
    @justchill4886 Před 2 lety

    sir can we increase pulses at the disk encoder

  • @kenadams4246
    @kenadams4246 Před 3 lety

    May I ask a silly question? It appeared you had the Arduino UNO mounted on the breadboard. How did you do that? I really liked your video - very easy to understand. Thanks!

    • @WillDonaldson
      @WillDonaldson  Před 3 lety +2

      just used some blu tack to temporarily attach it

    • @kenadams4246
      @kenadams4246 Před 3 lety

      @@WillDonaldson Thanks Will! Have a great day!

  • @krokdoprzodu9805
    @krokdoprzodu9805 Před 5 měsíci

    great video, great content.. Is this miley cyrus voice is natural or you make it?

  • @user-dq1wh6fy7w
    @user-dq1wh6fy7w Před 11 měsíci

    Will this work if I use two reed switch instead of quadrature hall effect encoder?

  • @rizamboi2776
    @rizamboi2776 Před 2 lety

    couldnt i use quadrature encoder to create a PID system? since i am using a 360 servo

  • @darkbloodshed
    @darkbloodshed Před 9 měsíci

    helloo good work : do u have angle measurement codes for ardiuno uno ? i have abz encoder too E80H30-1024-3-T-24 i need to encoder angle measurement thanks

  • @yhsbu
    @yhsbu Před 2 lety

    Will, how do you get the differential quadrature encoder to 4x its counts? I have heard that if the PPR of the encoder is 1024, then it can use both channels to up that count by 4 and get a far better resolution.

    • @WillDonaldson
      @WillDonaldson  Před 2 lety +2

      Correct, in the video, I demonstrate counting the pulses each time channel A transitions from a LOW to a HIGH state using the RISING command for an interrupt. This gives 1024 pulses per revolution. If however, I was to use the CHANGE command for an interrupt (instead of RISING) then I would count a pulse both when channel A goes from LOW to HIGH (rising) and also when channel A goes from HIGH to LOW (falling). This would double the number of pulses per revolution to 2048. If I also measure the change in pulse state from channel B using the CHANGE command, the resolution will double again to 4096. Hopefully, that makes sense, here are some resources if you want to read more.
      About Arduino Interrupts: www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/
      An alternative approach to decoding quadrature signals: cdn.sparkfun.com/datasheets/Robotics/How%20to%20use%20a%20quadrature%20encoder.pdf

    • @yhsbu
      @yhsbu Před 2 lety

      @@WillDonaldson Thank you very much, Will :). I will look into your links and learn more about them. You are a gentleman and a scholar!

    • @WillDonaldson
      @WillDonaldson  Před 2 lety

      @@yhsbu No problem, let me know if you have any other questions :) Thanks for the support on BMAC!

    • @yhsbu
      @yhsbu Před 2 lety

      @@WillDonaldson Does it matter if the encoder is a push-pull type or a line driver? If we're just trying to accurately count pulses, will either type work just fine?

    • @WillDonaldson
      @WillDonaldson  Před 2 lety

      ​@@yhsbu the operation is the same, they just handle noise filtering differently. This article gives a great explanation of the 3 types. A short summary is:
      1) Open-Collector Output: cheapest but susceptible to electrical noise and slow (relatively speaking)
      2) Push-Pull: Fast but still susceptible to electrical noise
      3) Differential Line Driver: Fast and good at filtering electrical noise (more expensive)
      I am not sure what the scope of your application is, for hobbyist projects any of these will be fine, if used in an application where very precise control is important and you have a long cable length that is susceptible to electrical noise then maybe use a differential line driver
      www.cuidevices.com/blog/comparison-of-common-encoder-output-signals

  • @vp.8181
    @vp.8181 Před 2 lety

    I am using incremental optical encoder 600ppr but gives 1200 readings per revolution (max 100khz) with arduino(16mhz). When the rpm is around 50-100 no issue with the readings. But when i rotate it at 300+ rpm the Arduino skips pulses and gives a wrong output. I tried removing the serial print function still there's some skipping. I am using it interrupt routine. Should I change the countroller to teensy 4.1 (600mhz) or any other solution. And if teensy then can i use 5 encoders on one teensy. Please help me with it.

  • @qbitsday3438
    @qbitsday3438 Před 3 lety

    Can we use this as a Galvo ? 12:34

  • @abdallhqise2248
    @abdallhqise2248 Před 2 lety

    I need your help. I want to make a measuring wheel using Rotary Encoder Connection and Arduino, but I don't know how to write the code. Can you help me?

  • @TidyThreads
    @TidyThreads Před 11 měsíci +1

    Handy tip if you are using the E6BS-CWZ6C Encoder This uses NPN Open collector outputs.. They need to be pulled up on the input. Use this pinMode(encoderA, INPUT_PULLUP); for all your inputs (:

    • @MichaelButlerC
      @MichaelButlerC Před 11 měsíci +1

      I don't know what the hell this means but maybe it's why my encoders don't seem to be working AT ALL. Thank you, will play around with this tomorrow

    • @TidyThreads
      @TidyThreads Před 11 měsíci

      @@MichaelButlerC HI It's arduino code... the input from the encoder to the arduino, these pins need to be pulled up. I think what it does is holds the pin high with 5v with a resistor. so when the encoder has a falling signal in the square wave it's from 5v to 0v .Not lets say 4.2v to 0v. i might be wrong but pulling the pin up on the arduino made mine work.

    • @MichaelButlerC
      @MichaelButlerC Před 11 měsíci +1

      @@TidyThreads thanks, I figured out already -- my pro micro was only giving 4.5v on VCC so the encoder refused to work. I ran a second USB cable raw, gave it 4.98v and it started working!

  • @zuhaibchoudhary5713
    @zuhaibchoudhary5713 Před 3 lety

    how much rpm can arduino read
    can it read a sensor with 1024 ppm ?
    and max 3600 rpm motor

  • @hamzarasool7172
    @hamzarasool7172 Před 3 lety

    Where can i get this encpder form, any online links

    • @WillDonaldson
      @WillDonaldson  Před 3 lety

      I bought them at a local store but a good place to look online for similar devices is pololu.com, aliexpress.com, and amazon.com

  • @pranjalpaul9048
    @pranjalpaul9048 Před 3 lety +1

    How do we handle 2 of those micro-gear motors with a single Arduino UNO since the uno has just 2 interrupt pins ?

    • @WillDonaldson
      @WillDonaldson  Před 3 lety +2

      I discuss using more than 2 interrupt pins from @17:10 onwards. See documentation for enableInterrupt if you want more info github.com/GreyGnome/EnableInterrupt

    • @pranjalpaul9048
      @pranjalpaul9048 Před 3 lety

      I want to control 2 of these motors for differential drive. How do I control them or play with encoders ?

  • @keshavkushwah4750
    @keshavkushwah4750 Před 2 lety

    Can you please help me to make a code for dmx stapper motor with encoder

  • @user-dq1wh6fy7w
    @user-dq1wh6fy7w Před 10 měsíci

    is there a tutorial for this implementing a C language instead of Arduino C++?

  • @igorchlebowski5881
    @igorchlebowski5881 Před rokem

    Why did you change the arduino from Leonardo to Uno when you had started using the optical encoder (2 phase) ? I ask because I am facing a problem with my leonardo.

    • @WillDonaldson
      @WillDonaldson  Před rokem

      This video only uses the Arduino Uno? Truthfully I've never owned or used a Leonardo so I can't help with that specific issue, but the code should work on all Arduino boards

  • @user-uh2lf1hn8d
    @user-uh2lf1hn8d Před 9 měsíci

    how to detecet speed and current position ??

  • @PladdenTim
    @PladdenTim Před 3 lety

    Don't you have problems with the accurancy of the encoders. I use some magnetic ecoders to integrated in my 37D Metal Gearmotors and they seemded not to be very accurated. For Example:
    I got 2 motors on my differencial robot and if i turn one on and the other not, both encoders give me "counts".
    The one which is off, only counts a lil bit, but thats a big problem.

    • @WillDonaldson
      @WillDonaldson  Před 3 lety

      Hmm, it is hard to diagnose hardware systems without seeing them in person. Are you using the code I provided, or has it been modified? By the description it sounds like you may have both encoders tied to the same interrupt service routine (ISR)? They should have two ISR. Also depending on your encoder you may need to include a pull up resistor on the output pin to avoid floating values. I would advise consulting the datasheet for more information

  • @wilco1v
    @wilco1v Před rokem

    Phase Quadrature Encoder 600P/R
    how much resistance to rotate the axel ??
    needed to copy budged style this swirl meter for my flow hobby

    • @WillDonaldson
      @WillDonaldson  Před rokem

      good quality encoders will have bearings and almost no resistance

    • @wilco1v
      @wilco1v Před rokem

      @@WillDonaldson Thanx ,ill order1 ,led the wind flow .....

  • @Deciptikon
    @Deciptikon Před 3 lety

    Cool))))))))

  • @harlandmandigo4521
    @harlandmandigo4521 Před 2 lety

    Mr Donaldson, was watching a CZcams video of you printing a 3d robot arm. Did you know that spidersilk can conduct electricity

  • @alphaO27
    @alphaO27 Před rokem

    How to measure RPM with quadrature hall effect ?

  • @atifhussain3455
    @atifhussain3455 Před 3 lety

    Hi, Have you tried it on higher rpm? ie.1500-2000?

  • @filipstojanovicmechanicale9265

    How is he plotting on serial monitor, is that some another application or arduino IDE can do it by itself?

    • @WillDonaldson
      @WillDonaldson  Před rokem +1

      it's a feature that comes with the IDE. Go to the top menu of the arduino IDE window and select: Tools/Serial Plotter

    • @filipstojanovicmechanicale9265
      @filipstojanovicmechanicale9265 Před rokem

      @@WillDonaldson Thanks friend, i am trying it tomorrow

    • @filipstojanovicmechanicale9265
      @filipstojanovicmechanicale9265 Před rokem

      @@WillDonaldson Just one question, it's kinda related to this video. I have some optical encoders from old printers and i can't identify pins, there are six of them. Do you have some idea where i can find more information about this?

    • @WillDonaldson
      @WillDonaldson  Před rokem +1

      @@filipstojanovicmechanicale9265 no, not with the information provided. There are multiple different ways it could be wired. Look for any part numbers and search online for documentation. If you can identify the power and GND lines (and at what voltage it operates) then you can probe the other wires to test them.

    • @filipstojanovicmechanicale9265
      @filipstojanovicmechanicale9265 Před rokem

      @@WillDonaldson Thanks 👍 I will try to identify the power and ground. And i will let you know if i succeded

  • @abdallhqise2248
    @abdallhqise2248 Před 2 lety

    Hello
    Can you help me write the Arduino code?
    I designed a small car that runs on a single DC motor, and I want to measure the distance traveled by the car and through this distance I can stop the car or reduce its speed.
    So I purchased (Incremental Rotary Encoder 600P/R) to measure the distance by attaching it to one of the wheels, and I also purchased (BTS7960 43A-H Bridge High-power Motor Driver) to control the motor according to the distance measured by the encoder.
    (For example, when the car travels a distance of two meters, it slows down or stops)
    Can you help me write the code on the Arduino?
    Note that I have the circumference of the wheel, and I will send a picture of the encoder and the Motor Driver
    Thank you.​

    • @WillDonaldson
      @WillDonaldson  Před 2 lety +2

      thanks for your interest in my tutorial! Unfortunately, I do not have the time to write custom software for everyone. That said, if you take the code provided in the description you can count the number of pulses. Knowing that there is 600 pulses/revolution you can calculate the distance. Distance = (wheel circumference) * (number of pulses) / (600 pulses per revolution).

    • @abdallhqise2248
      @abdallhqise2248 Před 2 lety

      @@WillDonaldson Thank you

  • @ahmadsabbah4634
    @ahmadsabbah4634 Před 2 lety

    Why when i use the last code the only thing i get is 0

    • @WillDonaldson
      @WillDonaldson  Před 2 lety

      Not sure, double-check the wiring, it's possible there is a loose or incorrect connection. Also if you have a different encoder to the one shown in the video, check the manufacturer's datasheet as they may have different wire names or colors, may operate at a different voltage, or have other features that make it incompatible with the code I provided.

    • @mitchellberthelot5655
      @mitchellberthelot5655 Před 2 lety

      The same thing happens for me, still troubleshooting. Did you find a solution?

  • @abdallhqise2248
    @abdallhqise2248 Před 2 lety

    I need your help. I want to make a measuring wheel using Rotary Encoder Connection and Arduino, but I don't know how to write the code. Can you help me?