BC Robotics

Controlling A Solenoid Valve With Arduino


In this tutorial we will be controlling a solenoid with an Arduino and a transistor. The solenoid we have picked for this tutorial is our Plastic Water Solenoid Valve (perfect for controlling flow to a drip irrigation system) but this tutorial can be applied to most inductive loads including relays, solenoids, and basic DC motors. 

A Few Considerations:
Before choosing this solenoid valve for a project there are a few things that should be considered:

• Water can only flow in one direction through this valve.
• There is a 3 PSI minimum pressure requirement on the inlet otherwise the valve will not shut off.
• This solenoid valve is not rated for food safety or use with anything but water.

How It Works:
This valve is very similar to those found in a lawn irrigation system – the only real difference being the size. The inlet water pressure actually holds the valve closed so if you do not have any inlet water pressure (3psi minimum) the valve will never close!

Parts Used In This Tutorial

This tutorial will be requiring a few common parts: 

The Schematic

This handy little diagram shows how we will be connecting everything. Don’t worry if it looks a little overwhelming, we will be going through this step by step!  

Step 1 - Powering The Breadboard

Let’s go ahead and set up the Arduino Uno and the breadboard right next to one another. The solenoid works with anywhere between 6-12V which is too high to use with the standard Arduino 5V. To get around this problem we will be using a 9V power supply – the solenoid will operate at 9V while the Arduino’s built in voltage regulator will turn that 9V into the 5V that it needs to operate. To gain access to the raw voltage going into the DC barrel jack on the Arduino Uno we will use the “Vin” pin located next to the ground pin on the Arduino. 

Start by connecting one of the jumper wires to the “Vin” pin on the Arduino and running it over to the positive rail on the side of the solderless breadboard. Next, run a wire from the Ground pin on the Arduino over to the negative rail on the solderless breadboard.

We now have 9VDC power on the breadboard! With the exception of the “Vin” pin, 9V is more than enough to damage your Arduino so do not plug ANY other pins from the Arduino into the positive rail on the breadboard.


Step 2 - The Solenoid's Wire Harness

This solenoid does not have a wire harness and instead relies on 0.250″ Quick Connects. These are the best way to connect the solenoid. If you do not have Quick Connects laying around, Alligator Clips or even soldering wires to the tabs will work!

The connections on the solenoid do not matter, the coil does not care which side is positive or negative. 


Step 3 - Solenoid To Breadboard

Connect the solenoid to the breadboard – we will need to add a diode between the two contacts so we will leave some space for that. 


Step 4 - Snubber Diode

Since a solenoid is an inductive load we need to include a snubber diode across the contacts. Snubber diodes help eliminate transient voltages caused when a magnetic coil (such as those found in a motor, relay, or solenoid) suddenly loses power. Without this diode in place the transient voltage spikes can damage other elements of the circuit.

The snubber is placed from the negative side of the coil to the positive side. Since diodes only allow current to flow in one direction we need to make sure we get this right, otherwise it will be a dead short between power and ground. Ensure the side with the White stripe is connected to power/positive side of the solenoid! In our circuit the Red wire is the positive 9V so we will connect it to this side. 


Step 5 - Power To The Solenoid

Now that we are sure the diode is facing the correct direction the solenoid can be attached to the 9V Power on the breadboard. The solenoid gets constant power because we will using low side switching to turn on and off this solenoid. Low side switching means we will be interrupting the circuit between the negative side of the solenoid and the ground rather than between the power and the solenoid. This seems a little counter intuitive, but we do this because switching the high side is a lot more difficult with a transistor when the voltage being switched is higher than the Arduino’s 5V logic. Don’t worry, in later tutorials we will go into high side switching! 


Step 6 - The Transistor

The current draw of this solenoid is higher than a standard transistor can handle so we will be using a TIP120 Darlington Transistor. A Darlington transistor is actually a pair of transistors that act as a single transistor with a high current gain. The pin output is still the same as a standard transistor so (for now) just think of this as a transistor with a higher current rating.

We will start by placing this transistor in the breadboard. 


Step 7 - Base Resistor

A base resistor is exactly what it sounds like – it is a resistor placed on the base pin of the transistor. This resistor limits the current going to the base (control line) of the transistor; no resistor would result in no current limit, and could result in a transistor blowing up! We will be using a 1K ohm resistor in this case; it can be placed from the base of the transistor as shown. 


