Introduction: Controlling Solenoids With Arduino

In this instructable we will be building a simple circuit that will allow us to control a solenoid using the popular physical computing platform arduino. I became interested in controlling solenoids for robotic music applications. A similar circuit can be used to control a motor or other devices that require more current than the arduino can provide directly from its output pins.

Since the solenoid requires higher current than the arduino can provide we will be using an alternative power source for the solenoid (a couple batteries) and will be "driving" it with a TIP120 transistor.

The circuit we're using can be found online here. This instructable draws from the excellent documentation in Tod Kurt's bionic arduino workshops.

Build time was exactly one-beer. Excluding the arduino and computer you use to program it, we are using less than $10 in parts. Ok!

Step 1: Parts

• Arduino board
• USB cable for programming and powering the Arduino
• Breadboard
• Some jumper cables
• A 1K resistor
• TIP120 transistor (TIP102 will also work fine)
• 1N4004 diode (1N4001 also works)
• Some batteries and connectors for solenoid power
• A solenoid with leads to connect to the breadboard

Step 2: Building the Circuit - Power Connections

USB powers the arduino, the batteries power the solenoid. A jumper grounds the two together.

Step 3: Building the Circuit - Transistor Time

 When the transistors labeled side is facing up the legs (from left to right) are B, C, E: Base, Collector, Emitter.

We will connect the output pin of the arduino to the Base leg of the transistor through a 1K resistor.  The Collector leg of the transistor will be connected to the ground leg of the device we are driving (our solenoid).  The Emitter leg is connected to the ground channel of our circuit.


Step 4: Building the Circuit - Connecting the Solenoid

The "ground" leg of the solenoid is connected to the collector leg of the transistor.  The "power" goes to the high voltage power channel (from our batteries).  I put "ground" and "power" in quotation marks because none of the solenoids I've ever used have been polarized so it doesn't matter which lead is connected to ground and which goes to power.  

The diode connects the power channel to the solenoid-ground-leg/transistor-collector-leg, preventing the kickback voltage from damaging the circuit.  The diode is polarized and should be oriented with white/sliver stripe on the power channel side of the connection.

I'm using a pull-type solenoid rated for 12 to 24 volts (got it here), which has plenty of kick.  You'll need some kind of spring to pull the shaft back out after it's been pulled in - I used a small length of insulated wire wrapped around the shaft to create a makeshift spring.  In a different sort of configuration one could use gravity to pull the shaft back out (but you'd need some of stopper to prevent the shaft from falling all the way out).

Step 5: Program Arduino and Enjoy

Now just program the Arduino to drive the appropriate output pins and enjoy.  I've connected the transistor to pin 13 so I can see the built-in LED work in time with the solenoid.  The sketch I've used is a simple modification of the "Blink" sketch where I've reduced the on time to 80 milliseconds, which is more than enough time to pull the solenoid all the way in.



Comments

author
0range made it!(author)2017-07-07

Hi, I'm trying to use this circuit to control a solenoid in a Double Water drip system by a guy called Ted Kinsman. He used a reed switch , but I can't find one. I've copied his sketch for my Arduino, it uploads, can't get it to work, do I have to change Ted's sketch to get the solenoid circuit to activate when it should? I don't know anything about electronics or circuits, any help would be appreciated

author
EricT182 made it!(author)2017-07-01

Hello - What additional components are required to connect three solenoids in parallel on this circuit? I wanted to use power of 3 solenoids of 12v 1 Amp in my project.

thanks

Eric

author
mdauria made it!(author)2017-06-19

Hello ! Thank you very much for your precise explanations. I realized the same montage and my solenoid seems to get very hot very quickly ... is this normal? Is this due to the 18v voltage instead of the 12v recommended? Is it dangerous?

author
RosediC made it!(author)2017-05-09

I found at first my 12v solenoid did not have enough power. So I use TP120 to drive a 6V relay and let the relay do the switching of 12vdc direct from power supply to the solenoid.

20170509_134605.mp4
author
Rocko8191 made it!(author)2017-04-22

Hey Guys!

after wiring the circuit exactly as it is shown in the diagram, im not getting any interaction with the uno, the solenoid would just retract and stay that way until I remove the alligator clip from the positive lead. i am using a 2n4401 transistor instead of the TIP, could this be the reason? plz help, i have alot of money invested into this vision lol!!!

author
AnandR37 made it!(author)2017-04-29

I went through the same issue. Used a TIP and it worked fine.

author
AnandR37 made it!(author)2017-04-29

Awesome! Combined this one with https://core-electronics.com.au/tutorials/solenoid-control-with-arduino.html. Used normal power source.

IMG_20170429_182846901.jpg
author
AbiramiN1 made it!(author)2017-04-12

can i do the same thing with raspberry pi?

author
Axertion made it!(author)2017-04-20

Yes, you should be able to as the GPIO pinout on the raspberry pi has the same 5v output as the Arduino.

author
AbiramiN1 made it!(author)2017-04-14

I have used this circuit for my project. Thank you for sharing this.

IMG_20170414_122126.jpgIMG_20170414_122142.jpgVID_20170414_121648.mp4
author
anas-b made it!(author)2017-03-27

could you send me the code please..

author
dieonroger made it!(author)2017-03-07

I used a 5V Solenoid with 3 AAA batteries.

20170307_192049.jpg
author
mohadalali made it!(author)2017-01-24

Hello, what other alternatives for the TIP120 and TIP102 due to the unavailability of them

author
JohnathanW15 made it!(author)2017-02-20

i forgot to mention, after checking a mosfet can handle the volts and amps you need, look for RDS on value... that is the resistance through the part when the "switch" is on. n channel is lower but p channel is easier to use and still very reasonably low.

author
JohnathanW15 made it!(author)2017-02-20

