 In the past couple of videos we've been building different parts of a water system. We went over water pumps, water solenoid valves, pressure sensors, water flow sensors, and more. In this video we're going to be going over this L298 motor driver which we'll be replacing our relay from our first video that we made with the water pump. The L298 motor driver, which I'm going to call driver, is like a controller for electric motors. Using D'Arduino you can give this driver commands. You can make sure the motor goes forwards or backwards and then you can control the speed of that direction going faster or slower. And you could do each motor individually. With our old system we had a relay to turn on the pump, on or off and that's all we had for control. This is going to be replacing that relay and giving us a lot more to control. Surprisingly the water pump we've been using has been able to build up enough pressure to cause leaks and problems with our project. So this is going to help us solve that problem because we can lower the water flow or bring it up depending on the situation. But at least we have that control now. It's not just on or off. One thing we can do with this pump unfortunately is reverse the direction of water like we did with the peristaltic pump from a previous video. If you want to see that I'll post a link in the description so you can see how that pump works and compare it to this one. For the L298N to control a motor the way it does it's going to need a couple of specific components on here that allow it to do it. The key component in the driver that allows it for motor direction control is the H bridge circuit. An H bridge circuit consists of four switches that can be turned on or off to control the flow of electricity. By switching these switches in different combinations the driver can control the direction of the current to the motor. If you want to learn more about H bridge switches I made a video where we actually made one ourselves using a normal switch and I go a lot more into detail on how to reverse the direction of the switch and I show you exactly how it works and how to make it yourself. For speed control the driver uses a PWM to regulate the amount of power delivered to the motor. PWM stands for pulse width modulation. PWM involves switching the power supplied to the motor on and off at a high frequency. So imagine if I have a light switch I'm flicking it on and off and on and off over and over and over and then I'm changing the time between the on and off and how long I'm leaving it on. And by doing that I can control the speed of a motor. In the future I'm definitely going to do a more dedicated video to this driver but today we're just going to get it working in this water system so we can replace that old and annoying relay. And so we can stop having water leaks because the pump just turns on right away and the pressure builds up and then table full of water. When it comes to wiring the L298N it can get a little bit complicated because there's a lot of plugs and each plug does a different thing. So quickly I'm going to put a picture up on the screen and we're going to go over what everything is and what everything does so that you can figure it out on your own instead of having to zoom in and try and follow why I'm plugging in things the way I do. You've got two pins here and two pins here. These are for both of your motors. Both motors have one and two and then three and four. It doesn't matter where you put the positive and where you put the negative. Just keep it consistent between the two. The reason for that is because we're going to be reversing it when we reverse direction of a motor so it doesn't need to be an exact thing. Then here you have three inputs. The first one is a 5V signal from the Arduino which is used to control different things here in the signal side. Then in the middle you have a ground which is going to be the ground for the power supply and then you have an input for the power supply because the motors are going to be using a lot more power than what the Arduino can supply. In my case this thing is built I think 5V to 32V so I'll put it up on the screen because I don't remember exactly and we're only using 24V so we're good. So our 24V will go here, then our ground for our 24V will go in there, then our pump positive and negative will go there. We're not going to use a second pump so we don't need this. Then we have 5V to the Arduino and then we have six plugs right here. The six plugs here are used for motor direction control and motor speed control. Starting at the bottom you have ENA. This is the speed control for motor number one. So this one gets controlled by the first pin at the bottom right here. Then after that you have IN1, IN2, IN3, IN4. These are used to switch the direction control of the motor and the code is going to make a lot more sense but I'm going to show you that we're only going to use IN1 and IN2 because we're only using pump number one or sorry, motor number one. Then after those four you have another ENB this time. So we had ENA for speed control for motor one. Now we have ENB for speed control of motor number two. Sometimes they come with these little, it's a little bit hard to see but on ENA and on ENB you have this little jumper. We're going to be removing that when we connect our pin so that we can control it ourselves. All the jumper is doing is jumping from the speed controller pin to 5V pin which is max power and then it's just always going to be max power. But we want to control the speed so we're going to be removing that and plugging a pin or sorry a DuPont cable over that pin and then connecting that to the Arduino board so we can control the speed. So first off let's start with getting rid of the relay. The relay has the power and ground for our pump so we're just going to loosen those screws. Now we can remove the relay and the cables that came with it. Now for the power supply we have a positive and a negative. The positive of the power supply and the negative of the power supply are going to go into the L298 where I said earlier down here. So the power supply we're going to put like this straight into the terminal block then we're going to close that up. Then we're going to need the ground of the power supply and that's going to go into the terminal block in the middle. Then one thing I'm going to do that I haven't done in previous videos and I had a commenter actually call me out on it is you should always be connecting your grounds all over the place so that they're consistent. So I'm going to be connecting a ground from the Arduino to the ground of the power supply using that terminal block. So I'm just going to run this little cable in the hole right here. I have to open it up a little bit but this is important to do to make sure your signals are good and that they're being grounded. Then I'm just going to run that ground into a ground on the Arduino board and the power supply will go eventually into the power supply but for now while I'm playing with it I'm going to keep it off of power. Then we have the positive and negative from our pump. That we're going to plug into output one and output two. We're going to put power into output one. So we have our pump connected positive and negative. We have our power supply connected positive and negative and then we have a negative going from the negative of the power supply to the negative the ground on the Arduino. Now we're going to take off our little jumper cable here or a little jumper connector from the ENA. Don't need that. Then we're going to take a cable. We're going to plug in one cable into ENA, one cable into IN1 and one cable into IN2. So we're going to plug in our ENA to pin number seven our IN1 into pin number two and our IN3 into pin number three. If you were to do another pump or another motor or whatever it is you would just repeat the process for the other side right here. So you can use the same power supply for both motors but you need to connect the other motor to this other pin to be able to control them differently. You can always put multiple motors in one but then you can't control them individually. They're all controlled through one power source. So by splitting them up into two different sides you can control this motor and this motor completely differently. So that's all we need for cables. We don't need tools anymore. Let's plug this into the Arduino and get started with the code. So for this we're going to be starting with a completely empty file and we're just going to write everything from scratch because it's pretty simple. As always the first thing you need to do is declare your pins. I'm going to write out the code as if I have two pumps and then we're going to modify it for our one pump just so that you know how to do it if you want to connect multiple things together. As you saw a minute ago we connected our ENA to pin number seven our IN1 to pin number two and our IN2 to pin number three and these are all variables declaring what's connected where. After declaring your variables you need to go in the setup and declare the pin modes. This lets the Arduino know what each pin is doing. And in our case all the pins are outputs because we're using these to control the L298 motor driver. These four will be controlling the direction these two for one pump and these two for if you were to use another one and then this is speed for the one pump and then this would be speed and then this is speed for our pump and this would be speed for another one if you were to use another one. Then in the loop it's pretty simple. The first thing we need to do is analog write our speeds to our ENA pin. Speed typically goes between zero and 255. In this case I'm putting 100 as the speed for both pins just so we kind of have like a nice medium. Think about 125 it's like halfway. So think of this as like 40% speed or whatever. Then after that we have two different options. For each one of those IN pins they have a high or a low and then the combination of both creates a output. So if you put low and low for motor number one that's going to mean pump off or motor off. I'm going to put up a screenshot on the screen and it's going to show you the combinations. So if you put high and low it's going to be on but in one direction and if you put low and then high it's going to be on but in another direction. And then you can combine those with the two different pumps. So we can have high low and low low which would be one pump on other pump off or one motor on one motor not. So in our case we're not using two. So we're going to delete this. I'm going to delete that. We only need those and we can delete everything for motor two here which includes that and includes this. Before we run this code on our actual project I want to do one thing to make sure we don't destroy the pump. Since this pump is not meant to be reversed in the opposite direction it's only meant to go one way. I'm going to unplug the pump cable and I'm going to put a multimeter so we can see the direction of electricity and we can use that to figure out which way the pump is going and then which way to put our cables in. Or what we need to do in our code to change it to make it go the right direction. So I'm just going to remove the negative and the positive from the water pump and I'm going to put in these two cables. Now that those are connected I'm going to turn on my multimeter and I'm going to plug in what I had as negative into the black cable and what I had as positive here. And we're going to see if the direction of flow or the direction of current is in the right direction. So one more time we're declaring our pins there are going to be outputs here's the speed we're going to be off here for three seconds and then on here for three seconds and we're going to figure out what direction that is. So let's upload that to the board and you can see even though the code's running our power supply isn't plugged in so we're not getting anything on here. I'm going to plug in the power supply and then we're going to plug that Arduino back in. Now the code's going to take a minute to run and then we should start seeing a volt on there. It's a little bit hard for it to read but we're getting 0.043 volts to this. Now if I was to reverse these which I'll do right now, I'll unplug this put that there and then this one right here we would be getting now a negative number which means the positive from the voltmeter is connected to the negative of the motor and then the same thing on the other side so we actually had it the right way around. So if you put high on motor one, pin one and low on motor one, pin two you will get forward direction and then if you were to swap those so it was low and high you would get reverse direction. So I'm going to unplug those I'm going to unplug my power supply I'm going to put it back in the water pump and then we'll run it with the water pump and see if it works. Pump is plugged in, it's not pushing any water out let's see if we put our speed higher if it will do anything. So we'll put motor speed at 200. So now the motor is starting to spin and I want to see what number I can get it to where the motor isn't too weak to push water but I can lower the water flow a lot more than this. So I'm going to lower it down to 150 and we're going to see if the motor can push water now. Now we're going to unplug the pump dump this water back into the main container I want to run it again but I want to see how low we can get the motor speed to be before the pump is too weak to push water because in our case we're not just turning a wheel we're actually pushing water and at a pretty high velocity. So I put it at 120 and I'm running it just going to plug in the pump let's see what happens. So it looks like 120 is a bit too low let's bring it up to 140. Let's see if 140 can get water flowing. Nice. So 140 is pushing some water I'm going to unplug that. Now let's see 255. This is max power this is what the relay has been putting through the whole time look how much more aggressive that is. One more of those and the bucket on the right will be empty and that's it. So that's how we're going to use a motor driver to control the flow and the water output from this pump in our water system. If you enjoyed learning about this water series I'm going to put a link in the description to the playlist of all the videos we've done on this specific project and if you enjoyed the video hit the subscribe button because I think 98% of people who watch these videos are not subscribed and don't get notified for the next one. So I have to rely on YouTube to promote them the new video with a subscription it just shows up and that's it. If you have any questions or concerns about this project let me know in the comments and I'll be seeing you in the next one. Oh and if you want the code make sure you let me know so I can send it to you.