How to control a DC motor with an encoder

Ғылым және технология

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

Пікірлер: 515

  • @cristianotulio7716
    @cristianotulio77163 жыл бұрын

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

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

    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

    7 ай бұрын

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

  • @muhammadtaimoor876

    @muhammadtaimoor876

    5 ай бұрын

    YOU ARE A LIFESAVER. THANK YOU SO MUCH.

  • @msr15334

    @msr15334

    4 ай бұрын

    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.

  • @rokonuddin72
    @rokonuddin723 жыл бұрын

    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

    3 жыл бұрын

    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

    2 жыл бұрын

    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.

  • @andalibrahman1263
    @andalibrahman12633 жыл бұрын

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

  • @TricksterJ97
    @TricksterJ973 жыл бұрын

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

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

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

  • @Greebstreebling
    @Greebstreebling2 жыл бұрын

    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.

  • @luccarodrigues781
    @luccarodrigues7813 жыл бұрын

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

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

    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

    Жыл бұрын

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

  • @MrStrangerr2002
    @MrStrangerr20026 ай бұрын

    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!

  • @bleach96215
    @bleach962153 жыл бұрын

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

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

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

  • @curiores111

    @curiores111

    Жыл бұрын

    Sure, and thanks for stopping by. 😉

  • @brandenchristiansen8529
    @brandenchristiansen85296 ай бұрын

    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!

  • @lightman500
    @lightman5003 ай бұрын

    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!

  • @mahirdrafi9579
    @mahirdrafi95792 жыл бұрын

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

  • @user-mj2br7cz5k
    @user-mj2br7cz5k6 ай бұрын

    Thanks for clear presentation and explanation

  • @erkamarslan3227
    @erkamarslan32273 жыл бұрын

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

  • @curiores111

    @curiores111

    3 жыл бұрын

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

  • @EEAMD-co6nw
    @EEAMD-co6nw Жыл бұрын

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

  • @josechoriego4718
    @josechoriego47183 жыл бұрын

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

  • @Merfnad
    @Merfnad3 жыл бұрын

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

  • @DarrinSK

    @DarrinSK

    2 жыл бұрын

    guess I am just a fucking idiot then

  • @Merfnad

    @Merfnad

    2 жыл бұрын

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

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

    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.

  • @lucasinacio8251
    @lucasinacio82513 жыл бұрын

    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

    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

    3 жыл бұрын

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

  • @andrewparker2751
    @andrewparker27512 жыл бұрын

    Another nearly perfect tutorial. Thanks!

  • @my3dcncpnp408
    @my3dcncpnp4086 ай бұрын

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

  • @curiores111

    @curiores111

    6 ай бұрын

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

  • @muhyin
    @muhyin2 жыл бұрын

    very well delivered, easy to understand

  • @vagarthgaurav
    @vagarthgaurav2 жыл бұрын

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

  • @curiores111

    @curiores111

    2 жыл бұрын

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

  • @felipemanhaes470
    @felipemanhaes4702 жыл бұрын

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

  • @curiores111

    @curiores111

    2 жыл бұрын

    Thank you, Felipe.

  • @ChrisHalden007
    @ChrisHalden0073 жыл бұрын

    Excellent video . Thank you!

  • @cyabd8515
    @cyabd85152 жыл бұрын

    Just a perfect presentation and Tutorial, thanks very much.

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

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

  • @curiores111

    @curiores111

    Жыл бұрын

    Appreciate that ❤️

  • @adamhsu2725
    @adamhsu27252 жыл бұрын

    Thanks for your super clear example ,fantastic!!

  • @eduardomeller6926
    @eduardomeller69263 жыл бұрын

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

  • @curiores111

    @curiores111

    3 жыл бұрын

    You are too kind. Thank you.

  • @luisgpr1
    @luisgpr13 жыл бұрын

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

  • @curiores111

    @curiores111

    3 жыл бұрын

    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.

  • @sinman1433
    @sinman14332 жыл бұрын

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

  • @curiores111

    @curiores111

    2 жыл бұрын

    glad to hear that, and thank you :)

  • @johnpuckett4032
    @johnpuckett40322 жыл бұрын

    Another great video CR, thanks a bunch!

  • @ProfeARios
    @ProfeARios26 күн бұрын

    Thank you so much for sharing!!!! Greetings from Panama!!!! 🇵🇦

  • @GlobalDeviceSolution
    @GlobalDeviceSolution2 жыл бұрын

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

  • @curiores111

    @curiores111

    2 жыл бұрын

    Thank you for the kind words, Bill.

  • @bikefarmtaiwan1800
    @bikefarmtaiwan18002 жыл бұрын

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

  • @curiores111

    @curiores111

    2 жыл бұрын

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

  • @onlooker251
    @onlooker2512 ай бұрын

    Dry well presented tutorial - Thanks for sharing.

  • @PatchBOTS
    @PatchBOTS2 жыл бұрын

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

  • @majdhammad7741
    @majdhammad7741Ай бұрын

    Impressive tutorial thank you ❤

  • @thejaydenx.3231
    @thejaydenx.3231 Жыл бұрын

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

  • @bensmith3304
    @bensmith33042 жыл бұрын

    This was extremely helpful, thank you so much!

  • @jamessoh2640
    @jamessoh26403 жыл бұрын

    Wonderful explanation of hardware and software

  • @hrishikeshgawas4579
    @hrishikeshgawas45793 жыл бұрын

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

  • @curiores111

    @curiores111

    3 жыл бұрын

    Thank you, friend. You are too kind.

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

    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💞

  • @fpranchas
    @fpranchas3 жыл бұрын

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

  • @curiores111

    @curiores111

    3 жыл бұрын

    😳

  • @julianmendoza7984
    @julianmendoza79842 жыл бұрын

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

  • @mrxox33
    @mrxox333 жыл бұрын

    Excellent introduction.

  • @naboulsikhalid7763
    @naboulsikhalid77632 жыл бұрын

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

  • @curiores111

    @curiores111

    2 жыл бұрын

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

  • @shubhamnayak9369
    @shubhamnayak93693 жыл бұрын

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

  • @naboulsikhalid7763
    @naboulsikhalid77633 жыл бұрын

    great tutorial. you saved my life. thank you

  • @kamleshpanchal8661
    @kamleshpanchal86614 ай бұрын

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

  • @Jim_One-wl4ke
    @Jim_One-wl4ke2 ай бұрын

    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 ❤

  • @AaedMusa
    @AaedMusa3 жыл бұрын

    Amazing this is very helpful!

  • @oscarmanuelcabreravidana6268
    @oscarmanuelcabreravidana62688 ай бұрын

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

  • @curiores111

    @curiores111

    8 ай бұрын

    Thank you!

  • @MEan0207
    @MEan02073 жыл бұрын

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

  • @vatsal9005
    @vatsal90054 ай бұрын

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

  • @Telectronics
    @Telectronics3 жыл бұрын

    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

    3 жыл бұрын

    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

    3 жыл бұрын

    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.

  • @knightautomation2020
    @knightautomation20203 жыл бұрын

    Excellent Video! Please make more!

  • @candyemrys564
    @candyemrys5643 жыл бұрын

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

  • @curiores111

    @curiores111

    3 жыл бұрын

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

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

    The best video I have seen. Thanks a lot.

  • @curiores111

    @curiores111

    Жыл бұрын

    Thank you 💖

  • @user-kt1se7fp2b
    @user-kt1se7fp2b Жыл бұрын

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

  • @blacerid-hf7ku
    @blacerid-hf7ku2 жыл бұрын

    Awesome!!! Best video I have ever seen🖤

  • @Nopaisti
    @Nopaisti2 жыл бұрын

    What a great and clear video!

  • @curiores111

    @curiores111

    2 жыл бұрын

    thank you, friend.

  • @RudolfZobel
    @RudolfZobel3 жыл бұрын

    Really good explanation 😉

  • @samnangmornh8564
    @samnangmornh85643 жыл бұрын

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

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

    Very nice and good explanation.

  • @carlospodest7141
    @carlospodest71413 жыл бұрын

    Fantastic video! Thanks !

  • @torstenhenke4294
    @torstenhenke42942 жыл бұрын

    Best i ve ever seen -thx a lot

  • @dmytro8585
    @dmytro85853 жыл бұрын

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

  • @rneal63
    @rneal632 жыл бұрын

    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.

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

    I need more videos.It's awesome.

  • @jeremysrobotics191
    @jeremysrobotics1913 жыл бұрын

    best tutorial ever!

  • @frankdearr2772
    @frankdearr2772Ай бұрын

    great topic, thanks 👍

  • @omargamal665
    @omargamal6653 жыл бұрын

    great video.. I wish you continue that great work

  • @johnfrancis9668
    @johnfrancis96683 жыл бұрын

    Thanks, worked like a charm

  • @curiores111

    @curiores111

    3 жыл бұрын

    Glad to hear it!

  • @timschafer2536
    @timschafer25364 ай бұрын

    This was so good.

  • @satchelsieniewicz5824
    @satchelsieniewicz58242 жыл бұрын

    this is realllly fantastic!

  • @user-rc4zt1kl7k
    @user-rc4zt1kl7k6 ай бұрын

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

  • @ziadzakaria8206
    @ziadzakaria82062 жыл бұрын

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

  • @curiores111

    @curiores111

    2 жыл бұрын

    You are too kind, thank you Ziad

  • @DustanWebb
    @DustanWebb2 жыл бұрын

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

  • @curiores111

    @curiores111

    2 жыл бұрын

    Well thank you Dustan. Hope to see you around.

  • @JuanOrtiz-di9rr
    @JuanOrtiz-di9rr3 жыл бұрын

    Que bello video y que hermosa exposición

  • 3 жыл бұрын

    Thank you for sharing.

  • @sermadreda399
    @sermadreda3993 жыл бұрын

    great video thank you for sharing

  • @akkarawat
    @akkarawat5 ай бұрын

    Very easy video for understand ❤❤❤❤❤

  • @Ninjamstrboy
    @Ninjamstrboy3 жыл бұрын

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

  • @johanmauriciofranciscososa4779
    @johanmauriciofranciscososa47793 жыл бұрын

    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

  • @Windy2468
    @Windy24682 жыл бұрын

    This is so good omg, thank you

  • @josepablolopezaguado6191
    @josepablolopezaguado61913 жыл бұрын

    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!

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

    Wow, great tutorial!!

  • @ApteraEV2024
    @ApteraEV20248 ай бұрын

    👏 👏 👏 really good instructions, 🙏 thanks ❤️

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

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

  • @perfoperfo9910
    @perfoperfo99102 жыл бұрын

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

  • @curiores111

    @curiores111

    2 жыл бұрын

    No problem. Thanks for the kind words. 😄

  • @perfoperfo9910

    @perfoperfo9910

    2 жыл бұрын

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

  • @cristiancamilocruzmontana3710
    @cristiancamilocruzmontana37102 жыл бұрын

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

  • @curiores111

    @curiores111

    2 жыл бұрын

    Glad to hear that! Good luck with your class Cristian.

  • @cristiancamilocruzmontana3710

    @cristiancamilocruzmontana3710

    2 жыл бұрын

    @@curiores111 ❤

  • @It4heath
    @It4heath8 ай бұрын

    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.

  • @EliottJohnson
    @EliottJohnson3 жыл бұрын

    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

    3 жыл бұрын

    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.

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

    Thank you for the video its really very helpful

  • @curiores111

    @curiores111

    Жыл бұрын

    No problem 😉

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

    Awesome video!! Make some more!!

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

    Just LOVE YOU!!!

Келесі