Controlling Solenoids With Arduino

408,289

299

95

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.



7 People Made This Project!

Recommendations

  • Big and Small Contest

    Big and Small Contest
  • First Time Author

    First Time Author
  • Make it Glow Contest 2018

    Make it Glow Contest 2018

95 Discussions

0
None
bcboy1956

Question 7 weeks ago on Step 5

I need help with the code for this project I would like to open and close the solenoid on repeat basis I would like to be able to control the time between opening and closing ,

Thanks for your help

1 more answer
0
None
hitman470bcboy1956

Answer 6 weeks ago

Hi,

Here's the code :

void setup() {
// put your setup code here, to run once:
pinMode(13,OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
digitalWrite(13,HIGH);
delay(5000);
digitalWrite(13,LOW);
delay(5000);
}

Use the pin 13.

How did you power your circuit ? I use a 12 V battery but it doesn't work.

0
None
ManasC4

Question 5 months ago

Could you post the Arduino code you wrote to program the solenoid? Would be really helpful

0
None
vinipux

5 months ago

I recommend to use this board – Multifunctional POWER SHIELD 6+6 T800 more info. http://v-vTech.com

0
None
AAxelB

10 months ago


Hi, thanks for sharing, everything works perfectly. But it turns out that I'm facing a problem, I try to connect my solenoid to a jack, and connected an input jack to the arduino, but I can not run my solenoide like that, I think even have broked the transistor tip120, fortunately I have other. I start in electronics, if someone can help me?

0
None
hazimh3

1 year ago

can i know what code use for this controlling solenoids. Cause I had try many code for my project. Only this code for controlling solenoids that only match. Ty .. appreciate it

0
None
0range

1 year ago

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

0
None
EricT182

1 year ago

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

0
None
mdauria

1 year ago

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?

0
None
RosediC

1 year ago

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.

0
None
Rocko8191

1 year ago

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!!!

1 reply
0
None
AnandR37Rocko8191

Reply 1 year ago

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

0
None
AxertionAbiramiN1

Reply 1 year ago

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

0
None
anas-b

1 year ago

could you send me the code please..

0
None
mohadalali

1 year ago

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

2 replies
0
None
JohnathanW15mohadalali

Reply 1 year ago

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.

0
None
JohnathanW15mohadalali

Reply 1 year ago

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. ;)

0
None
JohnathanW15

1 year ago

its 2017, use a n or p channel mosfet

0
None
FaizaK5

1 year ago

I need its ardino code , can u help me?