 In the past couple of videos we've been building up a water control system step by step. We started with the base 24 volt pump and added different sections to it such as a water solenoid valve, pressure sensors, motor drivers and more. And in this video we're going to be adding one more section which is this water flow sensor. A water flow sensor is like a speedometer for cars. It checks how fast water is moving through a pipe from one side to another. Inside the sensor there's a little wheel that spins when water flows past it. The faster the water moves, the faster the wheel spins. This sensor counts how many times the wheel spins to figure out a number of how fast the water is moving. It's the same concept of a windmill spinning to get the speed of how fast the wind is going. One thing quickly before we take this apart and I'll show you exactly how it works is that 97% of people who have been watching this series are not subscribed to the channel and don't get notified every time we put out a new project or a new sensor. So if you like this stuff and you want to support it, hit the subscribe button so you can get notified when the next one comes out. So when we open it up we can see there's an impeller in here which is what we were talking about that spins around and counts. Then we have in and out. On the back it tells you which way the water flows so it comes in here, comes out there. And on top of the impeller you might see this black piece here which is actually a magnet. The reason there's a magnet is because for us to count how many spins the impeller is making we have to use something called a Hall Sensor. A Hall Sensor detects the magnetic field every time the magnet comes around. That detection is converted into an electrical signal which is sent out through the cable to our board every single time it makes a full spin. By counting these signals the sensor can accurately measure how many times the propeller has spun around which directly relates to how the volume of water flowing through the pipe. And by being able to track the rotations of the propeller going through with the Hall Sensor we can provide a precise and accurate number for the measure of water flow through the tube. One thing I would like to check as well is if we can actually calculate how much water we think has gone through this and then we're going to check in the second bucket and measure it and see if it's close at all. Some real life cases where you would see something like this being used would be irrigation systems for farmers, they want to see how much water is going out to the crops, aquariums use these sometimes, they want to see how much water is going through the filters and making sure everything is good. And then these are used a lot in water cooling systems for computers so they can see the flow rate and put that on the screen. Most of the time this isn't a life saving device it's more just to track numbers and not have to measure everything by hand. It's a lot easier to just have a number on a computer screen than to go out with buckets and start measuring everything. This paired together with the pressure sensor is really powerful because the pressure sensor when everything's closed can tell you if things are building up too dangerously and then when the things are open and the pressure sensor is not reading anything or reading very low this can tell you how much water or how much liquid is moving around. Together they're a really good combo for having information on what's happening. So for setting up the tubing to use a flow sensor we're going to need a couple different things. In the previous videos we've been using a 24 volt pump, two buckets and a bunch of tubing in between we're going to be reusing that for this video. If you want to learn how I did that, how I made a 24 volt pump work with the Arduino with electricity and everything, I'm going to post the videos in the description of all the projects we've done before this one so you can follow along with the whole series. To put this flow sensor into the current system we have we're going to need a couple different things. We're going to need the flow sensor, some PEX tubing, some connectors to connect the PEX tubing and the flow sensor together, some crimps for the PEX tubing to make sure everything is connected and that there's no water leaks, some cables to connect the sensor to the board and we're going to need some Teflon tape to use on those connectors to make sure that there's no leaks. Once you have all that you can start connecting it all together. I'm going to keep it simple so I'm just going to start with this side right here from the water flow sensor to the 24 volt pump. Just get that Teflon out of here. Before I connect the sensor, I'm just going to wrap it up with a nice layer of Teflon tape. Make sure there's no water leaks happening and we just take this and it threads on and then we're going to put the sensor I think like this. Let's see, water flows. No, so we need to put it like that because this is the inlet, I'm going to put it like this, get it lined up. So now we have our 24 volt pump going into tubing. Then our tubing goes down into a flow sensor that I'm just going to cover up like this in case it leaks. Then our flow sensor just goes into this tubing and into the next bucket right over here. The goal is to see how much water is moving from here to here and what speed it's moving at. So now that we're done with tubing, we need to get our cables connected. The nice thing about the flow sensor is it's pretty simple. It only has three cables, a red, a black and a yellow. The red and black are power and ground and then the yellow is a signal wire. Mine happens to come with this connector. I'm not going to rip it apart and start stripping wires. I'm just going to use the pot cables and start putting them in and then I'm going to tape over that to make sure they don't fall out. So first wire we're going to connect to red. So the first cable we're going to put into red and then we're going to run that to the five volt port on the Arduino. We're going to run the next one on yellow, which is in the middle. And yellow, it's going to go to digital pin number five. The input from the propeller is just going to be like, hey, something passed, just an electrical signal. So it's on or off. So we don't need an analog pin. We're just going to use digital pins for this. Then the last one, which is the black cable, is going to go into the ground port. The relay is currently plugged into the three volt port. It worked last time. So I'm just going to keep doing it. But usually you should have a breadboard and you should put both relay and of this on five volt. These are rated, I think five volt to 12 volt, but I'm not going to run a whole power supply for nothing. So I'm just going to put this on five volt. All right. So flow sensor is connected up to power and ground and digital five. Now that everything's plugged in, let's plug the Arduino into the computer and start writing code to make this flow sensor work. Unfortunately for the code section of this sensor specifically, things are going to get a little bit complicated. If you're having any trouble following the code, you don't understand why we're writing something. Check the description. I'm going to put a discord link where you can join and you can ask all kinds of code questions and we have people that will help you out. I'll definitely have to make some kind of video on the programming side of Arduino in the future. If you just want to copy paste it, there's going to be another link down there with the whole code. So you can just copy paste and then continue on with the project. As always, the first thing we're going to do is declare the pins we're using. We're putting relay on pin number four. And we said that we're going to put the flow sensor on pin number five. Now when we capture information from the flow sensor, we're going to have to keep some things. We're going to have to take some numbers and calculate them. So for that, we're going to have to be making a bunch of different variables that I'm going to put on the screen right here. For calibration factor, it's a number we're going to be adjusting for our specific sensor. We're going to have to do some measurements and calibrate it for this system to make sure it's as accurate as possible. Then we have five variables here that are all going to hold different information for our calculations later on. So just write these out and you're going to see what they're going to do when we write the functions out. So then in our setup, we're going to do what we're going to do. We're going to set up the input pull-up which is something we haven't used before. When you set up pin mode to input pull-up, it tells the Arduino to use the internal pull-up resistor. So we want it to be high until the hall sensor pulls the electricity and that's what we know, the impeller spun around. The reason we set it to pull up or to pull down is so that the sensor is consistent with what we're going to do. So we're going to set input pull-up which is something we haven't used before. When you set up pin mode to input pull-up, the sensor is consistently either high or low and it's not jumping in between the two because in the previous video, we had that problem where electricity was jumping up and down and suddenly we were getting 20 psi when there was no water flowing through the tube. So using a pull-up resistor helps us with that. The next step is to declare our serial dot begin which allows us to write things into the console like we did in the previous video. Now is the time where we're going to be calling some of those variables we made earlier. We're going to have to do something that we haven't done before which is use this here interrupt pin or interrupt function method whatever you want to call it. These are used to start and stop the interrupt that listens for the flow sensors pulses. So while the program is running, it's going to be doing all sorts of things but the second a pulse comes through, we want everything to shut down and just to listen to what's happening on this pin. The reason for that is if we were to say every 5 seconds check to see if we waited 5 seconds. So we can't have any gaps of time in between those spins. So we're going to leave it always listening and then every time a spin happens everything's going to stop. We're going to count that spin and then go back to whatever we're doing. You can't really tell the impeller wait and let me do a couple things and then I'm going to come back and listen to you because by the time we do that it's on its fourth, fifth, sixth spin we won't know. After that we're going to start our loop. The loop it's going to start off with relay pin high and a 5 second delay. What this is going to do it's going to turn on the pump for 5 seconds. Alright, we're done with the setup. Let's go into the loop. The first thing we're going to do in the loop is turn our pump on. Here we have a delay which means our pump will run for 5 seconds until anything else happens. After the pump runs for 5 seconds we're going to turn it off. After the pump runs for 5 seconds and then turns off we're going to have to do some calculations to those variables that we made earlier. So you can see here we're counting pulses, we're counting flow rate, we're counting milliliters and then we're counting total milliliters and then we have a time counter. All these things are going to help us calculate the number at the end which is what we're going for. Then we're going to do a little formula here and we're going to calculate our flow rate. Then we're going to do another formula where we're going to calculate how much total liquid has moved from one bucket to another. As you can see right here we're using our calibration factor and that's taking into account everything else here. The reason we're using calibration factor here you can see the calibration factor and the reason we use it is that let's say we know a thousand pulses from this sensor right here so a thousand spins equals one liter of water and then we go and we measure the bucket and in the bucket it's a thousand two hundred liters or milliliters I should say. Here you can see we're using that calibration factor we talked about earlier. The reason we're using the calibration factor in this formula right here to calculate flow rate is that we need the amount of impeller spins to be accurate to how much liquid is coming out and it's not going to be accurate for every system so we have to do our own calibration. Here's an example. Let's say we know that one thousand spins of this impeller one thousand pulses into the Arduino equals one liter of water and we go and we measure and turns out it's actually supposed to be one thousand two hundred pulse for one liter so then we're going to divide one thousand by one thousand two hundred and we're going to get zero point eight three and that's going to be our new calibration factor and now it should be accurate to our system and our flow so if that was the case we would go up here we would do zero point eight three three whatever it is and now it will account for our specific system and our specific situation and tubing and all that so after we do that after we calculate the total liquid we're going to move on to printing everything out in the console we're going to be printing out the flow rate in milliliters per second and we're going to be printing out total liquid that came out from one bucket to another in milliliters so one thousand milliliters should be one liter in this bucket and then we're going to put a 10 second delay just to make sure nothing's leaking there's no problems we have 10 seconds to kind of look around one thing I forgot to mention is actually the delay is supposed to go under the if statement and right here after we print out everything we need to add our pulse counter to make sure that we're counting how many pulses we have overall and we're going to be attaching our pin back again which we detached right over here and then in the bottom after the delay and after your loop you're going to be adding this right here this is our little function here which is going to be adding to the pulse counter so now we want to get started with the demo since we finished all the code I'm just going to comment out everything from here all the way down to the delay so all our calculations and all that we're going to comment that out and I just want to run the pump for 5 seconds on and then 10 seconds off on for 5 seconds and then off for 10 seconds we can adjust this to maybe just one second on one second off and just check for any water leaks make sure we don't start spilling water everywhere like we did before so on one second off one second on one second off pump in the plug and we have water flow alright so we know water is going through I don't see a single water leak this time nothing is wet so I think we're good to go I'm going to comment this out I'm going to upload it to the board so that it stops triggering the pump completely empty let's dump that in there now let's test our flow sensor so what I'd like to do is comment out or uncomment absolutely everything and run it with the pump not connected so just info from the sensor should be coming through and it should be zero and then we can kind of calculate it to make sure that it's done right so we go here to tools go to serial monitor we're good our setup we see we're doing 9600 over here so we're going to reset this flow rate zero total liquid zero that's good it's not telling us 20 psi like the last project so things are at zero we're happy now what I'd like to do is plug in the pump for a second let it run and unplug it and we can see we're still at zero so something is not right just going to make sure that everything is plugged in properly and that our flow is going in the right direction it's plugged in to pin number five it's plugged into the positive over here negative over there so our cables are good let's go up here flow sensor is pin number five and upload the code again open up the serial monitor clear it see if the number is still zero okay so we're still reading zero I'm going to turn on the pump for five seconds and then ten seconds like we had it set up before we're going to see if keeping it open longer makes a difference okay we're still reading zero so I'm going to unplug the pump pour the water back in so we know the water is moving from one place to another that's all good but we have a problem with our system where we're not reading any flow at all what I'm thinking is there's probably a bad connection or we missed something really small on the code so I'm just going to change my pin number five to pin number three to see if maybe there's an issue with pin number five and then I'm going to go over here and I'm just going to change that so you can see now it's pin number three pin number three everything else we're keeping the same relay pin is still number four and if we reset this we should see that it's reading zero yep it's reading zero alright let's plug the pump in let's let it run once for five seconds let's see if anything changed okay so now on pin number three it's working so I'm going to unplug the Arduino quick before it runs again so we're getting 23.45 milliliters per second and the total liquid is 23 milliliters so one more time I'm going to dump this over here we're running it for five seconds to get our water flow so what I would like to do is to run it for five seconds measure how much water is in the bucket and see how off we are over here and then we can kind of do some calculations and get it calibrated so I'm going to plug in this one more time I'm going to reset it one two three four five I'm going to unplug it it's saying we've done 69.64 milliliters per second the total liquid is 69 milliliters now something's off here if we run it for five seconds at let's say 70 milliliters per second our total output should not be 70 milliliters because we ran it for five seconds and if I go into my little measuring cup right here we're just just between 200 and 250 so we're let's say 230 so if we did 70 milliliters per second four or five seconds it should be our number but what we really got is let's say 230 so we're about 120 milliliters off right now well it's been about four hours since the last clip I filmed and we ran into a lot of issues the first thing mainly that I'm not very good at math so I made a lot of calculation problems also the calibration factor the way it was being used was not very good so what I did was rewrite everything from scratch it still has the same idea the calibration factor does the same thing everything kind of does the same job that we went over it's just the order and everything is completely different now I want to do another demo to prove that it works so I'm going to dump out this and I'm going to run the code the current calibration factor is 0.42 the reason I was having a hard time with the calibration factor is that my pump my tubing size everything kind of in this scenario is not the same as everyone else and that's the reason that we have a calibration fact but because of the way my stuff is set up where I'm going big tube to small tube I'm using a submersible pump with different levels of water all these different things was kind of messing with the consistency of the number so now with a 0.42 calibration factor we're going to upload that to the board and then you should see the board turn on see that light we're going to wait for it to turn off and then I'm going to plug in my pump now that it's off I'm going to plug in the pump I'm going to go serial monitor I'm going to reset and now we should see our number popping up last time we measured in the cup it was 221 ml of water we're going to let this run we're going to unplug the pump and as you can see we're right around that to show how accurate this could be if you could get it properly calibrated I have a scale which I'm going to just put like this so we got 221 ml with 47.62 flow rate I'm going to put the cup on the scale I'm going to zero it and now I'm going to pour out our outputted water so I know it's a little hard to see on the scale but it says 218 grams and over here we have 221 on the screen in the code here so we're only 4 or 5 in terms of measurement we can always fine tune the calibration I only have it 2 decimals we can always add something like this and it would be even more accurate for the calibration factor if you want to calculate yourself when you do your project I want to put the formula up on the screen and then you can do that yourself to figure out what the perfect and ideal situation number is for you it's unfortunate that these videos sometimes don't go the way they're supposed to so that was 4 hours just to figure out how to get it that close if you enjoyed the video do me a favor and give the video a like and subscribe to the channel to see more stuff like this in the next video we're going to be adding one more thing into this system before we build a big project involving everything if you're looking forward to that make sure you subscribe to the channel so you can get notified when I post it if you have any questions join the discord we have people out there that can help you out with your problems and I'll see you in the next one