Freezing problems when power switching.

Hi mates,

I have been working on a medium power switching application for the last few months. The evolution is not too bad but I need your help very much.
I have to control a few DC coils and AC coils (contactors) by Arduino. I use an original Arduino UNO board.
I am using MOSFET transistor to control the DC coils and solid state relay to control the AC coils. I have drawn the schematic!
The problem is that Arduino sometimes freezes. This happens randomly by activating outputs.

Here the list of the components I use:
Optoisolators: PC817.
MOSFET n-channel: IRF3710.
MOSFET p-channel: IRF9532.
Solid State Relay: Input. 3-32 VDC | Output. 90-480 VAC. 8A. (http://www.ebay.com/itm/1PCS-ZG3M-308B-Solid-State-Relay-8A-Output-90-480VAC-/271159416771?pt=LH_DefaultDomain_0&hash=item3f225c57c3).
SL1, SL2, SL3: 36 VDC coils. 500mA.
K1, K2: 230 VAC contactors.

And here the Arduino code:

#include <Wire.h>
#include <i2ckeypad.h>
#include <LiquidCrystal_I2C.h>

const byte defNum[6] = {7, 6, 5, 4, 3, 2}, Q2=0, Q4=1, Q6=2, Q8=3, SSR1=4, SSR2=5;
byte state=0;

i2ckeypad keypad = i2ckeypad(0x3F, 4, 4);
LiquidCrystal_I2C lcd(0x20, 16, 2);

void setup() {
  
  Serial.begin(9600);
  Wire.begin(); keypad.init(); lcd.init(); lcd.backlight(); lcd.home();
  for(int i=0; i<6; i++) {pinMode(defNum[i], OUTPUT); digitalWrite(defNum[i], false);} lcd.print(state, BIN);
  
}

void loop() {
  
  char key = keypad.get_key();
  
  if(key != '\0') {
    switch(key) {
      case '1': update(Q2); break;  case '2': update(Q4); break;  case '3': update(Q6); break;
      case '4': update(Q8); break;  case '5': update(SSR1); break;  case '6': update(SSR2); break;
    } 
  }
  
}

void update(byte n) {
  
  digitalWrite(defNum[n], bitRead(state, n));
  bitWrite(state, n, !bitRead(state, n));
  lcd.home(); lcd.print(state, BIN);
  
}

Note.- Although the control of SL1 and SL2 may seems stupid (mosfet at the top for each), it must be in this way because of hardware limitations.

Hope you all can help me. Thanks.

K1 and K2, coils have no protection diodes.
May need several capacitors added.

jackwp:
K1 and K2, coils have no protection diodes.

But K1 and K2 are AC coils... A diode!?

jackwp:
May need several capacitors added.

Maybe.

Can K1 be switched on/off, only during zero crossing voltage, or may it turn off, when the AC is at its peak. 230V ac RMS, will peak much higher (maybe 300V ?).

Would you show us a camera picture of the setup.

LarryD:
Would you show us a camera picture of the setup.

For sure!

Disregard my diode for k1/k2. I realize I was way off base. Sorry for the confusion.

It may be noise pick up in your wires.

It happens when a solenoid operates?
It happens when a SSR operates?
It happens when there is a load on the SSR?

LarryD:
It may be noise pick up in your wires.

It happens when a solenoid operates?
It happens when a SSR operates?
It happens when there is a load on the SSR?

Noise through wires... mmm. I am usually very careful about noise. For this reason, I have optoisolated everything. If the noise can still reach the Arduino, what more to beat it!?

Ok, when there is nothing connected to the transistor/SSRs (solenoides and contactors on DB25) = no problems at all.
Problems only come when loads are trying to be controlled by transistors/SSRs (like on schematic). It's rarely Arduino fails when a solenoide (DC) is switching. It usually fails more often when a contactor (AC) is switching.
I do know AC coils usually give quite problems about noise. For this reason I am using SSRs just for this issue.

Maybe the noise is coming, not from the SSR, but from whatever heavy load you are powering. What is being driven by the SSRs ? May try disconnecting them, and leave the SSR in ckt, to see.

jackwp:
Maybe the noise is coming, not from the SSR, but from whatever heavy load you are powering. What is being driven by the SSRs ? May try disconnecting them, and leave the SSR in ckt, to see.

A contactor (see image attached) is being driven by each SSR. The contactor's coils are K1 and K2 on the schematic.
Sorry, what does "ckt" mean? (I am not a native English speaker).

If you will tell me what contactor is/does, I will tell you what ckt is. LOL, just kidding, I will tell.
ckt is short for circuit (at least it is for me, may not be for everyone).

Are the contactors using a lot of power, or are they like relays, and something else is using the huge power?

jackwp:
If you will tell me what contactor is/does, I will tell you what ckt is. LOL, just kidding, I will tell.
ckt is short for circuit (at least it is for me, may not be for everyone).

Hehehe, now I know some more! Thanks.

jackwp:
Are the contactors using a lot of power, or are they like relays, and something else is using the huge power?

The contactors coils are not using a lot of power. Contactors are like relays but for industrial applications, in this case they are switching a 3-phase motor of 1HP.
Arduino -> optoisolator -> SSR -> contactor -> motor. Arduino is quite far away from the motor. For this, it's difficult to believe by me that Arduino can pick up noise, but I don't discard anything.

Now I know some more to. This forum is so great!
How about putting the led's in place of the SSRs. That may tell us a lot.

jackwp:
How about putting the led's in place of the SSRs. That may tell us a lot.

Arduino works very well by itself. I mean the problem comes certainly from the circuit I try to control.
If I disconnect the contactor, the problem goes away.
The difficult thing is where is the problem and how to solve it. Because I have used lots of methods to avoid catching noise, it's even optoisolated!

Ok, got you. How about hooking a small load (light bulb etc) to the contactors. What difference if any then?

Oh, unfortunately it's difficult to do that, because I can only handle the coil's pins. They are built in an old machine which I have to control to.

albuino:

LarryD:
Would you show us a camera picture of the setup.

For sure!

One thing I can see that is wrong is that you are routing the SSR input and output wires close to each other - even in the same bundle. Don't do that. Keep the SSR input wires well away from the output wires. If you are using a connector to connect the board with the SSRs to everything else, you should use separate connectors for the inputs and outputs. This is good practice from a safety/isolation point of view as well as from a noise point of view.

Having said that, I suspect the problem may be due to the high currents drawn by the motors, that you are interrupting with the contactors. How far away are the contactors from the Arduino?

Ok, another thought. Maybe the motor is feeding/loading the ac line, and causing the arduino a power glitch.
Try to isolate it more. How are you powering the arduino now? Are you using Vin with 12V, or what?
We are getting closer I think.

dc42:
Keep the SSR input wires well away from the output wires.

It is true I can do more to get them more separated. I have 2 connectors from the SSRs board to the main board, however wires cross each other together on the air. I don't really understand how some electrical signal can drive through the plastic tube of the wire. But I know it happens.

dc42:
Having said that, I suspect the problem may be due to the high currents drawn by the motors, that you are interrupting with the contactors. How far away are the contactors from the Arduino?

Yes, the motor currents are quite high since it is 1HP motor. The contactors are located very far away from Arduino (2 meters).

jackwp:
Try to isolate it more. How are you powering the arduino now? Are you using Vin with 12V, or what?

I usually use the USB connector to feed the Arduino. It's very comfortable because I can use the serial monitor at the same time if I want.