Introduction: Control a Solenoid With Arduino
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.
- 1 x Small Solenoid
- 1 x Medium Solenoid
- 1 x Large Solenoid
- 1 x RobotGeek Geekduino Sensor Shield Kit
- 3 x RobotGeek Pushbutton
- 3 x RobotGeek Relay
- 1 x 6V2A DC Power Supply
- 1 x 12V10A DC Power Supply
- 1 x DC Squid Cable
- 1 x RobotGeek Large Workbench
- 3 x Barrel Jack Female Pigtail Lead
- Wire Nut, Electrical tape or solder
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!
10 Comments
5 years ago
Can I use the same 6v power supplier for the solenoid valve instead of that huge 12v adapter?
6 years ago
im working on a RC car that can jump. for the jumping mechanism ive been looking at a few things, spring launcher, co2/compressed air but now solenoids have caught my interests. can i get solenoids powerful enough to lift a very like RC car off the ground fairly high. its going to be small hopefully lighter than 100 grams or more.
what are your thoughts on this. BTW awesome tutorial?
6 years ago
I'm having the hardest time with this and have no idea why. It's like my relay has a mind of its own. I've tried 2 different modules. With both codes I get the same result. The relay switches on and off without pressing the button at all and only works with the button randomly, about 25% of the time
Reply 6 years ago
Could you post a picture of your setup somewhere so we can see the wiring? It sounds like it's taking a floating value from a pin that isn't connected properly.
7 years ago
Very nice tutorial! The setup is simple and the comments in the code makes it easy to understand. I have read that solenoids (and general devices with coils, motors etc.) can damage the circuits when they are turned off. Because of a voltage drop or reverse current I think. A diode close to the solenoid is then used to protect the circuits. Is this applicable in your setup or will the relays act as protection?
7 years ago
Very nice job. I appreciate the effort gone into making this tutorial
Reply 7 years ago
Thank you! We're doing everything we can to demystify technology.
7 years ago
A good way to control the speed and force of a solenoid is to use a dashpot to regulate the speed of the stroke, this is what arc lamps use when the solenoid advances the rod to re- establish the burn, if it moves too fast the arc can be extinguished, the dashpot ensures reliable re- ignition.
Reply 7 years ago
This is useful information, and sound advice! Thank you for sharing.
Reply 7 years ago
You're welcome, this I found out many years ago when troubleshooting and arc light machine that wouldn't re- light reliably, the manufacturer explained the dashpot component and it's important function in the machine.