Arduino Lesson 4 - Solenoid control using a Transistor

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello welcome to diy tech guy today i'm going to show you how to control a 12 volt solenoid through an arduino using a transistor as a switch a solenoid requires a larger voltage than the arduino can supply so we need to use the 5 volt pin from the arduino to switch the higher power on and off the techniques described in this video would apply to any project where you need to switch on and off higher dc voltage devices such as motors solenoids led light strips etc [Music] the parts we'll need for this project are the following we'll need a solenoid which is an electromagnetic coil that pulls a metal rod into the coil these are used everywhere from pinball machines water and air valves and locking mechanisms we'll need a transistor to act as our electronic switch since the arduino cannot provide 12 volts of power through its pins the transistor will allow us to switch the solenoid on and off using just a 5 volt signal from the arduino a 1k resistor is required to attach the arduino pin to the transistor we're using an in-4001 diode to prevent an electronic spike caused by the solenoid from going back into the electronics of the arduino we'll also need six jumper wires and finally we'll need an arduino uno and a breadboard okay let's go ahead and set up our hardware for this project like always the first thing we're going to do is connect those power rails so we're going to connect our five volts to our positive rail and we'll take our ground oops five volts to positive rail and our ground to our negative rail my grip is a little sticky here's the solenoid we're going to be using this is a 12 volt solenoid it pulls about almost an amp so this is obviously something we can't run through the arduino so we're going to use a transistor to switch the negative side of that of that solenoid so let's start off by putting our transistor in there across three separate columns there and what we're going to want to do we're going to take this back leg the one all the way on the right and we're going to bring that to ground because that is the piece that we're going to be switching so that'll go to ground we're going to need a diode because when this thing swings back it creates a magnetic pulse that could uh blow out the arduino and make it reset or even worse even fry the chip so every time that thing releases because uh deserves an electromagnet here it acts almost like an electric generator and creates a pulse that can go back through your electrical system in your arduino so we put a a diode this is an in 4001 across the two leads on the solenoid so we'll go ahead and stick this in place here get this right here now the stripe on the diode will always be facing the positive side of your device so this piece that's getting switched here you connect to the middle pin and go to the negative side of the diode and for the positive pin we have a 1k resistor that we're going to bridge that little gap and then from there we're going to bring that to our control pin for the that we're going to use to switch this transistor and we're going to use pin number seven so when pin number seven is high it's going to close that switch and bring this solenoid to ground so that it is that it is powered so now we can hook up our solenoid oh wait no we don't we need our our power end of this and this is kind of interesting this is a 12 volt power supply and the arduino does not provide 12 volts but if we use a 12 volt power supply that has enough amperage to run the solar we can plug that into our arduino now arduino will automatically bring that down to 5 volts for the arduino but there's sort of a neat trick in here there is a pin called n and that's voltage in and voltage in is usually used if you're bypassing uh the barrel jack and you'd see this mainly on a uh like an arduino nano which does not have a barrel jack you always power your device through the vin and you could put in i think up to 12 volts it might be up to 24 i'm not sure but if you do power from here which we're going to do it also translates that to over here so it's almost as if we're tapping into that original line which is what you would normally do with the voltages so i can tap into vin and bring that over here and i'm bringing power directly to that to that set of pins there and that's 12 volts directly from the power source and it's separate from the arduino power because it's prior to getting stepped down into 5 volts we have our two rows of holes here one is the positive 12 volt which we want to put our red wire onto and let's take that into there and then the other is on our ground and this is the switched ground that will be controlled by transistor so let me get it in there and there you have that's all that's required now when pin seven is brought to high the the transistor will connect these two pins which will allow the solenoid to retract when the pin is turned low power will be cut off and it it'll open up again so we'll be able to control this using the arduino and the diode here is going to protect our device from that electromagnetic pulse that's going to come from the electrical generation from this magnet being pulled across those coils or that piece of metal being pulled across those coils here is one final look at our completed hardware bill all we have to do now is to add the code to the arduino and test it out let's go ahead and enter the code for this project the code is rather simple as there isn't much going on here we are simply controlling the transistor through digital pin 7 and we didn't add any inputs to this particular project first we need to declare pin 7 as a global variable called solenoid pin we need to set solenoid pin as an output with this line of code in our setup routine for our main void loop we must first turn the pin high to switch the transistor into a closed twist position this will activate the solenoid magnet causing to pull the rod into the magnetized coil we will hold it there for two seconds using this delay statement once the delay has completed we will write the pin to low to turn the solenoid into an open switch state thus releasing the rod from the magnetized coil we'll delay for another two seconds and repeat the process indefinitely click the upload button oh i forgot to plug it in there we go let's try that again it's uploading and it's done alright it's uploaded to our board let's try this out we can't run it from this uh cord because this only provides five volts but once we plug this into our twelve volt wall work we should start seeing this thing start to uh open and close it looks like everything is working as designed the solenoid is alternating between on and off every two seconds this project gives us some great examples of how to handle devices that require more power than the arduino can provide we accomplished this by using a transistor to switch the higher power this design only works on dc power so this wouldn't work with any standard appliances such as 120 watt light bulb switching ac power is very similar except it uses a relay instead of a transistor be sure to check out our video on relays which provide the ability to switch ac power thanks for watching my video please consider subscribing or clicking the bell icon to be notified of future videos you
Info
Channel: DIY Tech Guy
Views: 30,197
Rating: undefined out of 5
Keywords:
Id: Yh3TLXihUps
Channel Id: undefined
Length: 10min 16sec (616 seconds)
Published: Fri Dec 04 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.