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
A couple of months of my graduate course in just 10 minutes! :-D ... Thanks for sharing!
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
7 ай бұрын
Very helpful - This ended up making my code work and support bi-directional control.
@muhammadtaimoor876
5 ай бұрын
YOU ARE A LIFESAVER. THANK YOU SO MUCH.
@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.
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
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
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.
This is a great tutorial: short and very precise explanation, yet covering a vast area on the subject. Pleaee make more videos like this.
This, in comparison with many other videos on the subject, is very well done!
Whole PID, encoder, Basic control theory with real applications under 10 minutes. Thank you and you are very well on educating.
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.
The near-perfect tutorial. Clear, concise, the animations are fluid and easy to understand, and straight to the point. Nice!
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
Жыл бұрын
Appreciate this Jeff. I am still working on some videos, but it's definitely a challenge to find the time and the right topics.
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!
Keep the hard work , I love how you connect the theoretical part with hand on application 😍
Feel like I struck gold finding this video - huge thank you for making it!
@curiores111
Жыл бұрын
Sure, and thanks for stopping by. 😉
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!
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!
Hi Curio, theses videos are amazing! Please keep up the great work!
Thanks for clear presentation and explanation
One of the best technical content I've ever seen on youtube. Please keep posting content!
@curiores111
3 жыл бұрын
Thank you, friend. I have a couple even more technical videos planned for hardware implementation of controls, so stay tuned.
very well made video. consise, straight to point, focused, visual and audio on point. very good. edit: subscribed.
Excellent Video. It really was what i was searching for!!
Super clear and straight to the point, I hope you're going to do more of these!
@DarrinSK
2 жыл бұрын
guess I am just a fucking idiot then
@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.
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.
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
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
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
Another nearly perfect tutorial. Thanks!
So well done! Saved me a lot of time...
@curiores111
6 ай бұрын
Glad to be of help! And thank you so much, you are far too generous!
very well delivered, easy to understand
Thank you for the very concise and clear explanation :) really looking forward to use this in my next project.
@curiores111
2 жыл бұрын
glad you thought so! and good luck with your next project. :)
Excellent ! An objective, clean explanation and good narration as well!
@curiores111
2 жыл бұрын
Thank you, Felipe.
Excellent video . Thank you!
Just a perfect presentation and Tutorial, thanks very much.
you are great Curio, made a great simple to understand tutorial
@curiores111
Жыл бұрын
Appreciate that ❤️
Thanks for your super clear example ,fantastic!!
Your KZread channel has a bright future. Excellent content. Keep up the good work!!
@curiores111
3 жыл бұрын
You are too kind. Thank you.
Impressively well done tutorial. I hope you find the motivation and time to do more of these
@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.
it is very clear and percise video and let me say ur voice is very comfort to listen to
@curiores111
2 жыл бұрын
glad to hear that, and thank you :)
Another great video CR, thanks a bunch!
Thank you so much for sharing!!!! Greetings from Panama!!!! 🇵🇦
Excellent video. So much useful information in a very concise delivery.
@curiores111
2 жыл бұрын
Thank you for the kind words, Bill.
Nice narration - watched it a long time ago but made more sense this time as I have learned more !
@curiores111
2 жыл бұрын
glad to hear that! there's a lot to unpack here and experience is everything...
Dry well presented tutorial - Thanks for sharing.
This is a really great video. Thank you! It's very helpful for my latest project.
Impressive tutorial thank you ❤
Thanks to Heavens I found you! THIS IS WHAT I TRULY NEED! Thank You SOOOOOO MUCH.
This was extremely helpful, thank you so much!
Wonderful explanation of hardware and software
Helped me a lot. The best video on this topic. Have subbed without thinking. You deserve a lot more audience. Keep going 👏👏👏
@curiores111
3 жыл бұрын
Thank you, friend. You are too kind.
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💞
oh wow! a future Multi-million subscriber channel in it's infancy! what an honor! Amazing content!
@curiores111
3 жыл бұрын
😳
Infinitamente agradecido, agradecido y muy agradecido.. Bendiciones para Usted!!!
Excellent introduction.
this video is an other level of programming, that make an other challenge and make me better programmer. Thank you very much
@curiores111
2 жыл бұрын
how kind. Thank you. I wish you luck in writing many beautiful programs. :)
Nice video... Make such videos which gives practical implementation based on Theory.. looking for more videos..keep it up
great tutorial. you saved my life. thank you
Programming is best with practical experience . 👍👍👌👌👌
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 ❤
Amazing this is very helpful!
OMG this is a well very made video, congrats!!
@curiores111
8 ай бұрын
Thank you!
I love you. It's very interesting and useful.
very useful video, keep up the good work!!!!
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
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
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.
Excellent Video! Please make more!
Thank you. I am reusing encoders from old photocopy machines. this will help.
@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...
The best video I have seen. Thanks a lot.
@curiores111
Жыл бұрын
Thank you 💖
Thank you for your explanation!! Have a nice day Have a nice year! 😊
Awesome!!! Best video I have ever seen🖤
What a great and clear video!
@curiores111
2 жыл бұрын
thank you, friend.
Really good explanation 😉
Thank you for sharing this knowledge, I think can help very much
Very nice and good explanation.
Fantastic video! Thanks !
Best i ve ever seen -thx a lot
one of the most useful videos about the dc motor position control! thank you.
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.
I need more videos.It's awesome.
best tutorial ever!
great topic, thanks 👍
great video.. I wish you continue that great work
Thanks, worked like a charm
@curiores111
3 жыл бұрын
Glad to hear it!
This was so good.
this is realllly fantastic!
your video is wonerful, helps me a lot thank u very much
that was extremely useful, you have bright technical and teaching skills. i hope that you will continue this great work. Warmest Thanks.
@curiores111
2 жыл бұрын
You are too kind, thank you Ziad
WOW such a great video and explanation. Def just gained a subcriber.
@curiores111
2 жыл бұрын
Well thank you Dustan. Hope to see you around.
Que bello video y que hermosa exposición
Thank you for sharing.
great video thank you for sharing
Very easy video for understand ❤❤❤❤❤
the easiest to understand video out there about pid motor position control. good job!
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
This is so good omg, thank you
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!
Wow, great tutorial!!
👏 👏 👏 really good instructions, 🙏 thanks ❤️
You are amazing 👏 Now I'll have to study more 😅🇦🇺
Excellent Video. Thanks for takibg the time to make it look so easy..
@curiores111
2 жыл бұрын
No problem. Thanks for the kind words. 😄
@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....
I am so thankful with you. You really helped me with my homework. Thanks a lot
@curiores111
2 жыл бұрын
Glad to hear that! Good luck with your class Cristian.
@cristiancamilocruzmontana3710
2 жыл бұрын
@@curiores111 ❤
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.
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
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.
Thank you for the video its really very helpful
@curiores111
Жыл бұрын
No problem 😉
Awesome video!! Make some more!!
Just LOVE YOU!!!