Controlling a Solenoid with an Arduino

This is a placeholder topic for “Controlling a Solenoid with an Arduino” comments.




We covered a whole bunch of rotational motion when we looked at DC motors. It’s only fair to give linear motion the same treatment.
Electric solenoids work on similar electromagnetic principles to those of DC motors, however solenoids can us…

Read more

Can you help me with the same setup on 12v solenoid valve instead of 5v solenoid?

Hi Soundar,

The setup will be pretty similar, just provide 12v power to the transistor, and use the signal from the Arduino to “close” it!

1 Like

Hello,

I’m extremely new to all this and just followed the tutorial. When powering the Arduino Uno via the usb power adapter (5V 2A), the solenoid doesn’t move at all. If I push it the solenoid stays in place and doesn’t bounce back. It’s a mini push pull solenoid with a 2pin jst connector, similar to the one in the tutorial. In fact I’m not even sure how to connect it to the breadboard properly, do I just use jumper cables?

The only things that differ from the tutorial is the diode, 1N4007 instead of 1N4001, and the transistor which is a BC547.

Apologies if there’s something really obvious missing here, I’m feeling really lost…

Hi Vincent,

The emitter and collector on the transistor you are using are on the opposite sides as the transistor in the tutorial. It will not work if wired up the same. Here is the BC547 pinout.

You mentioned that your solenoid is similar, is it a 5v solenoid? Does your solenoid move when connected directly to 5V?

Hi Stephen,

I believe it’s the exact same one as the adafruit mini push pull 5v solenoid (ID 2776), which looks the same as the solenoid in your tutorial.

Oh I did not know that the transistor pins were reversed, did not notice that! I think I got the hang of it now, thank you very much for the help!

1 Like

I want to use a similar setup to this one but I only want the solenoid to fire at a certain point after my arduino already preformed some other steps. Would I have to chnage anything about the setup or would it still work like this. The way i would like to activate the solenoid is by having a steppermotor reach a certain stepcount.
Thanks in advance.

Hi Mick,

The setup would be the same the only thing you would need to change is the conditions in the code that trigger the solenoid to fire.

Hey, does the power transistor in the circuit act as a voltage regulator, or should I try to find a voltage regulator to work alongside it.

I’m trying to use the circuit in a portable setting and would need to run it on batteries and wasn’t sure what was the best way to go about doing this.

The transistor does not operate as a voltage regulator. It provides the high current needed to operate the solenoid. The GPIO pins only provide current in the 10 to 20 mA range, not enough to operate the solenoid.

A voltage regulator would be required for the microcontroller if running of batteries.

2 Likes

What kind of transistor did you use? I’m very new to this, but it seems as though I’m not using the correct transistor as I cannot control the solenoid at all. Thank you!

I was not the one to write this article though it appears that it was done with an NPN transistor. If you would like to upload a diagram of how you have wired it up we should be able to get you back on track.

Could you kindly explain the function of the transistor and diode in this circuit. Also could you mention what kind of transistor and diodes are used? my other question is what are the ways to control the discharging of a solenoid? as in if i want the solenoid to discharge at a particular voltage can I do that?

Hey @divya101044,

As Robin said above

The diode used to essentially let the current flow when the transistor is off. This is because the solenoid is an inductor and the voltage across an inductor is equal to the inductance x der(i). Therefore, giving the current somewhere to flow will reduce voltage spikes. Aidan has the specific components used under “The Project” section of the tutorial.

I hope this helps.

@divya101044

If you are just experimenting, I would recommend buying a relay board. Most relay boards are already set up with the correct circuitry to drive the solenoid, and prevent damage to the microcontroller.

Otherwise you need to understand that the solenoid isn’t a storage device. It doesn’t charge or discharge. When current is flowing through the solenoid, it is acting in the same way as a motor. The current produces a magnetic field which moves the core. When the current is turned off, it operates like a generator. The collapsing magnetic field induces a voltage across the coil. This voltage can be much more than the original voltage. The flyback diode is there to limit this voltage.

Is it possible to control multiple elements with different timings using one Arduino controller? I need to alternately trigger two solenoid air valves at 5 second intervals while also triggering a latch solenoid at one second intervals. No synchronicity is required between the different timings, but I would like to perform all actions with one control mechanism if possible.

Hey Tim,

The problem would be that the Arduino is only capable of running one script at any given time. However with some clever scripting we can create a ‘clock signal’ each second and then every time it reaches the fifth second switch the solenoid valve. It would likely look something like this.

void setup(){
}

void Functiontotriggervalves() {
//Script to switch the valves
}

void Functiontotriggerlatch(){
//Script to trigger the latch
}

int t = 0;

void loop() {
delay(1000); //Wait one second
if (t==5){
Functiontotriggervalves(); //On the fifth second
t=0;
}
Functiontotriggerlatch();
t+=1;
}

Make sure to let us know how you go!

Bryce
Core Electronics | Support

1 Like

p.s. Sorry about the tabulations, copied this directly from the IDE.

This topic really caught my eye as I’m into model railways and using solenoids is 1 method used to control the movement of a turnout. The solenoids used in model railways are doubles, move one way to allow the train to go straight and the other way to allow the train to go through the diverging line. In this case, the solenoid has 3 wires, left and right to go to their own source of power and the common to ground. And in my case, I’d like to include LEDS to indicate which direction the turnout is set. That requires a continuous supply of power. But the movement of solenoids is generally controlled by momentary switches. Is this not the case here?

Hello David,

Indirectly, the Arduino is running a script which is controlling the flow of power to the solenoid according to the logic within a program. Luckily Arduino is also brilliant for controlling all kinds of LED, my recommendation for the project would be to use an RGB LED and an Arduino with an appropriate relay board so you can control the power to the solenoid and send appropriate signals to the LED. I’ve linked some products to take a look at below, have a great day!

Note, you’ll also need some jumpers and a power supply. Have a great day!

Bryce
Core Electronics | support