use a "logic level mosfet" they come in N and P variety. N turns on when the gate pin is driven high, p turns on when get is grounded. you can also use p channels as reverse polarity safety devices on stuff you build and frequently connect/disconect power wire... if you tie the gate pin to ground then connect positive through the remaining terminals... the switch will be off if positive gets connected where ground is supposed to be on your power in. ;)

author
JohnathanW15 made it!(author)2017-02-19

its 2017, use a n or p channel mosfet

author
FaizaK5 made it!(author)2017-01-03

I need its ardino code , can u help me?

author
trevorg37 made it!(author)2017-02-15

It should be easy, Its just send a high then low to the correct pin

author
EddieH33 made it!(author)2016-12-09

After recreating the circuit numerous times, I've found that I cannot get the transistor to engage, and that the circuit as I have done it runs current through the solenoid with or without voltage from the digital pin. Is there any explanation for this? I've wired two 9v batteries in series, and am using a 24v solenoid (like one used for a sprinkler). Both my transistor and diode are NTE, but should be interchangeable with the suggested models, as per the catalog. I greatly appreciate any input.

author
nayarasmontes made it!(author)2016-07-06

Hello!


What can I do when, after all of the connections are according to the tutorial, the solenoid works with voltage variation (if I change the current for example) and simply it ignores the signal sent by Arduino?

author
BrilliantM made it!(author)2016-07-07

I have the exact same problem. Have you worked out how to fix it?

author
nayarasmontes made it!(author)2016-07-08

I just rebuilt the circuit but basing on the photo and not on the scheme.

author
EddieH33 made it!(author)2016-12-09

What's the difference between the photo and the scheme? Just wondering as I am experiencing the same problem.

author
FrancoisD37 made it!(author)2016-11-23

just wondering if it would be possible to add another solenoid into the circuit and have it run at a different speed than the first solenoid??

author
jts3k made it!(author)2016-11-29

yep just duplicate the circuit for any additional outputs and modify the code for the extra output pins

author
FrancoisD37 made it!(author)2016-12-01

thanks and do you know how i could control the solenoid at differnt speeds?

author
PRAMODD6 made it!(author)2016-11-29

what is the use of diode in the above circuit???

author
jts3k made it!(author)2016-11-29

it prevents "kickback" current from damaging the arduino

author
AtikahA1 made it!(author)2016-06-13

how to control the on/off of the solenoid? include into the coding the condition it should be on and off?

author
NickH105 made it!(author)2016-05-24

where can I buy this solenoid? I am European

author
Ccameron0306 made it!(author)2016-03-26

I'm doing a project in school and need to operate three solenoids independent of each other. Is there a way to manipulat this setup to do that? Can anyone please help me with that?

author
vikask53 made it!(author)2016-04-09

Yes u can do that.above shown procedures is for one solenoid,similarily you have to use same circuit for controlling 2 or more solenoid indiviually.use three transistor and make the three circuit indiviual.just u have to use same power supply for all the three thats all

author
HanifM7 made it!(author)2016-04-08

you could use relay for each solenoid. and control those relay with arduino.

author
JuanM130 made it!(author)2016-03-04

Please, I need your help, I am Using the TIP 122 and is hot when the circuits is On, same with the Solenoid, this is normal? or I need to do something?

Thanks for your answers

author
YZ88 made it!(author)2015-12-16

This circuit is used to substitute a relay board???

author
Jonix06 made it!(author)2015-12-08

Can look at the codes? Ryply asap.

author
pheaktrainstruction made it!(author)2015-11-26

Nice project..! Do you have any code of solenoid lock or Unlock? please....!

author
Anurag+N made it!(author)2015-10-30

I want to use the pneumatic cylinder at slightly higher switching speeds. Can I use the IN4148 diode? The solenoid valve that I am using works at 24V DC.

author
oomgalih made it!(author)2015-10-05

Did you minded, if i see your schematic?

author
ThatchyLotus made it!(author)2015-10-01

Hello - we are currently using your instructions as a reference tool for a project and are confused at the discrepancy of the Arduino board vs the Arduino One board.

author
PushparajG made it!(author)2015-09-26

i need coding for this above video

author
effaowona made it!(author)2014-09-15

Hello, the solenoid i'm trying to control has 3 pins. where should i connect these pins? thanks!

bobin.jpg
author
talkingmonkey made it!(author)2015-05-19

I'd be interested to know as well. I've you figured out how to wire it?

author
talkingmonkey made it!(author)2015-05-20

*Have you? (late night typing)

author
BasK2 made it!(author)2015-06-23

I know this question is a month old, but it might be usefull for other people.

In valves like these, you have 2 parallel and 1 perpendicular pins. The parallel ones need to be connected to your output driver. It doesnt matter which pin gets the positive/negative or live/neutral. The perpendicular pin is for grounding.

author
talkingmonkey made it!(author)2015-06-23

Thanks for the reply :)

author
LBurrito made it!(author)2015-06-02

I believe that is a solenoid valve for water.

author
mjtrac made it!(author)2015-05-31

Thank you, this is very helpful to us newbies.

author
trimitsos made it!(author)2015-04-29

How do you modify the code in order to run twice a day?

Every 12hrs for exaple for watering plants needs to stay open for 2 mins and repeat?

What is the difference if i use a relay instead a transistor?

Thank for sharing

author
TeamJaeger made it!(author)2015-04-06

I've used this circuit many times now to control solenoids but also to control motors via pwm. Good Stuff - Thanks for Sharing!

IMG_1482.JPGIMG_1481.JPGIMG_1480.JPGIMG_1460.JPG

About This Instructable

359,598views

285favorites

License:

More by jts3k:Controlling solenoids with arduinoSuperterrific Tub Bass
Add instructable to: