 In the last couple of videos, we went over different water-related components. We went over how they worked, how to set them up, we built the whole system with them, how to make a massive and kind of dangerous mess, and how to write code to control everything. And in this video, we're going to be adding one more layer to the system, this here, pressure sensor. If you want to follow along, go and check out the other videos. I will link them in the description. And if you enjoy this content and series, subscribe to the channel so you can see more of these and get notified when I put new ones. 98% of viewers aren't subscribed, so let's change that number. A pressure sensor is a tool that measures the force of water or air pushing against it and turns that force into an electric signal. Imagine that this is a bathroom scale. When you step on the scale, it reads your weight because you're pushing the scale down towards the ground and squeezing it down. Then it can use that to make an electronic number, put something on the screen and tell you how much you weigh. In a very similar way, that's how this pressure sensor works. Air or water go through this little hole down here and push against the scale. And then we can get a reading from that on how much pressure there is when pressure is applied inside the pressure sensor. There's a special material that changes the electrical resistance when pressure is applied. This is called a piezo resistive material. The deformation of this material. So when it's getting squeezed and pushed around causes a change in the materials, electrical resistance. So it's like squeezing water through a water hose. The more you squeeze, the harder it is for water to pass through. And then depending on how much water is on the other side, we can read that and put it into a number so that change in resistance alters the electrical current. So we're pushing electricity. Imagine like we're pushing water through a hose, we're pushing electricity through a hose or through a cable. And we're going to be squeezing and releasing depending on the pressure that this reads. And then whatever electricity we receive back, so if we're putting in five volts and getting back two volts, we can do a calculation and figure out the pressure. And that's how this here pressure sensor works. If you're wondering in what kind of situations you would ever need a pressure sensor like this, here's some real life examples. This actual sensor and adapter that I bought with it is made for cars. It's made to monitor either fuel or oil pressure. You do that so you can see a problem going on before it blows up the engine. Another good example is in heavy machinery with hydraulics or even home plumbing. These sensors are great because you could see when there's a leak, because if you're expecting 100 psi and you're only getting 80 and everything is good, except for that number, you know somewhere that there's hydraulic fluid or water leaking. And you can use that before the leak gets worse or before there's a pipe that burst or something catastrophic. So this can actually save you in those kind of situation. Now one thing before we get started off the demo, I'm going to be building off a recent project where we did a 24 volt pump. So we use the pump, the relay, the Arduino, the tubing. So if you want to see any of that, I'm going to put the video down in the description so you can watch it and then come back here. So we're going to need the pressure sensor obviously. We're going to need some extra tubing. We're going to need a connector specific for the pressure sensor and a block off connector to close or open the water so we could build pressure. Now that we have all our parts, let's first modify this tubing here to fit our sensor and then plug in all the wires so we can get started with the code. Okay, so now we got rid of that little middle piece and now we broke it into two pieces. We're going to be putting our pressure sensor in the middle of the two right like this. As always, we're just going to press it down just a little bit so it's tight. So now we have tubing to pressure sensor, pressure sensor to tubing. Now we're going to add our little block off here. All right, so open close. This is open. That is close. All right, just to make things easy in case we have a little bit of a leak. I want to tape down this tubing like that. I'll loop this up here. Okay, so we go from buckets to being down to pressure sensor, pressure sensor over here into our little block off, which I can open and close. And then that goes up here into the next bucket. Now that we did that, let's connect up our wiring so we can get started with the code. Don't forget when you're putting in your pressure sensor, if there's ever metal on metal threads, you should definitely Teflon this up. The wiring is going to be pretty simple. We have our pressure sensor that has three different cables, two for power supply, power and ground, and then one for signal. In our case, we're going to be using a power supply for the pressure sensor. We're not going to be going straight off the board like we did with the relay. So I'm just going to bring that over here. And on this, we have our power and ground. So I'm going to plug those together. And now this brown one right here is going to go to the Arduino. And this is going to plug into the a zero or a one. And for the pressure sensor, when you plug it into the Arduino, you're not going to be using a digital pin. You're going to be using an analog pin. The reason for this is because digital is either on or off. As you've seen in recent projects, we've done low or high when working with the digital pin. It's like a light switch on or off. That's all you have. But with analog, you have a range. And since the pressure sensor is not going to be on or off, we're going to be using a range like how much PSI is there going from zero to 100. So it's a range. We're going to need an analog pin. So we're going to plug that into a zero. You can use a zero all the way to a five on whatever board you have. And then this is going to plug into the pressure sensor right there. So now in terms of wiring, we have our pump, which has a relay. And the relay connects to the power supply and the power supply goes to an outlet. We have our pressure sensor, which connects to the Arduino and to a power supply that goes to the wall. Once I plug in the pressure sensor, it will be powered at all times. It's not like the pump where it's on or off. It just stays on the whole time. Now let's prepare some dowels on the side. Let's plug in the Arduino into the computer and let's write some code to test this out and see if it leaks. Alright, sorry. We got to back it up a sec. I actually tried to film using the code I just showed you on the screen and the wiring that I had and everything went wrong. I've been sitting here for the past three hours trying to figure out why nothing's working. Here's the common problems I had. First off, I was using a power supply that was slightly overpowered. So my sensor constantly had a little bit too much electricity. So when there was no water, no flow, no pressure, I was putting out 20 psi or more, which makes no sense. Mistake number two was accidentally putting the pin in the wrong hole and for an hour and a half trying to figure out why my number was not changing no matter how many times I ran the pump. And then part three was that not everyone's sensors are going to be the same. We have to kind of calibrate them. So I added a piece of code that I'm going to walk you through right now to show you how to calibrate your sensor. I understand it's a big mess and it's probably very overwhelming, but let me just show you from the start what we're doing. First off, we're declaring our pins. Pump pin is the relay for the pump and pressure pin is the pin for the sensor. Then we're going to declare pump pin as an output, which we've done before in previous videos, and we're going to open up a console so that we can see data down here. Then we're going to turn the pump on for two seconds. That's it. After the two seconds, we're going to do a bunch of calculating and reading. So we're going to capture the analog reading from the pressure sensor and we're going to turn that into a voltage. Then we're going to take that voltage and we're going to put it out in the console as baseline voltage so that we could see what our pressure is at and what our voltage is at when there's nothing in the system. So ideally it would be zero psi, but that's not how electricity works. Sometimes there's too much or too little, so we've got to calibrate towards that. This is what fixed my problem. Then after that we're going to take our baseline voltage and our voltage and we're going to calculate using 100 as our max psi because that's what my sensor is rated to. We're going to calculate pressure and pressure as in psi. Then we're going to write that out to the console right here. We're going to do all that in a very short period of time, about 200 milliseconds. I'm just leaving a little delay so I can finish calculating and then after that we're going to turn off the pump and restart the whole cycle. So what I'm going to do first is I'm going to turn this delay into one second. All that seems to be good. We're going to save it and we're going to upload it to the board. Now my pump is not connected so what I'm hoping will happen when I connect the board and I run the code from scratch that we're going to get zero as our psi. So let that run. Go to tools, serial monitor and it'll be down here. Now look how every time it runs. I'm going to take out the cable so we can analyze this quick. Every time it runs it's going to give us baseline voltage. So when there's zero pressure this is how much volts are coming through anyways. Ideally it'd be zero volts but we are getting some. So we know that if we're around 0.4645 we should calibrate it to that and since I'm getting a lot of four pives I put it right here as 0.5 volts. Now we can remove that from our main voltage that we're getting when there is pressure to get a accurate reading. So that's kind of our calibration. Nice thing about this is that once we kind of get an idea of where it's hanging out we could just set it here and never touch it again. And since I have it always printing out to the console if it does change we know what to set it at for a new calibration. After that you could see that we're getting 0.20.1 psi that's okay we're not expecting a perfect zero but at least it's not 20 psi with zero pressure. Now what I want to do is I want to open the water line. I want to run everything again with the pump on but the water line open and I want to see if the pressure rises even a little bit. So I'm going to plug in the pump. I'm going to plug in the Arduino just to be sure I'm going to upload fresh code. I'm going to reset the console. I'm going to upload the fresh code. All right open up the serial monitor again. Let's reset this. Okay so now we're getting 0.6 volts and that's equating to 4 psi. 4 psi. Again 4 psi. 4.3 psi before it was 4.2 now back to 4.2. Okay perfect now let's unplug this one more time. So now we're getting more voltage because we're getting more pressure and through that we can calculate what pressure we're currently at. So we were at 0.1 base maybe 0.2 and now we're at 4 so we can round down or whatever now we're at 4 psi. So the pump on tubing all open just with the reduce here so it's going from big pipe to small pipe just with that we're getting 4 psi. Now let's close this and see what number we're getting and don't forget that we do have a baby tiny leak over here that I will fix in the next video. So let's clear the console save this plug in this now it's going to start running we're going to see what we're at 4.3 psi 4.2 psi 4.4 psi let's plug it in let's reset this let's see our readings 0.642 might get 4344 that's our average reading now we're going to close this we're going to see what it reads at look at that went from 4.2 to 5.7 5.4 5.6 5.5 5.3 and we do have a leak I wish I can get an accurate reading without the leak I'm going to try and squeeze it there so I get a better number we went from 4243 right away off the bat to 58 so we almost jumped to psi or psi and a half just by closing this and that whole time we had a water leak if we didn't have this water leak I bet that would be a bigger number so just to conclude this video we brought water from one bucket to another through tubing a pressure sensor connector and a valve to open and close when we left it open with the pump off we were reading 0.2 psi when we ran it with the valve open we were reading around 4 psi and then once we opened it up at one point we got really close to 6 psi and all that while having a water as the water and the pressure built up in the tube it pushed against the sensor kind of like I said earlier when you step on a scale you're squeezing it down towards the ground and that's how that works so we were squeezing water against the sensor and then receiving a reading of how much pressure we were putting on that sensor which in our case was six now we know when we use a pressure sensor and we move water from one place to another we can calculate pressure by saying how hard the water pushes against the sensor just like when you step on a scale you push against the scale to get a number when we ran everything open water pump off we got 0 psi then we ran the pump on but we left the valve open which gave us around 4 psi and then we ran everything on valve closed and we got almost 6 psi and all that while having a leak and all we did was push against that sensor hard enough to let more electricity through and using that electricity we can calculate psi with the code that I wrote if it was hard to follow around and write all the code I'm going to post it in a link in the description so you can copy paste it and follow along instead for those of you who want the code I'm going to put a link in the description so that you can go and access it if the video was good subscribe to the channel and give the video a like because 98% of you are still not subscribed to the channel in the next video we're going to be using a flow sensor instead of a pressure sensor to see how fast the water is moving from this bucket to this one if you're looking forward to see that let me know in the comments and I'll see you in the next video