How to control a DC motor with an encoder

Sdílet
Vložit
  • čas přidán 15. 02. 2021
  • Find the tutorial on our website: curiores.com/positioncontrol
    GitHub Code: github.com/curiores/ArduinoTu...
    If your platform does not have access to "atomic.h" (and so you get an error message), you can use the alternative version of the code that has been uploaded to the repository. It is labeled "_NoAtomic".
    An encoder makes it possible to control the position of a DC motor. In this video, I illustrate how an encoder works, and then use a PID control algorithm to control the motor position. All of the steps are included so that you will be easily able to make the system yourself.
    Parts used in this video:
    1. DC Motor - 19:1 Metal Gearmotor 37Dx68L mm 12V with 64 CPR Encoder:
    www.pololu.com/product/4751
    2. Motor Driver - TB67H420FTG Dual/Single Motor Driver Carrier:
    www.pololu.com/product/2999
    3. Microcontroller - Arduino Uno:
    store.arduino.cc/usa/arduino-...
    (okay actually I used an Elegoo Uno, but the Arduino descriptions are better :)
  • Věda a technologie

Komentáře • 509

  • @cristianotulio7716
    @cristianotulio7716 Před 3 lety +42

    A couple of months of my graduate course in just 10 minutes! :-D ... Thanks for sharing!

  • @rokonuddin72
    @rokonuddin72 Před 3 lety +151

    One of the best videos I have come across. The combination of the hardware setup and linking this to the block diagram and finally linking block diagram to code is an inspiring idea.

    • @curiores111
      @curiores111  Před 3 lety +23

      Thank you Rokonuddin. I'm very pleased that the format resonated with you. When I finished the video I was sure that I had covered too much. But I think that it can be worth the effort when you actually want to understand the details of how a system works.

    • @conraddiaz2274
      @conraddiaz2274 Před 2 lety +6

      Hi @@curiores111 , the scope of this video is perfect. If you had a feeling that it was too much, perhaps for a beginner it may be too much. For someone that already has a basic understanding and some experience coding it's absolutely perfect!! So well done!! Thank you so much.

  • @luccarodrigues781
    @luccarodrigues781 Před 3 lety +22

    The near-perfect tutorial. Clear, concise, the animations are fluid and easy to understand, and straight to the point. Nice!

  • @andalibrahman1263
    @andalibrahman1263 Před 3 lety +28

    This is a great tutorial: short and very precise explanation, yet covering a vast area on the subject. Pleaee make more videos like this.

  • @Ben_Lucy
    @Ben_Lucy Před 10 měsíci +19

    I looked into it a bit more, the disadvantage of this method is that it is only looking for encoder A pin rising. This means that wiggling the motor back and forth a little looks like the motor is moving continuously forwards. It also counts more than one step if the input bounces. Instead: checking for all four quadrature transitions fixes these problems and means 4x as many pulses per rotation which means you can be more precise.
    I added these lines at the beginning:
    #define readA bitRead(PIND,2)//faster than digitalRead()
    #define readB bitRead(PIND,3)//faster than digitalRead()
    I changed the attachInterrupt line so that it triggered on CHANGE rather than rising, and added another line for the B encoder pin:
    attachInterrupt(digitalPinToInterrupt(ENCA),readEncoderA,CHANGE);
    attachInterrupt(digitalPinToInterrupt(ENCB),readEncoderB,CHANGE);
    And I changed the functions triggered by the interrupt to this:
    void readEncoderA(){
    if(readB != readA) {
    posi ++;
    } else {
    posi --;
    }
    }

    void readEncoderB(){
    if (readA == readB) {
    posi ++;
    } else {
    posi --;
    }
    }
    This eliminates bouncing (posi will just count forwards and then backwards with each bounce), increases precision as you get 4x as many interrupts triggered per rotation, and prevents you from 'tricking' the encoder by moving the motor back and forth by a small amount. Credit to Cattledog on the Arduino forums in 2017.

    • @MattCreativeStudio
      @MattCreativeStudio Před 6 měsíci +3

      Very helpful - This ended up making my code work and support bi-directional control.

    • @muhammadtaimoor876
      @muhammadtaimoor876 Před 4 měsíci +1

      YOU ARE A LIFESAVER. THANK YOU SO MUCH.

    • @msr15334
      @msr15334 Před 3 měsíci

      You can set a interrupt on both pins and use it to access a lookup table. As a bonus, this can achieve full resolution of encoder.

  • @Greebstreebling
    @Greebstreebling Před rokem +4

    Nice combination of electronics, maths and programming. I'd come across those PID values in my Sitech motor controller and now I know what they are ! Thanks for clear presentation and explanation.

  • @TricksterJ97
    @TricksterJ97 Před 3 lety +19

    This, in comparison with many other videos on the subject, is very well done!

  • @MrStrangerr2002
    @MrStrangerr2002 Před 4 měsíci

    As a hobbyist I was struggling to get position control working on diy toy robot build, and this video has cleared a lot of questions for me. Thank you for the share!

  • @multilaton4835
    @multilaton4835 Před rokem

    Whole PID, encoder, Basic control theory with real applications under 10 minutes. Thank you and you are very well on educating.

  • @jeffdawson7936
    @jeffdawson7936 Před rokem +3

    I just discovered your channel -- it's fantastic! I appreciate your clarity, pace, and your choice of what to focus on in each video. I hope there will be more videos in the future!

    • @curiores111
      @curiores111  Před rokem

      Appreciate this Jeff. I am still working on some videos, but it's definitely a challenge to find the time and the right topics.

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

    I have to commend you on the best tutorial I have seen on motor control using PID! As others have mentioned you are clear concise and to the point. Your animations and diagrams support your lecture perfectly. Your progressive coding makes it easy to understand your logic flow....given the real world, converted to math, and then to software. Kudos!

  • @bleach96215
    @bleach96215 Před 3 lety +6

    Keep the hard work , I love how you connect the theoretical part with hand on application 😍

  • @Ben_Lucy
    @Ben_Lucy Před 11 měsíci +4

    Feel like I struck gold finding this video - huge thank you for making it!

    • @curiores111
      @curiores111  Před 11 měsíci +2

      Sure, and thanks for stopping by. 😉

  • @lucasinacio8251
    @lucasinacio8251 Před 2 lety +5

    The video is very well produced, the code explanation is amazing and made me (as a mechanical engineer with rudimentary understanding of motor control) feel capable of implementing this, well done!

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

      Thanks Lucas! I'm glad you feel that the level is right. It is hard to justify putting all that code in from a youtube video perspective, but when it actually comes down to implementing it yourself, it's probably the most important part.

    • @lucasinacio8251
      @lucasinacio8251 Před 2 lety

      @@curiores111 You managed to keep the (usually boring) code exposure very well connected with the practical application so it wans't boring anymore hehe

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

    Extremely well done. Have been utilizing an adaption of this PID control loop for leveling. Instead of using an encoder I pull data from a MPU6050 using the MPU6050_light library usually included in the Arduino IDE. Then Set the target to 0. It works really well!

  • @Merfnad
    @Merfnad Před 3 lety +27

    Super clear and straight to the point, I hope you're going to do more of these!

    • @DarrinSK
      @DarrinSK Před 2 lety

      guess I am just a fucking idiot then

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

      ​@@DarrinSK I know the feeling, but it probably just means you need to start with some other tutorials on whatever part of this seems complicated.

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

    Hi Curio, theses videos are amazing! Please keep up the great work!

  • @amrmusa7217
    @amrmusa7217 Před rokem +1

    that was amazingly informative thank you very much for this magnificent work, and the way the overshoot was eliminated was surprisingly accurate, I'll make sure to subscribe.

  • @hrishikeshgawas4579
    @hrishikeshgawas4579 Před 3 lety

    Helped me a lot. The best video on this topic. Have subbed without thinking. You deserve a lot more audience. Keep going 👏👏👏

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

    Your CZcams channel has a bright future. Excellent content. Keep up the good work!!

  • @erkamarslan3227
    @erkamarslan3227 Před 3 lety

    One of the best technical content I've ever seen on youtube. Please keep posting content!

    • @curiores111
      @curiores111  Před 3 lety

      Thank you, friend. I have a couple even more technical videos planned for hardware implementation of controls, so stay tuned.

  • @EEAMD-co6nw
    @EEAMD-co6nw Před rokem

    very well made video. consise, straight to point, focused, visual and audio on point. very good. edit: subscribed.

  • @josechoriego4718
    @josechoriego4718 Před 2 lety

    Excellent Video. It really was what i was searching for!!

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

    This is a really great video. Thank you! It's very helpful for my latest project.

  • @user-mj2br7cz5k
    @user-mj2br7cz5k Před 4 měsíci +1

    Thanks for clear presentation and explanation

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

    Thank you for the very concise and clear explanation :) really looking forward to use this in my next project.

    • @curiores111
      @curiores111  Před 2 lety

      glad you thought so! and good luck with your next project. :)

  • @Jim_One-wl4ke
    @Jim_One-wl4ke Před měsícem

    This is awesome, and you are a good instructor and a good presenter. Very good content flow esp with lots of diagram & graphics to add with the presentation flow 👍..love it. Thanks for making this tutorial ❤

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

    Impressively well done tutorial. I hope you find the motivation and time to do more of these

    • @curiores111
      @curiores111  Před 3 lety

      Thanks so much Luis. I am planning to do more videos. Feel free to make a request if you come a cross a topic that you feel needs a better explanation than what is currently available.

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

    Nice video... Make such videos which gives practical implementation based on Theory.. looking for more videos..keep it up

  • @sinman1433
    @sinman1433 Před 2 lety

    it is very clear and percise video and let me say ur voice is very comfort to listen to

  • @kijijikhalid601
    @kijijikhalid601 Před rokem +1

    wow, what a magnificent combination of hardware and software to explain the PID control. It's was a dream to understand it, and I got it now. How CAN THANK YOU💞

  • @rajivdey3103
    @rajivdey3103 Před rokem

    you are great Curio, made a great simple to understand tutorial

  • @cyabd8515
    @cyabd8515 Před 2 lety

    Just a perfect presentation and Tutorial, thanks very much.

  • @bikefarmtaiwan1800
    @bikefarmtaiwan1800 Před 2 lety

    Nice narration - watched it a long time ago but made more sense this time as I have learned more !

    • @curiores111
      @curiores111  Před 2 lety

      glad to hear that! there's a lot to unpack here and experience is everything...

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

    Great video tutorial! A lot of the PC CNC controllers have step and direction outputs for stepper motor drives, thought it might be pretty easy, if another interrupt is available, to change the setpoint based on step and direction inputs, should be superior to a stepper motor because of the closed loop.

  • @onlooker251
    @onlooker251 Před měsícem

    Dry well presented tutorial - Thanks for sharing.

  • @Telectronics
    @Telectronics Před 3 lety

    When my professor told me he can´t tell me how to implement pid into a microcontroller I thought it must be a very hard thing. Finally I came across your video and this was poor luck because my search on youtube gave nothing like yours. So thank you very much for taking your time to explain all of this. I like control theory because it´s hard to understand but If you understand it makes you proud.

    • @curiores111
      @curiores111  Před 3 lety

      Sounds like my profs. :D
      Thank you so much for your comment. Your comment is literally the reason I make videos.
      I agree. Control theory is challenging, rewarding, and exciting.

    • @Telectronics
      @Telectronics Před 3 lety

      Theory is dry but what you show makes more fun at least :D Hey I have an idea do you think an encoder attached to a bldc motor would allow me to set the motor speed perfectly with your pid method ? I have such a motor left from an old hdd and I would play around with your code. I only knew how to do it with op amps but changing code is faster than changing capacitors and resistors.

  • @GlobalDeviceSolution
    @GlobalDeviceSolution Před 2 lety

    Excellent video. So much useful information in a very concise delivery.

  • @felipemanhaes470
    @felipemanhaes470 Před 2 lety

    Excellent ! An objective, clean explanation and good narration as well!

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

    Excellent video . Thank you!

  • @johnpuckett4032
    @johnpuckett4032 Před 2 lety

    Another great video CR, thanks a bunch!

  • @luckyshadowtux
    @luckyshadowtux Před 2 lety

    one of the best tutorials I have ever watched. Have you thought of doing an MPC video?

    • @curiores111
      @curiores111  Před 2 lety

      You're too kind, thank you. Yes, I have, among a couple others. Thank you for the suggestion.

  • @andrewparker2751
    @andrewparker2751 Před 2 lety

    Another nearly perfect tutorial. Thanks!

  • @adamhsu2725
    @adamhsu2725 Před 2 lety

    Thanks for your super clear example ,fantastic!!

  • @julianmendoza7984
    @julianmendoza7984 Před 2 lety

    Infinitamente agradecido, agradecido y muy agradecido.. Bendiciones para Usted!!!

  • @majdhammad7741
    @majdhammad7741 Před 3 dny

    Impressive tutorial thank you ❤

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

    Thank you so much for explaining this so clear and concisely, the video, the animations, the coding and your voice are nice, and it became so clear to me I think I could do it on my own now!

  • @bensmith3304
    @bensmith3304 Před 2 lety

    This was extremely helpful, thank you so much!

  • @johanmauriciofranciscososa4779

    Impresionante video. Con una gran explicación y unas animaciónes que en verdad hace todo mucho mas fácil y entendible (Awesome video. With a great explanation and an animations that really makes everything much easier and understandable).
    Tuve algunos incovenientes para adaptarlos a mi proyecto y estuvo siempre atenta para ayudarme a solucionarlos.(I had some inconveniences to adapt them to my project and she was always attentive to help me solve them.)
    Thank you so much

  • @fpranchas
    @fpranchas Před 3 lety

    oh wow! a future Multi-million subscriber channel in it's infancy! what an honor! Amazing content!

  • @thejaydenx.3231
    @thejaydenx.3231 Před 11 měsíci

    Thanks to Heavens I found you! THIS IS WHAT I TRULY NEED! Thank You SOOOOOO MUCH.

  • @muhyin
    @muhyin Před 2 lety

    very well delivered, easy to understand

  • @jamessoh2640
    @jamessoh2640 Před 3 lety

    Wonderful explanation of hardware and software

  • @ziadzakaria8206
    @ziadzakaria8206 Před 2 lety

    that was extremely useful, you have bright technical and teaching skills. i hope that you will continue this great work.
    Warmest Thanks.

  • @samnangmornh8564
    @samnangmornh8564 Před 3 lety

    Thank you for sharing this knowledge, I think can help very much

  • @It4heath
    @It4heath Před 6 měsíci

    Very nice video, and close to what I'm looking at doing. My plans so far will be with any Arduino board, I'm wanting to have a AS5600 read a direction/step every second and write/save that info, so an average can be calculated for later use. Then once a minute or so read another AS5600 with a Stepper motor and have the stepper match the average direction/steps of the other one.

  • @my3dcncpnp408
    @my3dcncpnp408 Před 4 měsíci +1

    So well done! Saved me a lot of time...

    • @curiores111
      @curiores111  Před 4 měsíci +1

      Glad to be of help! And thank you so much, you are far too generous!

  • @user-kt1se7fp2b
    @user-kt1se7fp2b Před rokem

    Thank you for your explanation!!
    Have a nice day
    Have a nice year!
    😊

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

    Programming is best with practical experience . 👍👍👌👌👌

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

    I so appreciate that you wrote the code out rather than called library functions - I've been working to control a motor with encoder via an MCU that doesn't work with Arduino IDE so seeing the code written out really helps.

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

      Thanks for mentioning this Darren. Originally I thought I would be criticized for not using the library. Surprisingly, I haven't seen that yet. I'm pleased that the presentation I chose was suitable for your work. Good luck with your project.

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

    one of the most useful videos about the dc motor position control! thank you.

  • @naboulsikhalid7763
    @naboulsikhalid7763 Před 3 lety

    great tutorial. you saved my life. thank you

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

    I love you. It's very interesting and useful.

  • @naboulsikhalid7763
    @naboulsikhalid7763 Před 2 lety

    this video is an other level of programming, that make an other challenge and make me better programmer. Thank you very much

    • @curiores111
      @curiores111  Před 2 lety

      how kind. Thank you. I wish you luck in writing many beautiful programs. :)

  • @oscarmanuelcabreravidana6268

    OMG this is a well very made video, congrats!!

  • @JuanOrtiz-di9rr
    @JuanOrtiz-di9rr Před 3 lety

    Que bello video y que hermosa exposición

  • @Ninjamstrboy
    @Ninjamstrboy Před 3 lety

    the easiest to understand video out there about pid motor position control. good job!

  • @mrxox33
    @mrxox33 Před 3 lety

    Excellent introduction.

  • @DustanWebb
    @DustanWebb Před 2 lety

    WOW such a great video and explanation. Def just gained a subcriber.

    • @curiores111
      @curiores111  Před 2 lety

      Well thank you Dustan. Hope to see you around.

  • @EliottJohnson
    @EliottJohnson Před 2 lety

    Excellent, I just finished following your tutorial and everything worked out great. Still playing with the PID values to get the sinus as close as possible to the target but I think the motor just isn't precise enough.

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

      Good work! Using a motor with a higher gear ratio will help with precision. (of course, if you need much better precision a stepper is often a better choice) Adjusting the PID parameters can help, but won't be able to overcome some of the limitations of the motor, as you're finding.

  • @candyemrys564
    @candyemrys564 Před 3 lety

    Thank you. I am reusing encoders from old photocopy machines. this will help.

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

      Sounds like fun! I have seen people posting salvages from photocopy machines on reddit before. I need find an old copier to salvage...

  • @user-rc4zt1kl7k
    @user-rc4zt1kl7k Před 5 měsíci

    your video is wonerful, helps me a lot thank u very much

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

    very useful video, keep up the good work!!!!

  • @mandregomes
    @mandregomes Před rokem

    Very nice and good explanation.

  • @accu-KL
    @accu-KL Před 3 lety

    Amazing explanation.
    After saw full video, i subscribed immediately, by hoping upcoming videos would be best among.

    • @superabbasalmani6079
      @superabbasalmani6079 Před 3 lety

      Excuse me, only a nice Video, or did you try it real with those Codes from this lady Administrator ?
      Has somebody tested it bevor me ? Because i can try it only with another
      Motor Driver ( l298n)...etc...!

    • @accu-KL
      @accu-KL Před 3 lety

      @@superabbasalmani6079 yes. I did some project using this.
      I made a coil winding machine using dc motor with PID controller.

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

      @@accu-KL Thank you very much !
      You are a cool Guy, and Respect, because you took time 2 answer 2me ! Lovely!

    • @accu-KL
      @accu-KL Před 3 lety

      @@superabbasalmani6079 🙋🙋🙋🙋

  • @knightautomation2020
    @knightautomation2020 Před 2 lety

    Excellent Video! Please make more!

  • @perfoperfo9910
    @perfoperfo9910 Před 2 lety

    Excellent Video. Thanks for takibg the time to make it look so easy..

    • @curiores111
      @curiores111  Před 2 lety

      No problem. Thanks for the kind words. 😄

    • @perfoperfo9910
      @perfoperfo9910 Před 2 lety

      @@curiores111 May I ask if this is your voice or are you using a bot to do the text to speech? If you are using a bot then it is one of the best I've heard so which one is it ... If not (and it is your voice) then my appologies....

  • @omargamal665
    @omargamal665 Před 3 lety

    great video.. I wish you continue that great work

  • @cristiancamilocruzmontana3710

    I am so thankful with you. You really helped me with my homework. Thanks a lot

  • @aaedmusa
    @aaedmusa Před 3 lety

    Amazing this is very helpful!

  • @RudolfZobel
    @RudolfZobel Před 3 lety

    Really good explanation 😉

  • @youssefahourri3245
    @youssefahourri3245 Před 2 lety

    You got the same voice tone of a lady on "Element 14" . Although it's a VERY PRECISE tutorial thank you and keep going , I'll test this with a infrared encoder

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

    great video thank you for sharing

  • @carlospodest7141
    @carlospodest7141 Před 3 lety

    Fantastic video! Thanks !

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

    You are amazing 👏
    Now I'll have to study more 😅🇦🇺

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

    Perfectttt!!! I’ve been struggling with PID control for thesis💪 This helps alot thanks indeed

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

      so glad to hear that and good luck with your defense!

  • @AllTheNamesWereInUse
    @AllTheNamesWereInUse Před 3 lety

    Great video! Explanation and visual aid are amazing. Sub'd

  • @leonk_1986
    @leonk_1986 Před rokem

    I need more videos.It's awesome.

  • @blacerid-hf7ku
    @blacerid-hf7ku Před 2 lety

    Awesome!!! Best video I have ever seen🖤

  • @quantapq100tek9
    @quantapq100tek9 Před rokem

    The best video I have seen. Thanks a lot.

  •  Před 3 lety

    Thank you for sharing.

  • @franciscofortunato1864

    Gracias por tu revelador vídeo y por compartir tus conocimientos.,!

  • @akkarawat
    @akkarawat Před 4 měsíci

    Very easy video for understand ❤❤❤❤❤

  • @co4erol
    @co4erol Před rokem +3

    at 2:26 trajectories are wrong. When she said clockwise, it meant counter-clockwise. When she told counter-clockwise, it meant clockwise.
    We can see the results at 2:47. When she turns the arrow clockwise, the number goes to negative and vice versa.
    I'm fan of your channel by the way :)

    • @curiores111
      @curiores111  Před rokem +2

      Good observation. The gearbox actually reverses the direction of travel, so it's true that it triggers in the opposite order compared to the encoder by itself.

  • @Rupeshshirose
    @Rupeshshirose Před rokem

    Thank you for the video its really very helpful

  • @jegabolt706
    @jegabolt706 Před 3 lety

    its a great video thank you for sharing.

  • @justinberdell7517
    @justinberdell7517 Před rokem

    Awesome video!! Make some more!!

  • @dalekurita1075
    @dalekurita1075 Před 2 lety

    Way cool...you saved me time...yeah to finite differences math trick ....pid loop for robot arm using Irobot....it's big brother picked up Fukushima radioactive ....

  • @ApteraEV2024
    @ApteraEV2024 Před 7 měsíci

    👏 👏 👏 really good instructions, 🙏 thanks ❤️

  • @Nopaisti
    @Nopaisti Před 2 lety

    What a great and clear video!