Arduino crashes when controlling a solenoid

I'm testing this solenoid with this relay module by sending data from my laptop using Pure Data, to Arduino.
This is the code I wrote to test it

void setup() {
  pinMode(3, OUTPUT);
  digitalWrite(3, LOW);
  
  Serial.begin(9600);
}

void loop() {
  while(Serial.available()){
    byte in_byte = Serial.read();
    if(in_byte == '0'){
      digitalWrite(3, HIGH);
      delay(100);
      digitalWrite(3, LOW);
    }
    else if(in_byte == '1'){
      digitalWrite(3, HIGH);
      delay(10);
      digitalWrite(3, LOW);
    }
  }
}

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?

You need to make a pencil drawing of all of the wiring connections and post a photo of the drawing.

I suggest you do not test with the solenoid connected until you get more advice in case something is damaged.

Does the code work properly if the solenoid is not connected ?

...R

The code works fine with Arduino's integrated LED on pin 13.
Here's a drawing of the circuit, hope it's clear enough...
http://drymonitis.spgl.cc/solenoid_circuit.png

Cheers

So you are not using twisted pair to the supply or solenoid?

You have not shown how the Arduino is powered.

...R

I've no idea what's a "twisted pair to the supply", can you elaborate?
The Arduino is powered via USB from my laptop.

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.

Read up about star-grounding and use it.

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?

thanks!

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.

MarkT:
A handdrill or variable-speed electric drill and a vice can be used to twist wires together as required.

Or your hands (works for me every time.)

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.

cheers

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.

Regards.