Skip to Content

Controlling a Solenoid Valve With Arduino: A Complete Guide

Controlling a Solenoid Valve With Arduino: A Complete Guide

A solenoid is an electromechanical device that converts electrical energy into a magnetic field by creating a linear movement. It comes in various sizes, depending on the application.

You can quickly learn to drive solenoids using Arduino. 

In this tutorial, I will take you through everything you need to know about solenoids.

I will provide a step-by-step connection guide and example codes to test the circuit you have just built. 

Solenoids are helpful in several applications. Primary applications of solenoids involve automatic door locking, vending machines, secure access, etc.

You can also find applications of solenoids in testing and automation. 

At the end of this article, you can build your solenoid-based project and control it using an Arduino.

Let us get started!

Components Needed To Complete Arduino-based Solenoid Project

Hardware Components 

Software is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to products on

What Is A Solenoid?

A Solenoid is a helical coil whose length is far more significant than the diameter of the coil.

Usually, we call the entire apparatus, which will convert the electrical energy into mechanical energy using a coil and the apparatus as a solenoid.

What Is A Solenoid

In the above picture, you can see the cross-section of a solenoid. The armature will pull the pole inwards when you supply power to the Solenoid due to the magnetic field.

Once you remove the energy due to spring, the pole will move outwards.

Hence, Solenoid will not consume power when the spring is in the released position.

The image to the right has the arm out. Here there is no power supplied to the Solenoid.

You can see that the arm is pulled inside in the image to the right. 

The Solenoid needs more power since it has to hold the pole in place. The spring will try to pull the shaft out, but the magnetic field will always be stronger than the spring force. 

If you have a 5 V solenoid, and it is a low-power small solenoid, you may be able to manage it by connecting to the 5 V pin of the Arduino.

It would help if you always separated the power supply for the Solenoid from the Arduino’s supply. 

Let us look at a solenoid’s datasheet. The essential specifications you can derive from the datasheet are listed below.

solenoid’s datasheet
  1. Part number – The exact name to be used while you are placing an order
  2. Voltage – The voltage rating is the required DC voltage, which you should apply to operate the solenoid. If you use a lower voltage, the solenoid may not turn on. If you apply a higher voltage, you may overheat the solenoid.
  3. Duty cycle – The duty cycle corresponds to the time you can drive the solenoids. Since the Dutycycle mentioned here is 100%, these are called continuous-driven solenoids. 
  4. Power – total power dissipated as heat in the solenoid. You can calculate it as a product of voltage rating and the current rating.
  5. Current rating – The solenoid’s current when the rated voltage is applied is its current rating. In our case, you have to make sure that your switch (TIP120) can handle the current.
  6. DCR – DC resistance. You can measure this using a multimeter.

There is an additional note mentioned in the datasheet.

additional note

The statement means that you should apply the power when pulling the arm. When you cut the power, the spring automatically pulls back the arm. 

The following sections will provide you step-by-step connection procedure, a complete wiring diagram, and the associated code needed to test the setup.

Step-By-Step Instructions To Connect A Quadrature Encoder To An Arduino

In this section, I will take you through the steps to complete the wiring diagram for a simple solenoid and Arduino project.

Let’s start!

How To Connect Solenoid to Arduino?

Here are the connection details needed to complete the solenoid and the Arduino board.

I am using Arduino UNO for this example. 

-> Read our guide about What You Can Build with Adruino.

Step 1: Start with the Solenoid

Start with the Solenoid

The Solenoid has only two connections: Positive and negative.

When you apply a sufficient voltage across the pins, the Solenoid will get activated.

Since DC voltage is needed to turn on the Solenoid, we will use an Arduino-controlled switch to provide the necessary voltage.

Step 2: Connect the Power Adapter plug

Connect the Power Adapter plug

Make sure to connect only the plug. Do not connect the power supply yet.

You must complete and verify all the connections and only then supply the power.

This will avoid damaging circuits due to accidental wrong connections!

Step 3: Connect the Negative terminal of the diode (Cathode) to 12 V positive line

Connect the Negative terminal of the diode (Cathode) to 12 V positive line

You need to place a diode across the solenoid terminals. Since the solenoid is a coil, it will have similar properties to a motor.

When you stop the solenoid, the dropping magnetic field generates a huge voltage across the solenoid in the reverse direction.

The diode will provide a short path for the voltage until all the energy stored in the solenoid is dissipated.

If you dont rrovide a diode, the high voltage generated can spoil and damage the MOSFET or even the Arduino.

Please follow the link to learn more about the reverse voltage and the diode role in protecting the circuit.

The diode’s reverse voltage rating must be atleast 100 V or higher.

The below snapshot from on of the diode’s datasheet shows you various option.

My recommendation is to go for any diode other than 1N4001.

diode’s datasheet

Step 4: Connect the Anode of the diode to the negative terminal of the solenoid

Connect the Anode of the diode to the negative terminal of the solenoid

Place the diode close to the solenoid as much as possible using short cables to connect them together. 

Step 5: Connect TIP120 to Arduino Ground

Connect TIP120 to Arduino Ground

TIP120 is a general-purpose PNP amplifier. The TIP120 is famous for interfacing Arduino to DC motors, actuators, and solenoids.

The IC has a very good power handling capability. You can connect solenoids with a current rating of up to 4 A. 

