How to control a DC motor with an encoder

preview_player
Показать описание

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:
2. Motor Driver - TB67H420FTG Dual/Single Motor Driver Carrier:
3. Microcontroller - Arduino Uno:
(okay actually I used an Elegoo Uno, but the Arduino descriptions are better :)
Рекомендации по теме
Комментарии
Автор

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.

Ben_Lucy
Автор

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.

rokonuddin
Автор

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

cristianotulio
Автор

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

andalibrahman
Автор

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

Ben_Lucy
Автор

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

luccarodrigues
Автор

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

TricksterJ
Автор

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!

lightman
Автор

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

Merfnad
Автор

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:

curiores
Автор

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.

Greebstreebling
Автор

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!

lucasinacio
Автор

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!

jeffdawson
Автор

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

bleach
Автор

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

multilaton
Автор

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!

MrStrangerr
Автор

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

eduardomeller
Автор

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.

Telectronics
Автор

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

coerol
Автор

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!

brandenchristiansen