Control a Solenoid with an Arduino (Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
stop hitting yourself Arduino stop hitting yourself Arduino hey it's Matt here today I'm going to show you how to use an Arduino to control a solid to control a solenoid so what is a solenoid in its most basic form a solenoid is an electromagnet with some sort of shaft sticking out the middle when you apply electricity to the electromagnet the magnetic force pulls the shaft in very very quickly this can be a very useful part when you need to apply mechanical force to something very quickly but you don't need it to move very far a stereotypical example and the reason that I got so interested in solenoids is pinball machines pinball machines use solenoids everywhere including on the pinball bats but solenoids have a lot of other uses as well in fact more typically they are used to control the flow of fluid such as the cold and hot water intake on my poor washing machine don't worry I can put it back together I think in front of me I've got two types of solenoids the first is one that you will typically find on Amazon eBay Aliexpress and the like they are quite cheap and they are not particularly strong and they seem a little flimsy but they are a very useful type of solenoid this is a type of solenoid called a push-pull solenoid meaning that it can pull by attaching something to the shaft here or it can push by simply rotating it the other way around and putting something in front of the shaft like that now in addition these solenoids are super convenient because they've included everything that you need to use it it's got a little spring so that actually returns and it's got a little retainer ring for that spring so all you have to do is get it hooked up and you can go ahead and use it now the second type of solenoid that I have here is a little bit different and a lot more calm this is a solenoid that I bought off digi-key and you can find them in all sorts of shapes and sizes this is a Pontiac coil one from the L 90 series I believe and I'll include that in the description and this is known as a pol solenoid you'll notice that there's no shaft on the other end to push so you have to convert the pulling force into whatever direction you need it to go the big advantage of this type of solenoid is just a sheer variety of formats and strengths and so on that you can get them this is the one that I bought for my flippers for my pinball machine because this one wasn't nearly as strong okay so let's learn how to use an Arduino to control a solenoid now a small disclaimer I am NOT an electrical engineer I'm simply a guy that is building a pinball machine and as a result I have spent 20 maybe 30 hours researching solenoids how to control them all the little gotchas and catches along the way so I hope I got everything right but if you do notice any problems leave them below in the comments and we can all learn from them now let's talk about how to actually wire up the solenoid to be able to control a solenoid with an Arduino I think we should first look at how to control a solenoid in general there it's pretty easy to do but there are a few specific things that you have to watch out for so we're going to build this circuit up over three iterations starting with removing the Arduino and talking about how to control the solenoid with just a switch okay so here's version 1 of the circuit now every time I see someone else's circuit my first reaction is to get overwhelmed so I promise you this is actually very simple in fact it's pretty much the same as lighting up a bulb just of a couple solenoid specific things and I'll walk you through it step-by-step so let's begin so we've got power coming into the breadboard that's all this does it's connected to my power supply it's bringing the negative into negative or ground and it's bringing positive into the positive rail now let's follow through what this circuit does we've got a wire going from ground over to a fuse why do we have a fuse we have a fuse because solenoids have a tendency to draw a lot more current than a lot of other things that you might be used to such as LEDs for example the test solenoid that we're using today draws two amps and that's becomes a fair bit of current and we're going to come back to that because that's important but I've got a 3m fuse here so just in case anything shorts out or something else happens we don't continue to draw more and more current from the power supply because I've got this hooked up to a power supply that can put out like four or five amps next we've got a switch and that's simply a push-button switch and this we're gonna come back to this because this is gonna be an interesting experiment but this is what's going to turn the solenoid on and off or actuated I guess and from that switch we've simply got a wire going over to the solenoid and these two wires then lead to the actual solenoid here so you'll notice that there's also a diode put across these two wires this is known as a flyback diode it's a normal diode this is an N one zero zero seven I believe but it's known as a flyback diode any time that you put it across an inductive loads such as a solenoid so the issue of solenoids is when a solenoid activates and then you cut the electricity you can actually get some voltage flowing backwards across your wires and that's the same thing with motors and other inductive loads as well so to counteract that and keep that electrical force from then damaging everything else we put a diode across the lines but we put it in reverse so when you have normal current going forwards across this diode does nothing but if current starts going backwards across the line this diode basically stops that from happening and it allows the solenoid to basically short-circuit itself and turn all of that extra voltage into heat and then from the for some from the solenoid we go back to our positive power rail okay so I've got the solenoid here and then I'm going to press the button and activate it and sure enough awesome the solenoid activates but now I'm gonna do something here and it's gonna hurt me a little bit I'm gonna keep this solenoid activated for a little bit oh okay how how that hurt there's a problem with this circuit and I did this on purpose to demonstrate the issue with solenoids drawing so much current and the way that we have a circuits set up here so this solenoid is drawing two amps this little switch this little button is not rated for two amps or anywhere close to that so if you go ahead and you want to use this circuit you need to use a bigger switch you need to use one that's actually rated for two amps otherwise this switch gets hot and it eventually melts and stops working very very quickly and trust me I've known trust me I've burnt out a couple of these things before so the simple fix of course is to get a bigger button a bigger switch but of course that can be a little awkward if you want a small switch but the other problem is is when you're switching loads with a big amount of current then eventually the switch will wear out anyway which is something that does typically happen on older pinball machines that did work this way fortunately there is a better solution let's move on to version 2 of this circuit okay so I went and got myself a snack and I also rewired our circuit the main issue that I'm trying to solve with this version of the circuit is to allow us to act the solenoid without having to pass the full two amps through this poor little button this is going to be very important for controlling the solenoid with the Arduino because the Arduino cannot switch two amps of power it can't provide two amps of power to the solenoid which means that we're going to have to control the solenoid through a different power supply than that one coming from the Arduino itself so to solve all of our problems we are going to use a very cool little part called a MOSFET and that's this part here a MOSFET is basically an electronic switch it allows you to send a small amount of current to one pin which then controls a large amount of current through another pin and that's exactly what I've done here so again I'm gonna take you through this circuit step by step set you can understand what's going on so once again I have power coming in from my power supply into the breadboard but no the electricity is going to be able to take two different paths let's start by taking a look at the path it takes through this little push button here so we've got power coming from the power rail over to a resistor and then the resistor goes to a push button and then the push button goes to the gate on the MOSFET now why do we want to do that that's because what we want to have happen is we want the push button to tell the MOSFET to then switch the solenoid so the push button is no longer the one switching the solenoid directly it's telling the MOSFET to switch the solenoid and that all makes sense in a second now of course we need to complete the circuit so the MOSFET is also connected back to ground so it's this pin here this pin has a fuse because again we just want to protect everything and then it goes forward to ground so the one path that the electricity takes is it goes again from positive to the resistor to the push button to the gate on the MOSFET and then back to ground now the second path that the electricity can take is through the solenoid itself via the MOSFET so and now where we look at is we look at positive going from the power rail here up this wire into the solenoid and again we have our diode here which we talked about and from the solenoid it goes through here to the MOSFET and this is the middle pin of the MOSFET which is the pin that controls the higher amount of current going through the MOSFET and then back down through ground and again we've got the fuse here so when we activate this button here now I can click this button all day long and it is not going to burn out because I am not sending a ton of current through the button instead what's happening is when I activate this button I'm telling the MOSFET hey can that basically connect the middle pin with the ground pin and send current between them and that in turn activates the solenoid now one more quick thing to point out here I haven't yet talked about this resistor here and this wire here so this resistor is also going to the gate on the MOSFET so this far pin here and it's grounding that gate and the reason we want to do that is that we don't ever want to end up with what's known as a floating pin when you're working on your Arduino and that's where we're not sure of the voltage on a pin so when we activate this switch here that's going to go ahead and send a positive voltage to the gate but if there's no positive voltage going to the gate we still want to make sure that this gate is at zero volts and if we don't set up this ground here that might not happen and unless the MOSFET is grounding it itself and I'm not sure if this one is or not but typically and especially when you're working with Arduino so you end up grounding things a lot this way and that's known as a pulldown resistor so we're pulling this pin down to zero now that almost covers everything about this circuit the last thing I want to say is we are controlling the solenoid and this MOSFET through the same power source so 12 volts is coming in here 12 volts is going to the solenoid 12 volts is also going to the gate on the MOSFET so for this circuit I specifically chose a MOSFET that works with that type of voltage for its gate in the third experiment we're going to use a slightly different MOSFET that only requires logic level voltage in order to switch it okay after another snack and a little bit more rewiring I have the solenoid connected to the Arduino or actually in this case an Arduino clone so here's how it works I have changed almost nothing and that's the beauty of the last circuit is it really set us up for success here so here are the only changes first of all we are now using two different power supplies that's the last important conceptual thing to understand the Arduino is being powered by USB over here and we still have our regular 12-volt power supply here now any time that you are using two different power supplies it is critical absolutely critical that you connect the grounds of these power supplies together and that's what I've done here I have taken the ground pin on the Arduino and I have connected it to the ground power rail here that is so that both power supplies can agree on what 0 volts if you don't do this you can get all sorts of weird things happening up to and including destroying your Arduino or destroying something else now personally it took me forever and a half to understand why this is important so don't feel bad if you don't get it right away just understand that it needs to be done and then you can do some more reading about it later but it's very important to establish this the second thing that I've done is I've removed the power buttons or nut power button the push button switch that I used to have over here and instead of having the switch I've simply gone ahead and added a wire here and this wire here is gonna go to pin number eight on the Arduino in a sec and what that's gonna do is it's gonna allow the Arduino to switch this MOSFET instead of me pushing push button so it's the exact same concept as a push button but now we have an Arduino doing it oh and I also changed out MOSFETs I'm now using a logic level MOSFET which is a MOSFET that will switch the now will switch to on when you give it five volts or even 3.3 volts and I'll put the number of that part below there's a number of them out there so just pick the one that works for your project this one's been working great for me so on with the show I'm actually going to hook up the the pin eight of the Arduino because the Arduino is already running the code well but nope see the Arduino is firing the solenoid every second and what it's doing is it's sending positive voltage to the MOSFET for 20 milliseconds which seems to be more than plenty to activate the solenoid and then it's waiting a second and then it's doing it over and over and over again so here's the entirety of the source code that allows all of this magic to happen it's basically a blink program about with a solenoid instead of an LED I start off by defining pin 8 as my solenoid pin and then in the setup routine I define that as an output pin meaning the Arduino is going to be sending voltage across that pin and then in the loop I start off by setting that pin to high meaning we're gonna actuate that solenoid then we wait 20 milliseconds which is more than enough time for the solenoid to actuate and then we send a low signal to the solenoid which is telling it to not actuate and go back to its normal position and then we wait a second so that the solenoid is not going crazy and it also gives the solenoid enough time to cool off which is also kind of important to recognize that solenoids have duty cycles which means that unless you buy a solenoid that is rated for continuous use typically a solenoid has to be on for a certain amount of time at a maximum before it then needs a lot of time to cool off or at least a little time so most solenoids seem to have about a 25% duty cycle and you'll see that expressed as something like 30 seconds on 90 seconds off so make sure you look at your actual solenoids datasheet and understand how long you can have it on at a time and so the way I've structured my program is we only turn it on for 20 milliseconds and then we give it a whopping second in order to cool off if you're going to use this in a practical project obviously your codes going to be a little bit more complicated than mine but that's everything that you need to do in order to turn a solenoid on and off so I hope that was really useful to you if you want to see another example of a solenoid in action then be sure to check out my ball feed demo where I use solenoids in order to build a ball feed mechanism for a pinball machine and if you have used solenoids in your own Arduino projects or you feel there's something that I got wrong here or could have done differently then please feel free to leave a comment down below let's let everybody learn from each other
Info
Channel: Matt Inglot
Views: 162,630
Rating: undefined out of 5
Keywords: arduino, arduino code, arduino solenoid, solenoid, flyback diode, mosfet, solenoids explained, electronics, arduino tutorial, matt inglot, FQP30N06L, solenoid circuit, inductive loads, switching solenoids, diy pinball, arduino tutorials, solenoid with mosfet, solenoid controller, solenoid control, solenoid controller arduino
Id: nwVRMU9grSI
Channel Id: undefined
Length: 20min 1sec (1201 seconds)
Published: Tue Mar 19 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.