We are using the TIP120 transistor as a switch. When we want to turn the solenoid on, we will drive the base of the transistor using an Arduino GPIO pin. 

Step 6: Connect TIP120 to GND pin of DC jack

Connect TIP120 to GND pin of DC jack

Step 7: Connect the base resistor to Pin 1 of TIP120

Connect the base resistor to Pin 1 of TIP120

Choose a 220 ohms or a 470 ohms resistor to the base of the transistor. The collector current will flow through the solenoid.

Hence, if the solenoid needs 1 A of current, the collector current has to be 1 A at least. 

You can easily calculate the base current using the formula (5 V – 0.7 V) / 220 = ~20 mA. If you use a 470 ohms resistor, the base current will be (5 V – 0.7 V) / 220 = ~9 mA. 

The gain of the transistor is about 2500 (hfe parameter). It means the collector current will be 2500 times the base current.

Since the base current is nine mA, the collector current can go up to 2500 * 9 mA = 17.5 A. 

Hence TIP120 is a favorite choice for many circuit enthusiasts!

Step 8: Connect the other end of the resistor to Pin 13

You can choose any of the available GPIOs on the Arduino. In this example, I have chosen pin 13.

Since Pin 13 is also connected to an LED, you can monitor the code function. 

The example Arduino code shows that both onboard LED and the solenoid will toggle at the same rate!

Connect the other end of the resistor to Pin 13

Step 9: The Entire connection

The Entire connection

You can verify the connection once more. Make sure to match the supply voltage with the voltage rating of the solenoid. 

Congratulations on completing the arduino-solenoid project connections.

The next section will show the basic Arduino code needed to toggle the solenoid on and off. 

-> Read our article about How Easy Is It To Learn Arduino?

Arduino Code For Solenoid And The Arduino Uno Project

Here is a simple Arduino sketch you can use to program the Arduino. The Arduino code is the same as the one you find in the examples.

Since the built-in LED is connected to the Pin 13, I have also connected the solenoid to pin 13 of the Arduino UNO. 

The example code toggles the Pin13 every 1 second.

Hence the solenoid will also toggle every one second.

  Turns an LED on for one second, then off for one second, repeatedly.
  Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
  it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
  the correct LED pin independent of which board is used.
  If you want to know what pin the on-board LED is connected to on your Arduino
  model, check the Technical Specs of your board at:
  modified 8 May 2014
  by Scott Fitzgerald
  modified 2 Sep 2016
  by Arturo Guadalupi
  modified 8 Sep 2016
  by Colby Newman
  This example code is in the public domain.
// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   
// turn the LED on (HIGH is the voltage level)
// wait for a second
  digitalWrite(LED_BUILTIN, LOW);    
// turn the LED off by making the voltage LOW
 // wait for a second

The code is easy to understand. In the setup function, you define pin 13 as an output pin.

The main action happens in the loop function.

In the loop function, we change the pin status from 1 to 0 and 0 to 1 with a delay of 1 second.

FAQ’s About The Solenoid And Arduino Project

I have brought together the commonly asked questions about the Arduino with solenoid projects.

If you have questions that are not answered in this section or the article, please send me your questions via the comments box. 

1) How do you use Arduino to actuate a solenoid valve?

You should use a switch to drive the solenoids. One example of a power switch is a TIP120 IC which is very useful in these applications.

Since the current drawn by the solenoids is usually high, the switch is a must.

The article above provides you with an example of a switch usage to drive the solenoid.

2) How do you activate a solenoid?

You can activate a solenoid by applying a specific voltage across its terminals. There is a variety of solenoids in the market.

You have to find the voltage rating from the particular part’s datasheet.

Part numberSupply
DSTL-0418-066 V DC
DSML-0630-12C12 V DC
DSTL-0216-099 V DC

In the above table, I have listed three example part numbers of solenoids. The recommended operating voltages are mentioned in the second column.

3) What is the difference between intermittent and continuous driving of a solenoid?

Some solenoids are intermittent, and some are continuous solenoids. You drive the intermittent solenoid for a short term and let it cool down before going it again.

There are applications where you need more power for a short time. You have to allow the solenoid to cool down again before using it the second time. 

In the case of continuous solenoids, you can drive them forever.

They will withstand the power dissipation for long.

> Check out our guide to the Top 12 Best Arduino Online Courses


In this article, I have taken you through the basics of a solenoid, its applications, and an example project to build and test your solenoid circuit. 

I am hopeful that you have got all the necessary details to build your next solenoid project with confidence.

I have given basic explanations and background for the solenoid parts wherever needed.

If you have further questions, I will be glad to hear from you.

Please feel free to post your suggestions and comments in the comments window. 

Do you have any feedback to improve this article? Which projects would you like to build next?

What are your ideas to improve this article? Please let me know in the comments section.

Please dont forget to share this article with your friends!


Thursday 10th of August 2023

hello , please if you can to help me . i have solenoid that turn on with 5V and turn off with -5V HOW can i controlle this with arduino


Friday 11th of August 2023

You could use an H-Bridge or a DPDT Relay instead of the single transistor to switch the polarity.


Saturday 10th of December 2022

Hello I like what you’ve done in this tutorial, it’s clear and concise. I want to built an array of five normally open solenoids each activated at a specified temperature thru a thermoprobe Can you steer me how to go about doing that Peter