How to control a DC motor with an encoder

  Рет қаралды 420,519

Curio Res

Curio Res

3 жыл бұрын

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

Пікірлер: 514
@cristianotulio7716
@cristianotulio7716 3 жыл бұрын
A couple of months of my graduate course in just 10 minutes! :-D ... Thanks for sharing!
@Ben_Lucy
@Ben_Lucy 11 ай бұрын
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.
@andalibrahman1263
@andalibrahman1263 3 жыл бұрын
This is a great tutorial: short and very precise explanation, yet covering a vast area on the subject. Pleaee make more videos like this.
@rokonuddin72
@rokonuddin72 3 жыл бұрын
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.
@luccarodrigues781
@luccarodrigues781 3 жыл бұрын
The near-perfect tutorial. Clear, concise, the animations are fluid and easy to understand, and straight to the point. Nice!
@Greebstreebling
@Greebstreebling 2 жыл бұрын
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 3 жыл бұрын
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.
@MrStrangerr2002
@MrStrangerr2002 5 ай бұрын
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!
@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.
@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. 😉
@bleach96215
@bleach96215 3 жыл бұрын
Keep the hard work , I love how you connect the theoretical part with hand on application 😍
@lucasinacio8251
@lucasinacio8251 3 жыл бұрын
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
@brandenchristiansen8529
@brandenchristiansen8529 6 ай бұрын
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 3 жыл бұрын
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.
@lightman500
@lightman500 3 ай бұрын
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
@mahirdrafi9579 2 жыл бұрын
Hi Curio, theses videos are amazing! Please keep up the great work!
@curiores111
@curiores111 3 жыл бұрын
There's an update to the tutorial to include the volatile directive and the ATOMIC_BLOCK macro. These prevent potential issues with the interrupt. See this page for an example: www.arduino.cc/reference/en/language/variables/variable-scope-qualifiers/volatile/
@PatchBOTS
@PatchBOTS 2 жыл бұрын
This page is down, FYI
@curiores111
@curiores111 2 жыл бұрын
@@PatchBOTS Ah yes missed that one, still updating the website...
@erkamarslan3227
@erkamarslan3227 3 жыл бұрын
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.
@josechoriego4718
@josechoriego4718 3 жыл бұрын
Excellent Video. It really was what i was searching for!!
@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.
@PatchBOTS
@PatchBOTS 2 жыл бұрын
This is a really great video. Thank you! It's very helpful for my latest project.
@EEAMD-co6nw
@EEAMD-co6nw Жыл бұрын
very well made video. consise, straight to point, focused, visual and audio on point. very good. edit: subscribed.
@eduardomeller6926
@eduardomeller6926 3 жыл бұрын
Your KZfaq channel has a bright future. Excellent content. Keep up the good work!!
@curiores111
@curiores111 3 жыл бұрын
You are too kind. Thank you.
@vagarthgaurav
@vagarthgaurav 2 жыл бұрын
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. :)
@luisgpr1
@luisgpr1 3 жыл бұрын
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.
@AaedMusa
@AaedMusa 3 жыл бұрын
Amazing this is very helpful!
@felipemanhaes470
@felipemanhaes470 2 жыл бұрын
Excellent ! An objective, clean explanation and good narration as well!
@curiores111
@curiores111 2 жыл бұрын
Thank you, Felipe.
@andrewparker2751
@andrewparker2751 2 жыл бұрын
Another nearly perfect tutorial. Thanks!
@johnpuckett4032
@johnpuckett4032 2 жыл бұрын
Another great video CR, thanks a bunch!
@cyabd8515
@cyabd8515 2 жыл бұрын
Just a perfect presentation and Tutorial, thanks very much.
@adamhsu2725
@adamhsu2725 2 жыл бұрын
Thanks for your super clear example ,fantastic!!
@GlobalDeviceSolution
@GlobalDeviceSolution 2 жыл бұрын
Excellent video. So much useful information in a very concise delivery.
@curiores111
@curiores111 2 жыл бұрын
Thank you for the kind words, Bill.
@hrishikeshgawas4579
@hrishikeshgawas4579 3 жыл бұрын
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.
@ChrisHalden007
@ChrisHalden007 3 жыл бұрын
Excellent video . Thank you!
@shubhamnayak9369
@shubhamnayak9369 3 жыл бұрын
Nice video... Make such videos which gives practical implementation based on Theory.. looking for more videos..keep it up
@rajivdey3103
@rajivdey3103 Жыл бұрын
you are great Curio, made a great simple to understand tutorial
@curiores111
@curiores111 Жыл бұрын
Appreciate that ❤️
@bikefarmtaiwan1800
@bikefarmtaiwan1800 2 жыл бұрын
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...
@Telectronics
@Telectronics 3 жыл бұрын
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.
@fpranchas
@fpranchas 3 жыл бұрын
oh wow! a future Multi-million subscriber channel in it's infancy! what an honor! Amazing content!
@curiores111
@curiores111 3 жыл бұрын
😳
@user-mj2br7cz5k
@user-mj2br7cz5k 5 ай бұрын
Thanks for clear presentation and explanation
@thejaydenx.3231
@thejaydenx.3231 Жыл бұрын
Thanks to Heavens I found you! THIS IS WHAT I TRULY NEED! Thank You SOOOOOO MUCH.
@bensmith3304
@bensmith3304 2 жыл бұрын
This was extremely helpful, thank you so much!
@sinman1433
@sinman1433 2 жыл бұрын
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 :)
@ProfeARios
@ProfeARios 18 күн бұрын
Thank you so much for sharing!!!! Greetings from Panama!!!! 🇵🇦
@muhyin
@muhyin 2 жыл бұрын
very well delivered, easy to understand
@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💞
@onlooker251
@onlooker251 2 ай бұрын
Dry well presented tutorial - Thanks for sharing.
@knightautomation2020
@knightautomation2020 3 жыл бұрын
Excellent Video! Please make more!
@julianmendoza7984
@julianmendoza7984 2 жыл бұрын
Infinitamente agradecido, agradecido y muy agradecido.. Bendiciones para Usted!!!
@oscarmanuelcabreravidana6268
@oscarmanuelcabreravidana6268 8 ай бұрын
OMG this is a well very made video, congrats!!
@curiores111
@curiores111 8 ай бұрын
Thank you!
@Ninjamstrboy
@Ninjamstrboy 3 жыл бұрын
the easiest to understand video out there about pid motor position control. good job!
@rneal63
@rneal63 2 жыл бұрын
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.
@jamessoh2640
@jamessoh2640 3 жыл бұрын
Wonderful explanation of hardware and software
@majdhammad7741
@majdhammad7741 Ай бұрын
Impressive tutorial thank you ❤
@darrennewell3845
@darrennewell3845 3 жыл бұрын
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 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.
@Jim_One-wl4ke
@Jim_One-wl4ke 2 ай бұрын
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 ❤
@samnangmornh8564
@samnangmornh8564 3 жыл бұрын
Thank you for sharing this knowledge, I think can help very much
@dmytro8585
@dmytro8585 3 жыл бұрын
one of the most useful videos about the dc motor position control! thank you.
@naboulsikhalid7763
@naboulsikhalid7763 2 жыл бұрын
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. :)
@blacerid-hf7ku
@blacerid-hf7ku 2 жыл бұрын
Awesome!!! Best video I have ever seen🖤
@naboulsikhalid7763
@naboulsikhalid7763 3 жыл бұрын
great tutorial. you saved my life. thank you
@vatsal9005
@vatsal9005 3 ай бұрын
very useful video, keep up the good work!!!!
@my3dcncpnp408
@my3dcncpnp408 6 ай бұрын
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!
@mrxox33
@mrxox33 3 жыл бұрын
Excellent introduction.
@user-kt1se7fp2b
@user-kt1se7fp2b Жыл бұрын
Thank you for your explanation!! Have a nice day Have a nice year! 😊
@MEan0207
@MEan0207 3 жыл бұрын
I love you. It's very interesting and useful.
@candyemrys564
@candyemrys564 3 жыл бұрын
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...
@carlospodest7141
@carlospodest7141 3 жыл бұрын
Fantastic video! Thanks !
@quantapq100tek9
@quantapq100tek9 Жыл бұрын
The best video I have seen. Thanks a lot.
@curiores111
@curiores111 Жыл бұрын
Thank you 💖
@timschafer2536
@timschafer2536 4 ай бұрын
This was so good.
@Nopaisti
@Nopaisti 2 жыл бұрын
What a great and clear video!
@curiores111
@curiores111 2 жыл бұрын
thank you, friend.
@jeremysrobotics191
@jeremysrobotics191 3 жыл бұрын
best tutorial ever!
@RudolfZobel
@RudolfZobel 3 жыл бұрын
Really good explanation 😉
@josepablolopezaguado6191
@josepablolopezaguado6191 3 жыл бұрын
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!
@kamleshpanchal8661
@kamleshpanchal8661 4 ай бұрын
Programming is best with practical experience . 👍👍👌👌👌
@satchelsieniewicz5824
@satchelsieniewicz5824 2 жыл бұрын
this is realllly fantastic!
@torstenhenke4294
@torstenhenke4294 2 жыл бұрын
Best i ve ever seen -thx a lot
@ziadzakaria8206
@ziadzakaria8206 2 жыл бұрын
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
@johanmauriciofranciscososa4779
@johanmauriciofranciscososa4779 3 жыл бұрын
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
@DustanWebb
@DustanWebb 2 жыл бұрын
WOW such a great video and explanation. Def just gained a subcriber.
@curiores111
@curiores111 2 жыл бұрын
Well thank you Dustan. Hope to see you around.
@leonk_1986
@leonk_1986 Жыл бұрын
I need more videos.It's awesome.
@frankdearr2772
@frankdearr2772 28 күн бұрын
great topic, thanks 👍
@luckyshadowtux
@luckyshadowtux 2 жыл бұрын
one of the best tutorials I have ever watched. Have you thought of doing an MPC video?
@curiores111
@curiores111 2 жыл бұрын
You're too kind, thank you. Yes, I have, among a couple others. Thank you for the suggestion.
@co4erol
@co4erol Жыл бұрын
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 Жыл бұрын
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.
@Huapua
@Huapua Жыл бұрын
Wow, great tutorial!!
@It4heath
@It4heath 8 ай бұрын
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.
@user-rc4zt1kl7k
@user-rc4zt1kl7k 6 ай бұрын
your video is wonerful, helps me a lot thank u very much
@ricardogarciasaez6337
@ricardogarciasaez6337 Жыл бұрын
Just LOVE YOU!!!
@omargamal665
@omargamal665 3 жыл бұрын
great video.. I wish you continue that great work
@mandregomes
@mandregomes Жыл бұрын
Very nice and good explanation.
@JuanOrtiz-di9rr
@JuanOrtiz-di9rr 3 жыл бұрын
Que bello video y que hermosa exposición
@youssefahourri3245
@youssefahourri3245 2 жыл бұрын
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
@simondetlefsen3607
@simondetlefsen3607 Жыл бұрын
You are amazing 👏 Now I'll have to study more 😅🇦🇺
@johnfrancis9668
@johnfrancis9668 3 жыл бұрын
Thanks, worked like a charm
@curiores111
@curiores111 3 жыл бұрын
Glad to hear it!
@sermadreda399
@sermadreda399 3 жыл бұрын
great video thank you for sharing
@justinberdell7517
@justinberdell7517 Жыл бұрын
Awesome video!! Make some more!!
@dalekurita1075
@dalekurita1075 2 жыл бұрын
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 ....
@curiores111
@curiores111 2 жыл бұрын
😎
@ApteraEV2024
@ApteraEV2024 8 ай бұрын
👏 👏 👏 really good instructions, 🙏 thanks ❤️
@Windy2468
@Windy2468 2 жыл бұрын
This is so good omg, thank you
DC motor PID speed control
15:29
Curio Res
Рет қаралды 220 М.
This Component solves "All" Motor Problems?! (Motor Encoder) EB#58
11:34
Just try to use a cool gadget 😍
00:33
123 GO! SHORTS
Рет қаралды 85 МЛН
Final muy increíble 😱
00:46
Juan De Dios Pantoja 2
Рет қаралды 34 МЛН
ОДИН ДЕНЬ ИЗ ДЕТСТВА❤️ #shorts
00:59
BATEK_OFFICIAL
Рет қаралды 6 МЛН
What is Encoder?
8:40
RealPars
Рет қаралды 943 М.
How to use encoders (Optical, Hall Effect, Quadrature)
20:44
Will Donaldson
Рет қаралды 101 М.
Build a Custom Servo Motor with a DC Motor
42:08
DroneBot Workshop
Рет қаралды 125 М.
How to control multiple  DC motors with encoders
9:19
Curio Res
Рет қаралды 62 М.
Turn any DC Motor into a Servo Motor
25:24
How To Mechatronics
Рет қаралды 277 М.
Hardware Demo of a Digital PID Controller
2:58
Gregory L. Holst
Рет қаралды 975 М.
Adventures in Science: How to Use Rotary Encoders
12:05
SparkFun Electronics
Рет қаралды 222 М.
Magnetic rotary encoder vs stepper motor accuracy
5:41
Matthias random stuff
Рет қаралды 140 М.
How to Control a 12V Motor with Arduino: Easy Wiring & Code Examples
44:13
Using Rotary Encoders with Arduino
32:10
DroneBot Workshop
Рет қаралды 699 М.
Обзор Sonos Ace - лучше б не выпускали...
16:33
Hisense Official Flagship Store Hisense is the champion What is going on?
0:11
Special Effects Funny 44
Рет қаралды 1,3 МЛН
Ждёшь обновление IOS 18? #ios #ios18 #айоэс #apple #iphone #айфон
0:57