Introduction: Control a Solenoid With Arduino

About: The RobotGeek team is a 6-man operation that wants to make it even easier to use Arduino to make electronics and robots. Check out our instructables and robotgeek.com for all of our awesome kits.

This Arduino Solenoid Tutorial shows how to control a solenoid using pushbuttons and a relay with your Arduino compatible controller. Solenoids are electromagnetically driven actuators. When voltage is applied to the solenoid coil the electromagnetic force pulls the center plunger in. It is an excellent mechanical solution for all kinds of DIY projects requiring short quick linear actuation. Solenoids are most often found in latching and trigger-like mechanisms, such as door locking systems, paintball guns, pinball machines, dot matrix printers, and fuel injectors. Let's get started.

Step 1: Project Parts List

These are the parts recommended to follow through the tutorial while it is possible to complete this tutorial with other parts (or less parts, as in a single solenoid, relay, and pushbutton), and the concepts learned in this can be applied to other relays, solenoids, and Arduino compatible boards and shields, we recommend these parts for the easiest transfer of this knowledge.

Step 2: Wiring

Device Sensor Shield Port
RobotGeek Pushbutton 1 Digital Pin 2
RobotGeek Pushbutton 2 Digital Pin 4
RobotGeek Pushbutton 3 Digital Pin 7
RobotGeek Relay 1 Digital Pin 8
RobotGeek Relay 2 Digital Pin 12
RobotGeek Relay 3 Digital Pin 13

Wire everything as shown in the diagram. The photograph shows the correct wiring of one solenoid. All the solenoids will be wired the same. If you only have one solenoid, match the pushbutton and relay numbers when plugging into a pin on the sensor shield (if you plug the pushbutton in on pin 2, plug the relay in on pin 8, 4 - 12, 7 - 13).

Step 3: Demonstration Code 1

You can find this Demonstration Code in the RobotGeek Libraries and Tools under:

RobotGeekSketches -> Demos -> Solenoid -> directControlSolenoid -> directControlSolenoid.ino

This code simply activates a solenoid while a button is being pressed, and deactivates it once the button is released. This is great for quickly testing your solenoid, but doesn't have much real world application beyond that. Load this code to your arduino with the power to the solenoids unplugged. During the upload, the data transfer can sometimes cause the relays to fire, which can be dangerous if the power to the solenoid is connected. Once the code is uploaded, connect the 6V power supply to the arduino and the 12V power supply to the barrel jacks between the relays and the solenoids.

/*

Controlling a Solenoid with Arduino

This demo shows how to control a solenoid using pushbuttons and a relay with
your Arduino compatable controller.
 
 
 The circuit:
 * RobotGeek Pushbutton - Digital Pin 2
 * RobotGeek Pushbutton - Digital Pin 4
 * RobotGeek Pushbutton - Digital Pin 7
 * RobotGeek Relay - Digital Pin 8
 * RobotGeek Relay - Digital Pin 12
 * RobotGeek Relay - Digital Pin 13
 */

// constants won't change. They're used here to set pin numbers:
const int button1Pin = 2;     // the number of the pushbutton1 pin
const int button2Pin = 4;     // the number of the pushbutton2 pin
const int button3Pin = 7;     // the number of the pushbutton3 pin
const int relay1Pin =  8;      // the number of the Relay1 pin
const int relay2Pin =  12;      // the number of the Relay2 pin
const int relay3Pin =  13;      // the number of the Relay3 pin

// variables will change:
int button1State = 0;         // variable for reading the pushbutton status
int button2State = 0;         // variable for reading the pushbutton status
int button3State = 0;         // variable for reading the pushbutton status

void setup() { 
  
  // initialize the pushbutton pin as an input:
  pinMode(button1Pin, INPUT);     
  pinMode(button2Pin, INPUT);     
  pinMode(button3Pin, INPUT);    
  // initialize the relay pin as an output:
  pinMode(relay1Pin, OUTPUT);    
  pinMode(relay2Pin, OUTPUT);    
  pinMode(relay3Pin, OUTPUT);    
   
}

void loop(){
  
  // read the state of the pushbutton values:
  button1State = digitalRead(button1Pin);
  button2State = digitalRead(button2Pin);
  button3State = digitalRead(button3Pin);

  // check if the pushbutton1 is pressed.
  // if it is we turn on the small relay/solenoid
  if (button1State == HIGH) {     
    // turn relay on:    
    digitalWrite(relay1Pin, HIGH);  
  } 
  // When we let go of the button, turn off the relay
  else if ((button1State == LOW) && (digitalRead(relay1Pin) == HIGH)) {
    // turn relay off
    digitalWrite(relay1Pin, LOW); 
  }  
  
  // check if the pushbutton2 is pressed.
  // if it is we turn on the small relay/solenoid
  if (button2State == HIGH) {     
    // turn relay on:    
    digitalWrite(relay2Pin, HIGH);  
  } 
  // When we let go of the button, turn off the relay
  else if ((button2State == LOW) && (digitalRead(relay2Pin) == HIGH)) {
    // turn relay off
    digitalWrite(relay2Pin, LOW); 
  }  

  // check if the pushbutton3 is pressed.
  // if it is we turn on the small relay/solenoid
  if (button3State == HIGH) {     
    // turn relay on:    
    digitalWrite(relay3Pin, HIGH);  
  } 
  // When we let go of the button, turn off the relay
  else if ((button3State == LOW) && (digitalRead(relay3Pin) == HIGH)) {
    // turn relay off
    digitalWrite(relay3Pin, LOW); 
  }  

}

