Code for controlling a solenoid valve

Morning everyone

I'm relatively new to arduino, but have the starter kit and have successfully built most of the projects. I'm working on a project with a mate where we need precise control of the opening time of a solenoid valve in a compressed air set-up. The opening time needs to be somewhere between 200 and 1200 milliseconds. The help I need is with the code. I was hoping to adapt code from an existing project but can't find one in the starter projects that seems suitable. Any help would be greatly appreciated. I feel it's the kind of thing that would have been done already?
Thanks in advance
Tony

Have you looked at the BlinkWithoutDelay example in the IDE ?

It turns on an LED for a period but the important thing is that it changes the state of a pin for that period. To control a solenoid you will need an external power supply and a means of interfacing the Arduino pin with the solenoid

What will trigger the opening of the valve ?

Unless its a special kind of solenoid, the pull in time will be determined by the solenoid and power supply.

200mS seems pretty slow, 1.2 seconds is positively glacial !
Maybe it's not a solenoid per-se ?

Do you have a link to the solenoid datasheet?

Is that the time that the valve needs to be open, which is how I read it ?

Thanks for your swift replies guys. My engineer friend is researching the mechanical side of things so we haven't got the solenoid yet. I'll have a look at blinkwithoutdelay......but yes we're looking to create a precisely timed discharge of air. We're experimenting with varying pressure and discharge time combinations. We think around a second will be long enough but higher pressure and lower discharge time may be advantageous. Cheers Tony

1 Like

Hello tonymuso
Who or what is guiding this time frame?
Have a nice day and enjoy coding in C++.

I was assuming that the code will control the time with the output from the Arduino connected to the input of a relay which will drive the solenoid. I'm assuming in this that the 5V output mightn't be high enough otherwise?

Morning guys! I had some fun last night and used the Blink program to achieve what I wanted...... very easy to adjust the on and off time. I connected pin 13 to the breadboard and tested with a 220 ohm resistor and an LED. My engineer mate asked me about maximum current available from the Uno board. Am I right in saying it's 40mA from pin 13 but 400mA from the 5V output pin? Is there a way to use the Blink program to control the 5V output pin and get more current in case we need it?

You can use the external power supply via a relay, transistor, or a motor driver such as L298N.
This Arduno - Solenoid Valve may be useful for you

Thanks for that. I've only ever used my arduino with usb power so don't have a power supply for it. Are you saying the transistor or motor driver could be driven from pin 13 then?

The reason he told you to include transistors is because Arduino pins do not have the ability to supply/source that much current for the valve (or really anything more than a LED). So you will need to use the pins to drive a transistor to supply power to the valve.

So you should connect the gate (of the transistor/FET) to pin 13 (or any other non-special pin), and the source/drain to the valve and power source (do NOT source power from Arduino board, wire separately).

Depending on the valve you used, you don't necessarily need a relay, since you need a decent amount of current to energize the coil (which is probably more than what Arduino pins can supply so you need also a transistor).

Hi,
Can you please post a link to spec/data of the solenoid valve?
You will need to add a Back EMF protection diode to the solenoid also, if it is just a coil with no attached electronics itself.

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

Thanks for your comments Tom. I understand exactly what you mean now. I've been playing with the irf 520 MOSFET in the starter kit. My engineer mate is researching and buying the solenoid valve this week......will let you know the spec when we know. Cheers

More correctly, do not attempt to source power from an Arduino board! :astonished:

Well, that is cute, but since that FET is not a logic-level device, if you have a solenoid requiring more than 1 Amp, you should go looking for a logic-level FET. :thinking:

But you really(! :grin:) do not want to use a relay.

Thanks Paul..... that's really interesting because I have another more "heavy duty" motor at home and although it would spin I couldn't get it to draw more than 1A. I looked at the IRF520 spec and it quotes a drain current of 9.2A and pulsed current of 37A!

...... I just saw this is based on a 10V input!

that's the max current the FET is capable of delivering without blowing up. You want the current consumption (of whatever you hooked up to it) to be below this value.

WHERE IS YOUR DATASHEET FOR THE SOLENOID VALVE?!

I found this 5V one from Walmart. I prefer 5V ones because assuming you have a good 5V source you don't need to worry about the possibility of smoking your arduino board (whose max tolerance is 5V)

To open it, supply positive 5V to it (for 1 second), and then it will be open indefinitely, until you supply negative 5V to it (flip polarity), then it will be closed indefinitely until you supply positive 5V to it. Remember this is a "pulse solenoid", so it doesn't like to be powered for too long.

Hi,
If you are possibly getting a solenoid with 0 - 10V or 4 - 20mA analog control, then you will not need a MOSFET to provide the solenoid current.

It will have its own control circuitry, you will still need to provide drive current from another supply, but your control signal form the controller will possibly need some interfacing.

But we await the solenoid that you will be provided with to make final suggestions.

Tom... :grinning: :+1: :coffee: :australia:

Exactly. It is not a logic level device. :astonished:

Try to avoid pin13 (assuming Arduino Uno/Mega/Nano/etc).

Pin13 is also used by the bootloader, and could have unexpected (blinking) behaviour during bootup.
Leo..