Arduino Tutorial 37: Understanding How to Control DC Motors in Projects

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is polemic order from top tech boy comm and we are here today with lesson number 37 in our legendary our new our improved Arduino tutorial series today we're gonna learn how to control a DC motor with the Arduino so I will lead you to pour yourself a most tall and strong cup of iced cold coffee that's ice cubes with strong coffee poured over the top top very refreshing and I will need you to get your most excellent Eli go super starter kit what you don't have one look in the description down below there's a link that will take you to Amazon 35 bucks and you can get this kit has a boatload of components and it is the kit that I'm using for this series of lessons it does help a little bit if we are working on identical hardware helps you to follow along a little bit better or maybe a little bit easier ok let me get out of your way and show you what you will need for today's project you will need your Arduino you will need the power supply out of the a Lego kit you will need your motor controller it is the l293d chip which is in your kit let me warn you there are two chips that look identical one is something else like a shift register and this one is the motor controller how do you know which is which look very carefully on it and read the ID l293d the one that's labeled l293d is the one that you want to use you will need your DC motor and you will need your fan okay a couple of very important introductory comments related to controlling a motor from the Arduino first of all you cannot just go plug the motor into the Arduino like you do LEDs or small servos or some of the simpler things like that because DC motors in general require a lot more current than what the Arduino can put out so if you hook the motor up to the Arduino digital pins there are two things that can happen the first is that motor can to draw more current than what the Arduino pins are designed to put out and in that case you very easily can burn out that Arduino pin if not your entire Arduino the second thing is the motor acts as an inductor like a coil of wire and one of the laws of electrical engineering or physics or whatever is that you cannot instantaneously change the current in a coil of wire or an inductor so if you're running this thing and turn it off that creates a huge voltage spike that can actually be sent back into your Arduino and burning it out and so you always have to have something between the Arduino and your motor now one of the things like back in the day old school the way I was taught to do it is you would have the Arduino control a transistor and then that transistor control the motor and then put a diode reverse bias around the motor to take care of this induction problem and in fact you can do that but nowadays there is a much simpler way and that is to use a motor controller chip which we have shown here a couple of other warnings that is I'm teaching you the concept of using a motor controller in a DC motor this is suitable for our Arduino this power supply that comes with the e Lego kit this motor controller in this motor you could not go out and get an 8 horsepower motor off of something like an evaporative cooler and come in there and control it with this little chip in this little power supply power management and heat management become very very important on projects like this so if you're going to use a bigger motor you've got to make sure that this motor controller can handle the amount of current that the motor needs and then secondly you have to make sure that your power supply can provide that amount of current and so you have to match your power supply to your motor controller to your motor so that's a warning if you go out and do some else okay got it that's a warning but for this case these will work the motor in the kit with the motor controller in the kit with this power supply will all work nicely together so that is that I think there is one other thing that I need to warn you about and I've worn this I have warned you about this in the past and that is when we start having multiple things like we have the Arduino we have the power supply and we have the motor controller all three of those have to share a common ground and the thing that typically happens like a mistake I see over and over is you maybe hook the motor to the motor controller then you hook the motor controller to the power supply so the the powers of the the input voltage on the motor controller to the output voltage of the voltage source in the ground to the ground and you have that all hooked up and then you come in and start bringing control pins from the Arduino to then control this chip what is the problem this Arduino also has a ground in that ground has to be hooked to the other ground so when you have a project that has a bunch of chips or a bunch of components working together they have to share a common ground okay so you got to make sure that you hook the ground of the Arduino up to the ground of the chip and up to the ground of the power supply okay 10-4 right okay so let's go ahead and do that because that's probably something that yeah that's very important so just so that we don't forget it I'm going to get the GND off of the Arduino and I'm going to bring it to this second from the bottom row the second from the bottom row and you also have to make sure that your power supply is plugged in correctly if you look very carefully the power supply has a pin that's labeled positive right here that needs to go into your rail on your breadboard that is labeled positive the negative coming off of your power supply needs to go into the second Rowe which is labeled negative in that way if I'm hooking things up looking at this positive and negative on these rails they actually match what's coming out of the power supply the other thing about the power supply is there is a jumper this jumper can be hooked to the pins on this side or it can be hooked to the pins on that side and that will control whether you're getting 5 volts or 3.3 volts out we want the jumper down here we want the jumper to be going over this pin and this pin that are labeled 5 volts if you move it you're not going to get the 5 volts out so what have we accomplished so far we have the ground of the Arduino hooked to the ground of the power supply and that's something that we wanted to do up front so that we didn't forget it now what is this nonsense of the l293d motor controller let's take a look at that and let's see if we can unpack that and figure out what's going on I think this would be a pretty good view and the first thing that you need to see is you need to kind of understand on this chip let me find something handy to point with you have to understand that there's 16 pins there's pin 1 2 3 4 5 6 7 8 on one side and then you go around and it's 9 10 11 12 13 14 15 16 so you can see that up here and what you need to see and this is very important is this is sort of the way that pins are numbered a standard way of numbering most integrated circuits you start with 1 2 3 4 5 6 7 8 you go around the corner and then come back 9 10 11 12 13 14 15 16 so it's sort of circular its circular in nature and that is reflected up here starting at 1 2 3 4 5 6 7 8 and then 9 10 11 12 13 14 15 16 now the question is where is pin 1 is it this one or is it this one well the way you can tell that is there is a on the chip and if you look very carefully right here you can see a little notch and in case you can't see that perfectly I have some artwork right find the knotch find the notch in the chip to the left of the notches pin one then it goes down one through eight around and comes back nine through sixteen so you've got to make sure that you're plugged in like this with the notch to the left the knotch to the left all right in similarly way up there the knotch would be not drawn but the knotch not drawn on that particular one okay this notch is right there at the top okay so you can hopefully get that orientation now let's talk about what this chip does this chip will control the speed and the direction up to appropriately sized DC motors the the pins on the left side will control one motor and the pins on the right will control another motor and so since we're just doing one motor I'm just going to talk about the pins that are on one side on the left okay and you will notice up at the top that we start with an en one and that's pin one what Ian one does is it controls the speed of the motor you do an analogue right - Ian one which you remember remember analog right is pulse width modulation so in order to connect Ian's one to the arduino you need to use what you need to use one of the digital pins with the squiggly and so that would be like 1110 six five and three on my Arduino a digital pin that has a squiggly you need to use one of those of your choosing because it wants the analog function and then you can see that we have pin 2 and pin 7 10 2 & 7 or label I in one in in2 those are directions this is the way it works if you put too high and you put seven low it'll want run one direction if you put too low and seven high it will run the other direction so a digital right - - and a digital right - seven this or this will make it run forward or backwards we are cruising through this we know what one pin one is we know what pin two and seven are now let's look at pins 3 and pin 6 that's out one and out - that's what actually connects to the motor notice that this is not a ground you do not go in and ground this black wire separately the red pin goes to pin 3 and on the chip okay pin 3 on the chip and the black wire goes to pin 6 on the chip so that's how you hook this up that's the output the output from the chip that goes to the motor now pin four is simple enough that's zero volts that's the ground again that is the common ground this ground rail that you want to hook to down here now we're all the way down to eight which is just the positive voltage coming from the power supply so that will come down here now over on the other side of the chip you could do the whole thing again but let's jump in and let's get this thing hooked up is carefully as we can so let's start at pin 1 okay pin 1 on the motor controller where did we say that pin 1 needed to go - it needed to go to one of the squiggly digital pins on the Arduino I'm going to choose pin 5 that is a squiggly okay 10 5 pin 5 on the Arduino is going to go to pin 1 pin 5 is going to go to pin 1 on the l293d alright pin - right we talked about 10 - in pin 7 need to connect to digital pins and so I'm just going to take pin two on the motor controller and I'm just going to go to the adjacent pin four on the Arduino so two from the motor controller goes to four on the Arduino I still have another one I need to now go down seven right so I come over one two three four five six seven and seven also is a Direction pin two as a direction can seven as a Direction pin two went to pin four on the Arduino pin seven is going to go to pin three on the Arduino and so this is kind of neat five four three are the pins that we're using on the Arduino so we have the speed pin hooked up and we have the Direction pin cooks up now we need to hook up the motor so what did we say on the motor red two out one on the motor control chip that's pin three red to pin three see that up there and three is an output in black to output 2 which is pin six on the motor controller so we go one two three four five six like such to pin six on the motor control so far so good what else do we need to hook up well we need to hook up the ground we've hooked up the ground from the Arduino to the power supply ground we now need to ground and I am quickly running out of little wires okay we also need to ground the zero pin four on the motor control chip so we go over one can you see that one two three four so that fourth pin is going to hook to that negative rail that we created right that negative rail that we created all right now what do we have left to do we still have the V out the pen ate the pen ate the v motor needs to connect to the power supply positive side come out over to pin 8 on the chip pen 8 on the chip needs to go to this first real this bottom rail that is hooked to the 5 volts on the power supply so that is all looking pretty good I think I have most of this hooked up now you remember how we are always using our Pookie if you don't have any Pookie get some and I've made a little ring because I want to stand the motor up but as you stand the motor up make sure that you don't do anything that would rub on that shaft that's coming up the backside of the motor because if you put something on that it'll keep the motor from spinning so that's why I have a little ring and now I can press that motor down to hold it good and now I'm going to come in with the little fan and put the fan on it in the fan will help you see if the motor is running or not ok I actually really really think that I have this hooked up now I might just be crazy and might have all types of problems here but I do believe that I have this hooked up so what do we need to do now we need to go over and start doing some coding so let's come over to a nice code view you need to call up your bare minimum on your Arduino IDE have a little coffee and now the first thing we need to do is we need to set up our pins well pins are set with int oh why are you not seeing that okay this is too wide let me see if I can fix that okay I believe that will work okay so we're gonna do an int and the first pin was that motor speed pin and so that was we will call it speed pin and the speed pin where did it go remember that was pin 1 on the motor controller coming to pin 5 on the Arduino so speed pen is 5 on the Arduino got to keep that straight pin 5 on the Arduino then we have Direction pin 1 right direction pin 1 was pin 2 on the chip which came to which came to pin 4 on the Arduino so what that means is then door pin 1 on the earth we know it's pin 4 and similarly 7 on the 7 on the lemme check and make sure 7 on the controller chip was pin 3 on the Arduino it was pin 3 on the Arduino so we come back over here and we say derp 2 Direction 2 is 3 so I put pin 4 high and pin 3 low to go one direction pin for low and pin 3 high to go the other direction so I've got my pins set up three pins on the Arduino I have three variables I also I'm gonna need a parameter of M speed for motor speed that is desired right this is going to be a number between 0 and 255 0 would be completely off 255 would be completely on in numbers in between there would control the speed of the motor I will give you a little hint practically I found that I really can't run the motor at a write value an analog value of less than a hundred so for me if I right zero it stopped I've got to write a hundred to get it going and then between a hundred and 255 you get a nice smooth variation in speed so I will just hope this thing run so we're gonna go full blast to begin with at 255 okay we need to do our pin mode so I'm gonna go pin mode and what do I have I have speed ten is an output so I'm writing to it and then I have pin mode I have dur one which is also an output I'll give you a hint all these are going to be outputs pin pin mode and then I have dur to is going to be an output like that so speed pin dyrone indoor two are all outputs I'm going to start my serial port serial dot begin just because we might need to look at that in a minute 9600 always good to start your serial port all right I believe I have my pin modes done all right so now in order to turn the motor on what do we need to do well we need to tell it a direction first so I'm going to say digital right and we'll go dyrone high okay well if dur one is high what's your dirty B it should be low digital right there to look and so these are the pins that you have to high seven low or too low seven high so if we change these we've got to change both of those alright so we have set the direction now we need to do what we need to turn it on set a speed so I'm going to do analog bright what I want to write to the speed pin and what do I want to write I want to write M speed for motor speed in speed such now I need everyone to hold their breath and we are going to turn this on we're gonna download it and hope this thing works giddyup did you see that the motor is spinning boom the motor is spinning and if I look at it it is spinning in the this is kind of hard this is spinning in the counterclockwise direction okay it is spinning in the counterclockwise direction okay if I want to make it go clockwise what would I do well I would come up here on my directions and I would set this low and I would set this high okay so this is counterclockwise this is counterclockwise and now I'm going to download it again boom it's going clockwise I have a motor and I am controlling its direction from the Arduino mm-hmm yeah and you can too okay so we got the direction going what do we want to do now we want to play with the speed so let's come all the way down to 100 and let's see if we can start it there so I put a hundred in oh it's trying you see I had to cheat a little bit and give it just a little help to start I wonder if it will run at 75 okay it's 75 it just can't even quite run even if I try to help it a little bit so it looks like probably a hundred it would be slow speed and then if we go up to 255 it'll be going full-speed so let's try that 255 it starts itself in full speed what if I go like 175 stops and then okay 175 it's running slower but it can actually start itself all right we are controlling both the speed and the direction of a DC motor and so that is a pretty big step I just kind of had an idea do you notice like one of the things you see is let's go back to a hundred and let's put a hundred in you see it won't run but if I give it a little help once it'll run at a hundred but it won't start at a hundred but you see so if you put in a hundred that's too slow it won't work what could you maybe do I want you to think what could you maybe do where you could have it start you know and still be able to use the speed of a hundred and not have to help it okay what you need to do is you need to give the motor a little kick you got a little kick in the rear end to get it going so how would we do that what if down here in the void loop I started with an analog right and I would analog right to speed and and what if I gave it like a little kick in the rear of 255 to get it going and then like delay 100 milliseconds and then come in okay and after I do this then I would put this back at M speed and then of course I need to put a longer delay so that it stays at that higher speed so then I would do a delay and let's just put like fine a delay of five thousand so it would stay there at the higher speed and then it would look loop through again but let's see if this would work I want to see if I can start it okay do you see how now it's running slowly and it started itself how did it start itself I gave it this kick and then after I gave it this kick and then after giving it the kick I put the desired speed so this would be interesting I wonder if I could run it now at like what if I could run it at 80 using this technique okay yeah Oh almost it just won't quite I wonder we know a hundred would work I wonder if Nani if it would keep spinning so if you give it a little kick you can actually run it at 90 okay you can actually run it at 90 now what I want to know is what is the smallest kick I can give it to actually get it started so by small kick I mean small time what if I made this 50 would that still a kick of 50 milliseconds would that still get it started let's see yeah the 50 will actually be enough to get it started of course I always start getting obsessive-compulsive how small can of a kick can I get it to have it still kick start let's see 25 milliseconds yeah that was enough so even with 25 milliseconds that's enough to kick this fan where it will start and then it will run at the low speed boom okay this has been a really exciting project you are now controlling the speed and the direction of a DC motor again with this you could put another one on the other side that would work you could run two motors at once you could control the speed of two motors and you could control the direction of rotation of two motors so you could see like what if you had two wheels if you ran them together they would go forward if you ran one forward and one backwards you would turn if you ran one faster than the other one you could make gentle turn so you see there's some pretty cool stuff that you can do with what you've learned here if you're thinking kind of about robotics or things like that okay this to me has been a really exciting lesson so I am going to give you your homework for next week and what your homework is is imagine if this were a real fan you wouldn't want this thing to turn over and then you see if it turns over and it's trying to run you could maybe catch the motor on fire or maybe as its turning over it could run into these wires and do all kinds of bad things okay so what your assignment is is that I want you to put a tilt switch on here that if if this thing tilts over I want you to stop the motor but at the point that it goes back up right and it's not tilted I want you to start the motor again so this would be something that your fan turns over if it turns over you want to stop the fan okay so that is the assignment for next week I will give you a hint you have a tilt sensor in your Arduino and if you've been playing along at home lesson number 36 talked about that tilt sensor so that is your assignment go off do that and then again what annoys doesn't annoy me but what do I find disheartening I wonder if anyone ever actually makes it to the end of these videos because I look on my statistics in the average view time on my videos is like three minutes it's like hit and run man does anybody stay around till the end of the video I will show you today's secret word is rose this is my yard in Africa and up there in the pink is rose and she keeps the weed out the yard and will throw and Ronald over here rose and Ronald keep the yard looking nice and so the key word for you to put into the comments to let me know that there is one person out there that watches the whole video go into the comments and leave secret word words rose and Ronald okay guys this has been a really fun project you have your homework for next week and then I will come back next week and I will do it with you but you need to go ahead and do it before the video comes out and do it on your own Paul McWhorter from top tech boy comm and I would appreciate you guys giving me a thumbs up I would appreciate you subscribing to the channel make sure you ring that bill hit that Bell so that you will get notifications when new material comes out and then think about sharing this with other people it's a huge encouragement to me when you guys leave comments down below read every single comment hope to hear from you again top tech boy comm I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 143,191
Rating: undefined out of 5
Keywords: DC Motor, Control, Arduino, L293D
Id: fPLEncYrl4Q
Channel Id: undefined
Length: 31min 24sec (1884 seconds)
Published: Tue Nov 19 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.