Step 4: Demonstration Code 2

You can find this Demonstration Code in the RobotGeek Libraries and Tools under:

RobotGeekSketches -> Demos -> Solenoid -> delayControlSolenoid -> delayControlSolenoid.ino

This code activates each relay for a different amount of time. This is the most common way to create a latch or locking mechanism for doors and containers, as it gives the user a set amount of time to react to the latch being unlocked, and then returns to locked position. Load this code to your arduino with the power to the solenoids unplugged. During the upload, the data transfer can sometimes cause the relays to fire, which can be dangerous if the power to the solenoid is connected. Once the code is uploaded, connect the 6V power supply to the arduino and the 12V power supply to the barrel jacks between the relays and the solenoids.

/*

Controlling a Solenoid with Arduino

This demo shows how to control a solenoid using pushbuttons and a relay with
your Arduino compatable controller.
 
 
 The circuit:
 * RobotGeek Pushbutton - Digital Pin 2
 * RobotGeek Pushbutton - Digital Pin 4
 * RobotGeek Pushbutton - Digital Pin 7
 * RobotGeek Relay - Digital Pin 8
 * RobotGeek Relay - Digital Pin 12
 * RobotGeek Relay - Digital Pin 13
 
Products Used in this demo:
 - http://www.robotgeek.com/solenoids
 - http://www.robotgeek.com/robotgeek-geekduino-sensor-kit
 - http://www.robotgeek.com/robotGeek-pushbutton
 - http://www.robotgeek.com/robotgeek-relay

 */

// constants won't change. They're used here to set pin numbers:
const int button1Pin = 2;     // the number of the pushbutton1 pin
const int button2Pin = 4;     // the number of the pushbutton2 pin
const int button3Pin = 7;     // the number of the pushbutton3 pin
const int relay1Pin =  8;      // the number of the Relay1 pin
const int relay2Pin =  12;      // the number of the Relay2 pin
const int relay3Pin =  13;      // the number of the Relay3 pin

// variables will change:
int button1State = 0;         // variable for reading the pushbutton status
int button2State = 0;         // variable for reading the pushbutton status
int button3State = 0;         // variable for reading the pushbutton status

void setup() { 
  
  // initialize the pushbutton pin as an input:
  pinMode(button1Pin, INPUT);     
  pinMode(button2Pin, INPUT);     
  pinMode(button3Pin, INPUT);    
  // initialize the relay pin as an output:
  pinMode(relay1Pin, OUTPUT);    
  pinMode(relay2Pin, OUTPUT);    
  pinMode(relay3Pin, OUTPUT);    
   
}

void loop(){
  
  // read the state of the pushbutton values:
  button1State = digitalRead(button1Pin);
  button2State = digitalRead(button2Pin);
  button3State = digitalRead(button3Pin);

  // For the first button, we just activate the solenoid/relay for two seconds
  if (button1State == HIGH) {     
    // turn relay on   
    digitalWrite(relay1Pin, HIGH); 
    delay(1000);    // waits for 1 second
    //turn relay off
    digitalWrite(relay1Pin, LOW); 
  } 

  // For the second button, we just activate the solenoid/relay for two seconds
  if (button2State == HIGH) {     
    // turn relay on   
    digitalWrite(relay2Pin, HIGH); 
    delay(2000);    // waits for 2 seconds
    //turn relay off
    digitalWrite(relay2Pin, LOW); 
  } 

  // For the second button, we just activate the solenoid/relay for two seconds
  if (button3State == HIGH) {     
    // turn relay on   
    digitalWrite(relay3Pin, HIGH); 
    delay(3000);    // waits for 3 seconds
    //turn relay off
    digitalWrite(relay3Pin, LOW); 
  } 

}

Step 5: So What Can You Do With a Solenoid?

The video here shows off the abilities of the large solenoid at different voltage levels. Keep this in mind when designing your project. The small and medium solenoids cannot handle 24V. Please also keep in mind that these solenoids are intended for intermittent use - leaving the solenoid on for extended periods will cause the coil to heat up and possibly damage itself. Do not overtighten bolts in the solenoid frame. While the solenoid is a sturdy device overall, bolts can damage the coil, leaving the solenoid useless. Make sure the bolts do not come into contact with the coil (coil is contained in the black fabric sheath within the metal solenoid frame).


We're huge fans of using solenoids in conjunction with RFID. We made a Lock Box kit that has everything you need to get started with that. There are also Solenoid Valves that work on the same principles as our regular solenoids that you can use in a project like the ShotBot to select a liquid source while utilizing a single pump. We hope this has been helpful, and we'd love to hear what you use your solenoids for!