Controlling DC Motors with the L298N H Bridge and Arduino

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we'll be working with the L 298 n 8 fridge motor driver you'll learn what an 8 fridge is and how to use the inexpensive L 298 n module with an Arduino to control the speed and direction of a pair of DC motors we'll even build a simple robot car that you can control using a joystick so stay tuned and welcome to the workshop hey welcome to the workshop today we're going to be working with DC motors and we're going to be controlling them with a component called an L 298 n H bridge we're going to look at how DC motors actually work and even a tiny bit of the history of because I think that stuff's interesting but after that we'll discuss what an H bridge is if you're not sure of that once we do that we'll examine the L 298 n the L 298 n is a really common H bridge module that you can use on its own or with an Arduino and we're going to be doing both after we do a couple of experiments with it we're going to actually do something amusing and build a little robot car that we can control with a joystick using an Arduino and l2 98n so let's get at it let's take a look at DC motors DC motors or motors that operate on direct current as opposed to motors which operate on alternating current the first motor was invented by a Hungarian physicist named an EOS daedalic in 1827 and he called his device an electromagnetic self rotor interestingly enough the self rotor that you see pictured here was constructed in 1827 by Jeff Lake and today can be found in the museum who applied arts and budapest and the device is still working a hundred and ninety years after he constructed it now the self rotor wasn't a particularly practical motor the first practical motor was invented in 1832 by a British sign named William sturgeon DC motors are available in several different configurations from tiny little motors to absolutely huge ones small DC motors are very inexpensive and therefore these are ideal for hobbyists just like us for all of our little contraptions we can use DC motors to construct robot faces that can be used to create quad copters they're also often used in model planes and boats and myriad of other applications so how exactly does a DC motor work well here you'll see a diagram of a very simple DC motor the shaft of the motor the part that does you're rotating is referred to as an armature and on the armature you will see a couple of coils of wire these coils are connected to the commutator that's that little copper ring at the very front of the armature that you see the connections to the commutator are called the brushes where the positive and negative voltage are applied on the outside of the motor you'll see a couple of permanent magnets arranged in opposite magnetic polarity now when DC current is applied to the commutator it sets up a magnetic field inside the coil the coil magnets interact with the permanent magnets causing the armature to rotate now as the armature rotates the polarity is continually reversed causing the magnetic field to be reversed and the rotation to continue this type of a motor is referred to as a brushed motor as opposed to a brushless motor and this is the type of a motor that we will be experimenting with today so let's take a look at how an h-bridge actually works in order to simplify things I've drawn an H bridge using four switches in real life the switches would probably be replaced with transistors although you could build an experience of switches if you really wanted to now why is this thing called an h-bridge well if you look you can distinctly see a letter H in the configuration with the motor sitting in the bridge part of the letter H thus the term H bridge so how does this all work well you have a positive voltage applied to the top of the experience circuit and a negative voltage applied to the bottom now watch what happens when we connect these two switches the positive is applied to one side of the motor and the negative to the other side and in this case our motor will spin clockwise now let's take a look at what happens when we close the other two switches you will see that a negative voltage is now applied to the one side of the motor and a positive to the opposite side causing our motor to spin counterclockwise this is how an H bridge functions now there are a couple of different styles of l2 98n modules that you'll run across and i want to show you the differences between them they work exactly the same and you can use either of them in the experiments that we'll be doing but I just didn't want anyone to get confused if their module didn't look like the one that I was using so here's the one that I'm using and you'll see all of the digital inputs with the enables on either side of this connector here is another common module and here are the digital inputs the enables are actually here and their jumpers so they're always enabled but you can just pull those jumpers and use them exactly in the same way there's also a 5 volt jumper which I've got off on my module that's over here the 5 volt jumper on this common module is over here then the motor connections the two green connections on this module are motor a and motor B you will find two connections on this module for motor a and motor B and finally the connections to the 5 volts the 12 volts and the ground are all done in the middle of this module here whereas on this module you'll find the same three connections on this connector so otherwise these modules are identical and you can use either one for these experiments and they'll work perfectly fine let's take a close look at the l2 98n module now the module that you have may be physically different than the one that I'm showing here with the connectors in a different position but they'll be labeled the same and they'll function identically now first we'll look at the power connections there's an input for the motor power which can be anywhere from five to thirty five volts there's also a common ground which is common to all power supplies then there's a five volt input now this five volt is to drive the logic circuitry on the board and is actually optional it's possible to use the motor power to also supply the five bolts after that we have two connections for our motors motor a and motor B now here's the jumper that we use to select whether we're using the optional five volt supply or whether we are using the internal one in this illustration you can see that the jumper is open up just got it hanging there so we are using the optional five volt input the way I've shown it after that we have the logic inputs of the controller there are three logic inputs for motor a the enable line input one and input 2 and then motor B has the similar connections enable input three and input four now here's how the inputs work I'll show you motor a motor a is enable line when set the five volts enable the motor when it is grounded or set the logic zero the motor is disabled it can also be fed pulse width modulation signals and this can control the speed of the motor and we'll talk about PWM in a few minutes now motor is input one and input 2 work as follows if you apply 5 volts to input 1 and ground to input 2 the motor will spin in a forward direction if you reverse that and apply ground to input 1 and 5 volts to input to the motors will run in Reverse and of course the motor B inputs work in exactly the same way now pulse width modulation may sound with a complicated technical subject but when it comes to controlling motors it's actually pretty easy to understand we simply send a series of pulses to the enable line in our motor the wider the pulses the faster the motor will spin so for example if we send a series of short little pulses to the motor the motor will spin slowly if we increase the width of it the motor will spin faster increase the pulse width a bit more and the motor will spin even faster if we want to bring the motor to a stop we'll just stop sending pulses and hold the enable line low this will disable the motor if on the other hand we want the motor to run at full speed we'll stop pulsing it and hold that line high and this will cause the motor to be enabled constantly and spin at full speed in order to show you how the l2 98 works I've set up a little experiment on my workbench what I've done is a-tickin my l2 98 I remove the 5 volt jumper so that I can power this with an external supply I've connected a power supply to it what I've done is I've used a little buck converter that has reduced my 12 volt power supply down to 7 point 4 volts now you might be wondering why 7 point 4 volts well the reason for that is that the two transistors have active switches in the l2 98 are each going to have a voltage drop of point seven of a volt so what I need to do is apply one point 4 volts more than my motors actually require and since these are 6 volt motors I have given it 7 point 4 volts in addition to the 7 point 4 volts I've attached the 5 volt power supply I'm just using my bent to supply for that for now I've attached two motors to it motor a and motor B and I've also run the 5 volts over to my fog breadboard at this point I then taken all of the inputs from the L 298 and wired them up to the breadboard so that I can experiment with the inputs and send them to either ground or 5 volts okay for the first experiment this is how I've wired the inputs I've set an able 1 to 5 volts which is a high input number one is also at 5 volts and input number 2 is a ground these are the lines of control motor number a for motor B I've taken input number 3 and set it to 5 volts input number 4 has been grounded and enable number 2 has also been grounded so when I do this what will happen is that motor a will move forward motor B however won't move at all and that is because it's an able line is set to ground and you can see that as follows okay for the second experiment I'm going to take enable 2 which controls motor B and I'm going to move it high as well in this case both of our motors will move forward next I'm going to reverse the inputs for motor number a so I'm going to take input number one and ground it an input number two and bring it to five volts this will cause motor a to run in reverse while motor B still runs in the forward direction [Music] and last but not least I've taken both of the enable lines and grounded them so enable number one is grounded and so is enable number two in this configuration both motors will simply be off and as you can see they are so now that we've shown how the l-29 t8 works with a breadboard let's add in our glee note of the equation and see how we can get it to control the speed of the motor as well as the direction so let's bring in our dueƱo into the pit here I've taken away the 5 volt power supply and I've used the 5 volts and ground from the Arduino Uno to power the L 298 and logic circuits I've hooked the inputs of the L to 98n to a series of the digital outputs from the Arduino and the hook-up is as follows I've taken the enable one line on the L 298 and connected it to digital output pin nine on the Arduino input one on the L 298 is connected to digital output pin eight input two is connected to output pin 7 and put three two output pin five input 4 to output pin 4 and the enable to lie on the L 2 98 is connected to the Arduino digital output pin 3 again you could use different output pins on the Arduino if you're willing to modify the sketch but just keep in mind that both of the enable lines need to be connected to a digital output pin that is capable of pulse width modulation so let's look at our Arduino sketch to do our motor demo now we're going to start off by declaring a number of integers for motor a and for motor B and these are just the inputs to the l2 98n the ena and ENB are the enable lines for motors a and motor B and then I am one in2 in3 and ions or are simply inputs one two three and four to the board now again you could use different pin numbers if you wish by just defining different numbers for these integers however keep in mind that enable a and enable B have to use pins that are capable of pulse width modulation pins 9 and pin 3 are capable of PWM so if you're changing this keep that in mind after we've defined these integers we're going to just go through the setup routine and set all of them up as outputs because all of these are outputs from the Arduino because we're feeding these into the l2 98 ends then we're going to start with one of two functions our first function is called demo1 and demo2 on the motors in both directions at a fixed speed so the first thing we're going to do is we're going to turn on motor number a what's the input 1 and input 2 lines then we're going to set the speed of motor a we're going to be using the analog write command now analog write will write pulse width modulation signals out to PWM and able pins on the Arduino and it can have a range of 0 which is basically off to 255 which is full speed we're going to set these two analog right to 200 so it's going to be near full speed but not quite and then we're going to do the same thing for motor B we're going to set it on in one direction and we're going to set it to also do an analog right of 200 now we're going to run the motors of that speed for 2 seconds so we put a delay of 2,000 milliseconds in here and then we're going to change the motor directions so we're going to take inputs 1 2 3 & 4 and set them to reverse the motor so we do that over here and then the motor will continue to run at the speed of 200 because the analog right is still 200 we do that for 2 seconds and then we're going to turn the motors off by setting inputs 1 2 3 & 4 all to a value of low and that's basically our first demo now we have a function for demo number 2 and what this is going to do is it's going to run the motors across all the range of possible speeds it's going to run it up to the full speed and then run it down to low speed so first of all we're going to go and set the motors on again in the forward direction so we said inputs one two three and four accordingly and then we're going to take a loop and accelerate it from zero to maximum speed now in this loop we're going to take an integer we call AI and we're going to loop it between the values of 0 and 255 and so we're going to do an analogue right to enable a and enable B it's going to start off with these values being zero and then we're going to delay it for 20 milliseconds and it's going to increment it and the values will be 1 it will delay for 20 milliseconds etc etc all the way until it gets up to 255 once we do that we're going to do the opposite thing we're going to run a loop except we're going to decrement it so this is the same thing except we're starting at a value of 255 we're working our way down to a value 0 and we're decrementing the loop and so again we're going to start off with an analog right of 255 to e to the motors delayed by 20 milliseconds and then go to 254 delay by 20 milliseconds etc until we get down to 0 and then finally we're going to write digital rights to all of the pins low again to turn off the motors and so now that we've defined our two functions in the loop all we simply do is we run demo 1 and then we delay it for a second and then we run demo to delay it for another second and then the loop repeats itself infinitely so this is basically our motor demo so let's take a look at it working all right I've loaded the motor control sketch onto my Arduino and I've temporarily unplugged the USB cable from the Arduino because this sketch will start immediately as soon as the Arduino is powered up otherwise everything is connected up about my power supply connected to the l2 98n board the motors are connected to it as well and the board is connected to the Arduino so as soon as I power this it should start our demo sketch so let's take a look at that [Music] and it runs through it you see it'll repeat it it starts by going forward and it goes backward and then it gradually comes up to speed I'm going to gradually reduce the speed so that's our demo now let's expand upon our circuit and connect the couple of potentiometers your variable resistors up to the Arduino Uno this will allow us to manually control the speed of each motor now the value of each of these potentiometers is not particularly important as long as they are 10k or higher they should work you also want to get linear taper potentiometers and these are the most common type anyway now one end of each pot is connected to the ground and the other end of it connected to 5 volts and my hookup I use the spondylus breadboard to simplify the connections although that isn't illustrated here the wiper of each pot is connected to one of the analog inputs on the Arduino and I connected pot a to analog input 0 and pot B to analog input 1 again if you wish to use different analog inputs that would be fine you'll just need to modify this gets accordingly so now that we've hooked the two potentiometers up to our Arduino let's take a look at the sketch that we're going to be using now our sketch starts off in the same way as the last sketch we define a number of integers for the output pins on the Arduino that we're using as input for the l2 98n then we've got a couple of new integers over here speed control 1 and speed control 2 and these are defined as a 0 and a 1 these represent the analog inputs in the Arduino now if you wanted to use two of the other analog input pins just change the value of a0 and a1 and you can do that now we have another couple of integers we've defined and these are motor speed 1 and motor speed 2 which represents the speeds of the 2 different motors we'll initialize these with a value of 0 then we go into the setup which is identical to the last program we set all of the digital outputs and define them as outputs we don't need to set the analog inputs up as inputs because by nature they're already inputs then everything is happening in the loop we'll set the motors to a forward to action by writing to the in10sity meters using the analog read command now the analog read function uses the internal analog to digital converter that's built into the Arduino this is a 10 bit analog to digital converter so we're going to get values from 0 to 1023 now if you recall when we do an analog right to send pulse width modulation out to our enable lines we can take a value from the vero to 255 so we've got to convert this range of 0 to 1023 to arrange a 0 to 255 and there's a couple of ways we could have done this one way would have simply been to divide these values by 4 and that would have actually worked as fine but I chose to use arduino z' maps function and if you haven't used the map function before it's a good function to get familiar with what map does is it takes one range in our case 0 to 1023 and maps it out to another range of 0 to 255 this really works well when you have two ranges that don't evenly divide into each other and so I've set motor speed to be mapped to a value of 0 to 255 and then the last thing I do here is I have found that the motors actually started to buzz when I got to a very very low value and so what I've done is I simply included these if statements and in the if statements I say that if the motor speed is anything less than 8 then we'll just call it a zero because the motors aren't moving down at that speed anyway they're just making an annoying buzzing sound you may want to adjust this value of 8 to suit your own particular motors and then finally we do an analogue right of motor speed to enable a and enable B and that sends the pulse width modulation out to our motors and this is in the loop so if this continues to repeat read our potentiometer values and send the values of to the to enable lines and that's it for our sketch so let's walk hit an action okay now that we've hooked up the two pots load our sketch let's try it out we'll turn the pot for motor number eight [Laughter] and I can adjust EC motor Peas same thing [Laughter] so now that we've seen how we can use two potentiometers to control two motors with an Arduino let's take it a step further our two motors you've noticed have been mounted onto a robot car base and what I'd like to do is actually drive this little car around the room now two potentiometers are going to be a very difficult method of controlling a car plus I have no method of actually reversing it so what I've decided to do is replace the two pots with a joystick now a joystick essentially is gets two potentiometers one that controls the vertical or y-axis and the other one that controls the horizontal or x-axis a joystick also contains a momentary contact switch but we're not going to use that in this particular hookup now let's take a look at how I've hooked the joystick up essentially what I've done is just replace the potentiometers with it a joystick also has two pins one labeled VCC and one labeled ground I've connected VCC to my five volt power supply from the Arduino and I've connected the ground of course to the ground line now the vertical output I've connected to analog input a zero and the horizontal output is two analog input a one again just the same way I had the potentiometers now what I've done is I've arranged a joystick so that when I push the joystick forward the motors will drive the car forward when I pull it backwards the motors of course will drive it backwards when I move the joystick a bit to the left the car will steer to the left and when I move it to the right it'll steer to the right as you can see over here I'm going to push the joystick forward and are going my motors and when I pull it backwards or the other direction if I move it up to the left one motor spins faster than the other and I move it to the right the opposite effect occurs so now that we've looked at that let's take a look at the sketch that makes all of this possible and then we'll take a spin with our little car and drive it around the room so here's the sketch I've come up with in order to control my little car with a joystick now as with all the other sketches you can find these on rambha workshop comm along with a complete explanation of each sketch but I'm going to go over this one with you right now now we start off the same as we have with our other sketch we've defined a number of integers and these are for the inputs and the enable lines to the l2 98n then we've got the analog inputs of the Arduino which I've mapped out to joy Burke and joy horse for the vertical and horizontal potentiometer in the joystick as with our last sketch we define a couple of integers to represent the motor speed for each motor and we initialize them as zero we've also got a couple of new values here for the vertical and horizontal positions of the joystick and they're initialized at 512 now remember that the analog to digital converter in the Arduino will convert the input voltage to values between 0 and 10 23 so 512 is pretty well in the middle of the range then in the setup we go and initialize our pins as outputs as we did before and we'll also start our motors off both disabled with enable a B and lo and enable B being low and also set so the motors will spin in a forward direction then we go into the loop now we're going to get the joystick positions and put them into the values that we define for joystick positions using an analog read command just like we did with the potentiometers in the last sketch now remember the vertical joystick is the one which determines whether we're going forwards or backwards if we push it all the way up we want to go forwards if we pull it all the way back we want to go backwards also the more we push it up or the more that pull it down the faster we want to go so we're representing both speed and direction with this joystick now the vertical position will be above a value of 512 if we want to move forward and it will be below 512 if we want to go backwards however we have to consider that most joysticks are not perfect and that the variable resistors are potentiometers inside them have a little bit of a tolerance so I've allowed about 10% and I've decided that any value above 564 means that we are want to go forward where there are a value of below 460 determines were going backwards so in the first case here I evaluate the values below 460 we want to go backwards we'll set the motor directions backwards and then we'll go and determine the motor speed now in this case as we pull joystick down the numbers were reading on the analog input are going to get lower and lower yet we want our motor to go faster and faster so we need to do a bit of math so it takes the value and I subtract 460 from it that's going to give me a negative number and so what I need to do is multiply that by negative 1 in order to make it positive then after that I use the map command as we've seen before to map the values between 0 and 462 values between 0 and 255 so I can drive my motors with pulse width modulation now in the case where the vertical potentiometer is reading above 564 we are going forward so again we'll set our motors direction to forward and we'll determine the motor speed with a map command we don't need to do the fancy math we did before because as we push the joystick forward the values are going to go up and up so we'll just map the values of 564 to 1023 to values between 0 and 255 if it doesn't evaluate to either the previous two conditions then the motor stopped and we'll set the motor speeds to zero now we'll do the steering and we do that with the horizontal potentiometer again we use the criteria of below 460 or above 564 if it's the low 460 we do basically the same thing we did before we do the mathematics and convert it to a positive number and then we'll map that position to a value between 0 and 255 now we do a little bit of math after this because remember when we want to turn left what we want to do is in the right motor faster and spin the left motor slower so motor number 1 is our left one and motor number 2 is our right one so on motor number one we actually minus this value from the current motor speed which we determined earlier on motor number 2 we add the value to it so we make motor number 1 go slower and motor number 2 go faster now this will likely produce a value in many cases that exceeds 255 or goes below 0 so we have a couple of this statements here if the motor speed is below zero then it's equal to 0 whether whereas if the motor speed is above 255 then we'll equal it to 255 now if the horizontal is above 564 we want the turn right and we basically do the same thing we'll map the values and then we do the same math to add and subtract only now you'll notice that in this case we are adding two motor speed number 1 and subtracting from number 2 because this time we want to turn right again we make sure we don't exceed the values of 255 or 0 finally I have the same statements I've always had to prevent the motors from buzzing if the motor speed is less than 8 then we'll just consider it to be 0 and last but not least we use the analog write command to write to the enable lines on both of the two motors to suspend them at the correct speed and then Loup district heats itself as long as the Arduino is powered up and we can drive our car around the room so now it's time to give our little car a test drive as you've noticed I've put a very very long wire between the car and the L 298 and that's of course so I have a little bit of space to maneuver the vehicle so I'm going to stick this down on the floor and see if I can dry the best as I can I'm going to move the joystick forward and our car moves forward then we look backwards car moves backwards I'm going to move it a little off to the right and off and left and so I can steer the car - not that practical with this wire on it but as you can see it's actually working the robot car [Music] now who said Arduinos can't be fun okay so that about does it we've learned quite a bit in this video haven't we we've learned what a DC motor is how it works even a little bit about the history about DC motors we've learned how an H bridge works and how we can use the L 298 n H first controller with an Arduino and even without an Arduino to control a pair of motors we've taken that knowledge and we've even built this cool little robot car that we can drive around the room using a joystick so I think we've covered quite a bit I hope you've enjoyed it if you did enjoy it please consider subscribing to the channel and if you have any comments please leave them below the video if you need the code for any of these projects visit drone bot workshop comm where you'll see a detailed article about everything we've covered as well as all the sketches you'll need in order to recreate all of these experiments yourself so thank you once again for joining me and I hope to see you soon in the workshop take care [Music] hm [Music]
Info
Channel: DroneBot Workshop
Views: 899,853
Rating: 4.93293 out of 5
Keywords: L298, L298N, Motor Controller, H-Bridge, Arduino (Computing Platform), DC motor, L298N Dual H Bridge, Robot Car, H Bridge, Joystick, Arduino Uno, Arduino Sketch
Id: dyjo_ggEtVU
Channel Id: undefined
Length: 36min 41sec (2201 seconds)
Published: Sat Mar 11 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.