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 dueo 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 2000 milliseconds in here and then we’re going to change the motor directions so we’re going to take inputs – 1, 2, 3, amp 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. Amp. 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.
arduino double Video
arduino double news
Low cost laser RangeFinder (Time of Flight ) Distance sensor for Arduino – Full tutorial – Advanced Imaging Pro
Posted on Tuesday September 10, 2019Low cost laser RangeFinder (Time of Flight ) Distance sensor for Arduino – Full tutorial Advanced Imaging ProLow cost laser RangeFinder (Time of Flight ) Distance sensor for Arduino – Full tutorial. The VL53L0X is a Time of Flight distance sensor like no different you’ve … … Continue Reading »
Posted on Wednesday September 11, 2019“Open Despite” promotes student voices through choreography Macalester College The Mac WeeklySince the new Theater and Dance Building opened its doors in January, a refreshing new era has begun for its namesake department. The annual Spring … … Continue Reading »
Posted on Thursday September 05, 2019Mesh Bed Leveling – All You Need to Know All 3DPMesh bed leveling is capable of giving you a smooth, even first layer, even without special hardware. Dig into this detailed guide where we explain this leveling … … Continue Reading »
arduino double Social