Step 8 - Connecting To The Arduino

Now that the current limiting resistor is in place we can go ahead and connect this up to one of the Arduino digital pins. Take a wire and run it from Arduino pin 4 to the current limiting resistor we just placed in the last step. 


Step 9 - Connect The Solenoid

Next we are going to connect the solenoid’s negative terminal to the collector on the transistor. The collector is one side of the “switch” in a transistor, this is connected to the emitter (other side of the “switch”) when the base pin is has a voltage applied. An easy way to remember what goes where on a transistor is:

“The Collector collects whatever the Emitter will emit when the Base commands it to”

So in this case we are going to “collect” the negative from the solenoid and “emit” it to the ground of the circuit. So let’s run a wire from the solenoid negative to the middle pin (collector) of the transistor. 


Step 10 - Connecting To Ground

Now we will connect the transistor’s emitter to the ground rail on the breadboard. The circuit is complete! 


Step 11 - Double Check And Plug It In!

Before we give the Arduino power it is always a good idea to go over all of the connections to make sure there are no wires in the wrong spot – sometimes that can make for a very expensive mistake!

One way to avoid this problem is good wire color discipline. In other words, decide on a purpose for each color of wire and stick to them! In this example all 9V power are red wires, all grounds are black wires, orange is the negative output of the solenoid, and yellow is the signal wire. This way, if you ever see a red wire going to a black wire you will know right away that something isn’t quite right! 


Step 12 - Starting The Code

					void setup() {
// put your setup code here, to run once:
void loop() {
// put your main code here, to run repeatedly:

Now that we have finished with the hookup we need to start writing some code. We will be using the Arduino IDE, this is available from https://www.arduino.cc/en/Main/Software

We will start with the “BareMinimum” sketch found by clicking “File” and selecting Examples / Basic / BareMinimum. This sketch is a great starting point as it includes the Setup and Loop functions – we will write the rest! 


Step 13 - Understanding How To Control The Solenoid

Since the transistor is doing all of the heavy lifting in this circuit we do not need to do much in terms of coding. When Arduino pin 4 is set to HIGH this will connect the transistors collector to the transistor’s emitter, which will activate the solenoid.

Let’s go write the code!


Step 6 - Writing The Code

We are starting with the BareMinimum Sketch found in the IDE, it should look something like this: 

					void setup() {
// put your setup code here, to run once:
void loop() {
// put your main code here, to run repeatedly:

So first we will need a variable for the Arduino pin: 

					int solenoidPin = 4; //This is the output pin on the Arduino we are using

void setup() {
// put your setup code here, to run once:

void loop() {
// put your main code here, to run repeatedly:

Next we need to set the Arduino pin to act as an output: 

					int solenoidPin = 4; //This is the output pin on the Arduino we are using

void setup() {
// put your setup code here, to run once:
pinMode(solenoidPin, OUTPUT); //Sets the pin as an output

void loop() {
// put your main code here, to run repeatedly:

Ok, now all of the pins are set to outputs. Next we can write some code in the loop to switch a relay on and off:

					int solenoidPin = 4; //This is the output pin on the Arduino we are using

void setup() {
// put your setup code here, to run once:
pinMode(solenoidPin, OUTPUT); //Sets the pin as an output

void loop() {
// put your main code here, to run repeatedly:
digitalWrite(solenoidPin, HIGH); //Switch Solenoid ON
delay(1000); //Wait 1 Second
digitalWrite(solenoidPin, LOW); //Switch Solenoid OFF
delay(1000); //Wait 1 Second

So if we want the solenoid to allow water to flow, set the pin high. When you want the water to stop flowing, set the pin low. In this case it will turn the water on for 1 second and then off for 1 second, looping forever (or at least until it is unplugged!) This solenoid valve could easily be used with the flow meter featured in our last tutorial to create a system that only allows a certain volume of water to flow before shutting off. 


Step 15 – Upload The Code And Test

Now that all of the code has been written it can be uploaded to your Arduino! Click “Upload” button in the top left corner of the Arduino IDE and it should upload without any issues. After a few seconds the solenoid will start opening and closing. 


183 thoughts on “Controlling A Solenoid Valve With Arduino”

Leave a Reply

Your email address will not be published.

Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
  • Attributes
  • Custom attributes
  • Custom fields
Click outside to hide the compare bar
Compare ×
Let's Compare! Continue shopping