But after firing the solenoid a few times, the whole thing crashes, both Arduino and Pure Data. Is there something wrong with the code (first time I'm experimenting with solenoids)?
I'm powering the relay with a 5V/3A power supply, is it bad?
With power wiring that's got noise on it (as here), you normally used twisted pair (the two
wires tightly twisted together) to reduce the EMI broadcast from the wiring. A handdrill
or variable-speed electric drill and a vice can be used to twist wires together as required.
The twist minimises the area enclosed by the varying current (inductance) and causes
most of the fields to cancel out completely at a distance. This helps reduce noise/spike
emissions from the power wiring into other more sensitive circuits. Its important with
high currents never have to go round a large loop of wiring because that will generate magnetic
interference to everything nearby.
If you can't use twisted wires make sure the loop area is minimised - always run supply
and return together. Keep other sensitive circuits away from the power wiring as
much as possible.
Thanks, I'll take a look at it.
I didn't twist the wires, instead I used alligator clips with wire to connect the power supply to the relay and the solenoid. Is the circuit drawing I posted above correct? Or should wire things in a different way?
Your diagram did not show a diode connected across the solenoid. Besides being hard on your relay contacts, the solenoid can generate enough back EMF to crash the Arduino.
Connect a diode (1N4004), etc. with Cathode going to the PLUS side of the coil and the ANODE to the negative side. Even if your solenoid draws 2A, the 1N4004 only has to handle it for a very short time so it should be sized OK.
rmetzner49:
Your diagram did not show a diode connected across the solenoid. Besides being hard on your relay contacts, the solenoid can generate enough back EMF to crash the Arduino.
Connect a diode (1N4004), etc. with Cathode going to the PLUS side of the coil and the ANODE to the negative side. Even if your solenoid draws 2A, the 1N4004 only has to handle it for a very short time so it should be sized OK.
Been away from the thread for some time. I'll give that a try, thanks for pointing it out.
I think the only difference in each if statement is the duration of open time of solenoid, which seems very short to me. If it is a normal cheap relay that sold on ebay, as far as i experienced, they do not have that fast response times. If it is a SSR, i have no experience that, so my comment may be void for SSR.