killing serial port with L289N, simple relay, 12v plus kills loop()

Greetings from Dallas and "huh?"

Using ELEGOO UNO, don't remember the brand of the L289N controller. Driving an off-the-shelf AutoZone relay to power a 70N electromagnet. Code below drives electromagnet 1 time through loop(), but as you can see from the output it stops the serial communication - there should be several additional text output lines. It also makes the port unrecognizable on my PC.

char pgmname[] = "solenoidtest1";
int i, j, k;
int working, holdingup, loopcount;
int solenoidmilliseconds = 5000;
byte greenLED = 8;
byte yellowLED = 12;
byte Lenable1and2 = 5;

void setpins(){
  pinMode(greenLED,OUTPUT);
  pinMode(yellowLED, OUTPUT);
  pinMode(Lenable1and2, OUTPUT);
  digitalWrite(Lenable1and2, LOW);
  } // end setpins()


void setup() {
  Serial.begin(9600);
  Serial.println(pgmname);
  setpins();
  working = 0;
  loopcount = 0;
}

void loop() {
  loopcount++;
  Serial.println(String(loopcount));
  error1:
  Serial.println("Enter milliseconds to hold");
  while (!Serial.available()){ ;}
  delay(50);
  while(Serial.available())
  {
    solenoidmilliseconds = Serial.parseInt();
    if(Serial.read() != '\n')
    {
      if(solenoidmilliseconds > 10000){
        working = 0;
        goto error1;
    }
    }  
  Serial.println("DOING:  solenoid() Green to hold for "+ String(solenoidmilliseconds));
  solenoid();
  delay(1000);
  Serial.println("DOING: solhold() then solrelease() Yellow");
  solhold();
  delay(solenoidmilliseconds);
  solrelease();
  }
  }  // end of loop()

void solenoid()
{
  Serial.print("START solenoid(),  ");
  digitalWrite(Lenable1and2, LOW);
  digitalWrite(greenLED, HIGH);
  holdingup = 1;
  digitalWrite(Lenable1and2, HIGH); //energize
  delay(solenoidmilliseconds);
  digitalWrite(Lenable1and2, LOW); //release
  holdingup = 0;
  digitalWrite(greenLED, LOW);
  Serial.print("DONE solenoid(),  ");
} // end solenoid

void solhold(){
  Serial.print("START solhold(),  ");
  digitalWrite(Lenable1and2, LOW);
  digitalWrite(yellowLED, HIGH);
  holdingup = 1;
  digitalWrite(Lenable1and2, HIGH); //energize
  Serial.print("DONE solhold(),  ");
}

void solrelease(){
  Serial.print("START solrelease(),  ");
  digitalWrite(Lenable1and2, LOW); //release
  holdingup = 0;
  digitalWrite(yellowLED, LOW);
  Serial.print("DONE solrelease(),  ");
}

The electromagnet holds and the LEDs light according to above code. The serial monitor displays:

solenoidtest1 1 Enter milliseconds to hold DOING: solenoid() Green to hold for 2000 START solenoid(),

I think the code makes sense, it is something about the circuit for the magnet. There are 2 accelerometers , a 12v brushless motor, a 12v circuit with 100' of cable for a pushbutton and 3 fairly large indicator lights and all that works fine when I pull the electromagnet out & remove the code for it. The code above intended to show this forum the problem in isolation.

Thanks for any comments, Chuck

Sounds like too much current through the pin. How much current does the coil on the relay use? Does it have an internal protection diode as well? Is the coil on the relay 5 volt?

the relay is connected to the OUT1 and OUT2 poles on the L289N board. There is no direct connection to any pins on the Arduino.

chuck659:
the relay is connected to the OUT1 and OUT2 poles on the L289N board. There is no direct connection to any pins on the Arduino.

What is Lenable1and2 connected to in this code which seems to be where the problem happens?

digitalWrite(Lenable1and2, LOW);

Hi, Does the code work OK with the relay disconnected?

May be Electromagnetic Interference. Some suggestions here:

http://arduino-info.wikispaces.com/Arduino-Project-Planning-Electrical

JustJohn - Lenable1and2 is connected to the "Enable 1 and 2" pin on the L289N, which pin allows the L289N OUT1 and OUT2 terminals to carry the 12v current

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Showing ALL your power connections and supplies.

Can you post a picture of your project so we can see your component layout?

Thanks.. Tom.. :)

TomGeorge: Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Showing ALL your power connections and supplies.

Can you post a picture of your project so we can see your component layout?

Thanks.. Tom.. :)

Best idea. Thanks Tom for jumping in.

If you disconnect the electro magnet dose it work right? You should be able to tell from the clicks.