Controlling Water Flow with a Solenoid and Arduino IoT Cloud Part 1
com. All right lets get started all right, so heres an overview of what we plan to cover in part one and two. So first off were going to start with a linear, solenoid thats, going to be integrated with a valve to control. You know water or gas or oil flow, so well talk about the hardware. What it takes to drive a solenoid circuit well give an overview of what a solenoid is were going to be using an esp32 because of its wi fi capabilities to control our solenoid and also to connect to the arduino iot cloud and on the iot cloud. Well, look at how to set up a simple switch to control our solenoid from the cloud, so you can do it from from the comfort of your couch or from you know, another country. All you need is an internet connection to control it and well also look at how to set up a scheduler on the iot cloud. So the solenoid turns on at a certain interval every day or every week, for you know 10 seconds a minute, 10 minutes. So thats what were going to cover in part one and two in part, one were going to focus more on the hardware setup. What is a solenoid? How do we drive it and well set up a simple sketch to control it manually from the esp32? Let me start off by saying im, not a solenoid expert. In fact, this is the first time ive controlled a solenoid, so its sort of a beginner first timer, giving probably other beginners an overview of how things work so im, not a solenoid expert.
If anything, i miss, if, if someone knows a lot about solomons, please comment in the section below, but a solenoid is a lot like a mechanical relay and ive done a lot of circuits for mechanical relays, a lot of products that are actually on the market, because The solenoid is going to work based on current through a coil that creates a magnetic field and that magnetic field is going to move a plunger similar to a you know, a relay a relay is typically open and then a applying a current creates a magnetic field That pushes the relay closed. Whenever you have current flow through any wire, it creates a magnetic field. The way to make the magnetic field stronger is by coiling the wire in tight coils, so that you create more of a magnetic field to actually move some. So a solenoid can be used for different things. It can be used for door locks. Things like that were using a solenoid or im using a solenoid thats integrated with a valve to open or close the valve, so water, gas or something can flow, so solenoid typically has a plunger and that plunger is held into place in its default state with a Spring typically, so when you apply current through the coil inside the solenoid, it creates a magnetic field that moves the plunger out from its default spot. And you know its default spot may be open or it may be closed for our tutorial im going to use a linear, solenoid thats normally closed, meaning water cant flow through it.
And then, when you apply current and create the magnetic field, it opens and as you can imagine, the stronger the spring, the more pressure or psi the solenoid can handle, but of course, the stronger the straight spring. The more current you need to create a stronger magnetic field to move that plunger out of its default position. So when the solenoid is in its default position, you know your your circuits basically consuming no current and then, when its actuated, then youre getting typically a high current consumption. Okay lets look at our solenoid control circuit, so were going to use a digital pin on our esp32 low and high to turn the solenoid closed or open. So if we look at our diagram here, our schematic, the solenoid coil, is represented by this green box and im going to be using a 12 volt power supply. So im and ill provide all the part numbers for stuff im using on a later slide. But the idea is, solenoids can come in ac power. They can come in 24 volts dc. They can also come in 12 volts dc im using a 12 volt dc solenoid on one end of the solenoid power wires. I have 12 volts and on the other end i have it connected to the drain of my n channel mosfet im going to use my n channel mosfet like a switch, so im im assuming folks watching have a you know, a basic understanding of how basic circuit Components, work ill go over briefly how the mosfet works.
If you want more information, theres plenty of tutorials out there, if i have a zero volt difference between the gate and the source, the gate being here where my digital pin is connected and the source being connected to ground. If i have zero volt difference potential between the gate and the source, then my mosfet is off or it acts like an open. So no current can flow through the coil because theres no path to ground, if i pull it high and for the mosfet im using you know, i need to get it about four volts or higher to to close it or to turn it on. And so, if i take the gate up to four volts or higher and im going to use five volts in this example, with the source connected to ground ill, get a 5 volt difference, my n channel mosfet will turn on or close and all of a sudden Current can flow through the solenoid coil, which actuates the solenoid and opens the valve in our case, r1 is just serving as a current limiting resistor for protection of the uh. The digital pin from the esp32 r2 is just meant to be a pull down so that, if, for some reason, this gate is floating when theres no power or whatever when were programming, our esp32 r2 just makes sure the gate stays at the same potential as the Source so it doesnt accidentally turn on and then finally, this flyback diode is important because when we have current throw flow through the coil creates a magnetic field that magnetic field has stored energy.
So when we open our switch after weve had current flowing, what happens? Is the magnetic field begins to collapse? The solenoid goes back to its default connect position, but that magnetic field when it collapses it induces current back onto the the circuit and the flyback diode will forward bias and allow that that extra energy to just dissipate now. One thing you might say is: if youre using an esp 32 esp 32s work with 3.3 volts, so you can only get a 3.3 volt high out. So if i need above 4 volts or above to drive my gate, i need something else. Besides, just the esp32 pit, so im, also using in my prototyping a level shifting circuit for this example, you know im calling this prototyping. I just pulled a mosfet. I had out of my lab stock. Now you may be able to find a mosfet for this application. In fact, i know you can that 3.3 volts will drive it for me. Im doing prototyping in the future ill put this all together on a custom pcb, and i may not need the level shifting circuit, but for the mosfet that i had in my lab stock. I needed i needed 5 volts to drive it on my esp32 board that i designed for a different application that im just using for prototyping here. I already have a level shifting circuit integrated on it. So once again, you may or may not need this level shifting circuit and the idea here with this level, shifting circuit is were using a mosfet.
Again we have the gate of the mosfet tied to three 3.3 volts. We have the source tied to the digital pin of the esp32, and then we have a five volt power supply that can pull up this line with a 10k resistor. But the idea here is when my esp32 pin is an active low. The source is low. The gate is at 3.3 volts and for this mosfet thats plenty of voltage difference to turn it on. This is a lower power mosfet. We basically get a low here because we have it. This acts like a short and so this five void. This potential gets pulled to zero, and i should say this is connected to here and that pulls this low, so our switch is open or off. If i apply a 3.3 volt logic here, all of a sudden, our gate and source are at the same voltage potential. So theres zero voltage difference between the gate and the source. The mosfet turns off and acts like an open. So this 5 volts pulls this potential up to 5 volts, which essentially turns our mosfet on or closes the switch the gate the source. Really, when you turn it on very little current flows, you know nano amps or micro amps of current, so its okay, that you know i have these resistors in between here. Itll still turn on fine, okay, thats, our hardware setup and, like i said before, ill provide a bomb at the end.
So you can see what i what i used. Okay now were gon na take a look at our arduino code and, of course, were gon na. Have different code in part two for using arduino iot cloud, but for this demonstration of the hardware i just set up a simple sketch, where i have a switch that one of the pins is a input pull up so its automatically high. If its left floating, then i have a switch between this pin and ground which will pull this pin low. So you can see if this pin is high, our drive pin is low, so the mosfet switch is off or open and then, if my switch pulls our control pin low, then our drive pin goes high and turns our switch on or closes our switch. Okay, now lets look at our uh circuit and our code in action. So what youre looking at is my kitchen sink, and this hose this black hose piece is connected to my solenoid. Now what i had to do was i found out when you turn on the solenoid. Water can actually shoot out of here pretty quickly and i didnt want water to splash on my electronics. So i have this nozzle here to restrict the flow of water, but well see. Obviously, when its turned on and so im going to start the video and we can see thats our our solenoid and ill, give you part numbers for the adapters i use to connect the hose, but the other end of the solenoid goes to a hose.
That goes out the window thats connected to a water line outside my house here, im going to zoom in on the uh on my circuit, so this is once again a prototyping circuit, so just stuff i had in lab stock heres my diode for flyback. This is my n channel mosfet. This resistor is the current limiting resistor, and these clips are coming from my 12 volt power supply, black is ground or low, and red is high, so black is connected to the source of the mosfet and red is connected to the power wire that goes to the Solenoid play the video again and well see that heres my esp32 circuit. This was a circuit i designed for another project im just leveraging it for prototyping, but basically the pin that drives the mosfet on and off is coming out here on one of these wires and then also another. One of these wires is ground and my level shifting circuit is on this board and then these green wires they connect my mechanical switch that ill use to turn the mosfet on or off theres my switch right there and then im going to show my power supply Im showing you that the output is on and my fancy power supply lets us see voltage and current, so we have it at 12 volts, but no current flowing, because our mosfet switch is open. So what im going to do is im going to turn it on and im going to point the camera at the solenoid.
You can see the solenoid jerk when it turns on and then ill show you the current flow there the solenoid turned on and we can see now we have some current flowing, and so one thing to keep in mind is depending on what solenoid you get. I got a pretty a pretty beefy one that can handle high psi youre, going to get a lot of current flow, so im getting above 3 amps or almost 38 watts, depending on what solenoid you get. You may not need this much current or if you get a really beefy one, you may need more. Who knows so well, go back and well go to my hose and if i squeeze the nozzle we can see water is flowing through the solenoid. So i let go of the trigger on there im then going to turn the solenoid or our switch off. We look here. No current flowing im going to turn this on at first we see water come out, but that was just the water left in the line. So we can see its off now and then just for good measure. I turn it on one more time there we go and water is flowing so thats. The demonstration next in part two well show that from the arduino cloud, okay heres the bomb of the parts. I used, and once again this is for prototyping, so the mosfet i use is a little bit of an overkill when i design my pcb ill use a different one but heres the part numbers i use heres.
The part number of the solenoid, the adapters from the solenoid to the hose the solenoid i used, uses uh whats known as mpt threads. I forget what thats called, but i got a half inch one you can get them at different sizes and then in the u.s ght is the typical threading for hoses at three quarter inch. So i have a female and a male, and then my valve with my solenoid, is female on both sides, so i got a male and these these adapters are pretty easy to find. I got mine off amazon. If you just search this on amazon or whatever place you shop at youll, youll be able to find them. Okay, thats it for part one. If you have any, if you think theres anything, i missed feel free to comment or if you have any questions comment there uh in part, two well be focusing on the arduino cloud and the dashboard and the code for that uh im gon na assume. You know how to do the basic setup to tie your arduino device to the arduino cloud, if not ill, provide a link to the tutorial in the description for the video thats it for part. One.