Arduino autoreset ?!

Hello I experience a very bad issue..
I Have a compatible arduino uno, but I see that when I switch off a relay, it seems to autoreset..
Often with a crash of the serial monitor or IDE
In that moment the L led blinks a bit.. then It start again it cycle..

How can I catch the trouble?
Am I missing something?
I have a simply dht22 temp sensor in the 7 pin and the relay connected to the pin 8 (but I tried also other pins).

I'm using an optocoupled relay and separated voltage

Post a schematic.

Post your sketch. Please use
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags.

this is a very simple sketch...
when temp go under 24 degree the relay switch on..
when it reach 25 it switch off..
(high pin means off in my relay board)

sometimes seems that is reset by itself.. others it freezes serial monitor..

#include <SPI.h>
#include <DHT22.h>

/*---------------------------------------------*/
/*        DHT22 sensore temperatura             */
/*  serve una res da 4k7 tra VCC e il pin DATA  */
/*----------------------------------------------*/
#define DHT22_PIN 7     // il pin che uso per il sensore 
DHT22 myDHT22(DHT22_PIN);
float temp;             // temperatura
String TempMsgErr;      // messaggio in caso di errore 
float tempmin = 24.0;   // temperatura di accensione
float tempmax = 25.0;   // raggiunta questa temp spengo il sensore

int relay1_pin = 3;         // pin relay 1
boolean relay1 = false;     // stato relay 1

/*--------------------------------------------------------------------*/
void setup(){

  pinMode(relay1_pin, OUTPUT);
  digitalWrite(relay1_pin, HIGH);
  Serial.begin(9600);
  delay(2000);
}

void loop(){
  /*-------------------------------------------------------------*/
  /* Sezione che comanda i relè quando lo stato è in automatico  */
  /* lettura temp e umidità                                      */
  /* 2s è il tempo minimo di lettura del sensore                 */
  /*-------------------------------------------------------------*/

  DHT22_ERROR_t errorCode;
  TempMsgErr = "";

  errorCode = myDHT22.readData();
  switch(errorCode)
  {
  case DHT_ERROR_NONE:
    temp = myDHT22.getTemperatureC();
    // hum = myDHT22.getHumidity();
    break;
  case DHT_ERROR_CHECKSUM:
    temp = myDHT22.getTemperatureC();
    //  hum = myDHT22.getHumidity();
    TempMsgErr = "Errore di parità";
    break;
  case DHT_BUS_HUNG:
    TempMsgErr = "BUS occupato";
    break;
  case DHT_ERROR_NOT_PRESENT:
    TempMsgErr = "DHT non rilevato";
    break;
  case DHT_ERROR_ACK_TOO_LONG:
    TempMsgErr = "ACK timeout";
    break;
  case DHT_ERROR_SYNC_TIMEOUT:
    TempMsgErr = "Sync timeout";
    break;
  case DHT_ERROR_DATA_TIMEOUT:
    TempMsgErr = "Data timeout";
    break;
  case DHT_ERROR_TOOQUICK:
    TempMsgErr = "Lettura troppo veloce";
    break;
  }

  /*--------- gestisco i reley in base alla temperatura ------------*/

  if ((TempMsgErr != "") && (relay1 == true))                         //se il dato sulla temp è errato e il relè è ON lo spengo
  {
    digitalWrite(relay1_pin, HIGH);
    relay1 = false;
  } 
  if ((TempMsgErr == "") && (temp < tempmin) && (relay1 == false))     //temperatura bassa. accendo relè
  {
    digitalWrite(relay1_pin, LOW); 
    relay1 = true;      
  }
  if ((TempMsgErr == "") && (temp >= tempmax) && (relay1 == true))  //temp alta. spengo il relè
  {
    digitalWrite(relay1_pin, HIGH);                                
    relay1 = false;    
  } 

  Serial.print("Pin n. ");    // stampo lo stato dei relay
  Serial.print(relay1_pin);
  Serial.print(" status = ");
  if (relay1) Serial.print("ON ");
  else Serial.print("OFF ");
  Serial.println(temp);
  delay(2500);
} // fine loop

Have you got a snubber diode on the relay coil?

yes, of course, I have a similar relay board http://www.bajdi.com/wp-content/uploads/2013/02/relay-board.jpg
optocoupled, I tried in ground isolation and shared ground between relay board and arduino.. same result..
the board get crazy.

Serial output?

Still no solution.. Ok, I tried another arduino board! same result... (Damn) :zipper_mouth_face:
The serial monitor freezes and the board reset itself after relay switching off.
The code is obvious, I could experience the same problem either with the led example sketch using the pin 8 as out

What's happening?
Ok, the last doubt is the relay board now.. I've two identical relay boards too, but same result.
The last thing is that the relay board switch on when pin is low.

But I doesn't make sense..
I'm gonna build a relay board by myself, no more solution.. Never experienced a baddest issue...

How are the arduino and whatever the relay is switching powered? Does it make a difference if you power the arduino from batteries?

same story with both 9v battery and separate powering sources, in arduino and relay board.
separated ground and relay board with optoisolation. Is there anyone with a similar relay board as mine with switching on at low pin?
I already linked a picture in a previsious post.
2 arduino boards tried. So I can exclude arduino boards. I tried also either with and without ethernet shield.
I guess that this issue is sistematic and can be repeated by everyone..
just try to connect a relay and try led sketch..
I repeat.. My last doubt is the relay board type (because I had the same issue with 2 similar relay board too).

I tried also to power the relay board with a 7805.. same result.
Also with fast diodes, same result.
Also with a 220 uf cap.

Are there quite elements to open an hardware issue?

I suppose it must be some sort of inductive issue, but I can't see what is causing it. The picture shows a jumper with two positions and 'vcc' printed next to it. Do you know what this does? If it gives the option to connect the relay board's logic supply to the Arduino 5v supply then in the wrong position it might still be coupling the two supplies together, even when you think you have the Arduino powered separately.

Connections are separated.. my multimeter doesn't lie :wink: (in both switch positions)

Yes, the jd-vcc bridge let you using the relay in 2 way: optoisolated and shared vcc.
If you want to use the shared vcc, just keep the bridge on the board, and connect relay as usual to the arduino vcc and ground.

If you want to run it optoisolated, you pick out the bridge, and you connect JD connector to a separate 5v power source and ground. Then you connect 5v arduino to vcc relay board.
See this scheme: http://arduino-info.wikispaces.com/RelayIsolation

I found at least 3-4 similar recent threads here, but no solution..
I wonder if there is anyone that can use the relay without troubles..
but the fact is, that maybe lots of people doesn't "see" the problem, because, the code after few seconds (10-30) seems to run again (sort of "autoreset"), so they can see it, only if they use serial monitor or webserver to pilote the relay.

This night I made other tries, and I found that when I turn off relay (HIGH pin for my relay board) the arduino seems to reset,
blinking sometimes the L LED, but sometimes, in random way, the serial monitor continues to work for a few cycles, and either if the led blinks, the millis() at every second continues its increments displayed. So, it's not a real reset.. It seems something happening different..

I'd really ask if is there anyone could repeat my issue.
It's sistematic to me..
and I repeat, my last doubt is this kind or relay board with LOW pin on.. but it makes no sence to me..
I'm really discouraged..

Does the relay board distance from arduino affects in some way the arduino?

I think I found some similar issue here in the forum.. Just a little search, but I think I can find more
http://arduino.cc/forum/index.php/topic,155014.0.html
http://arduino.cc/forum/index.php/topic,160006.0.html
http://arduino.cc/forum/index.php/topic,161355.0.html
http://arduino.cc/forum/index.php/topic,161287.0.html
http://arduino.cc/forum/index.php/topic,138322.0.html

The reset is sensitive for spikes. I have this problem now and then.
But there is not a single solution for this.
You could add a capacitor of at least 1000uF to the 5V and GND and/or to the power supply input.
You should take a good look at grounding. If a relay switches, is the current of the relay going through wires that could lift the ground of the Arduino? Or is that current going through the Arduino board ? Perhaps using 10 ohm between grounds, or a ferrite coil spike suppressor.

The Arduino is not shielded, and the inputs and outputs are not protected. That is why this happens. But if the Arduino was fully protected, a wrong grounding would still cause this.

You could use a small capacitor of 1nF to the RESET pin and GND.

Thanks Erdin for your help. I already added a 220uf cap between arduino vcc and ground. I can try a bigger one..

I already tried to round a ferrite on the control wire. but I guess, I could use it at the vcc wire (but nosense, because the totally isolation).
I already made a star ground for arduino pins, but a cap between reset and gnd could be helpful..
I'll try soon..

Here I am again..

I made lots of other tries. Probably I found the main trouble, but, I still cannot resolve at all.

I went back to normal relay connection, with shared ground (but it doesn't matter).
I'm running now with a simple dht22 temp sensor and relay board (no ethernet, no ds1307 clock, no SD)..
Just relay switched at temp reached.

Finally I twisted and shielded the 3 wires (+, gnd, ctrl) between arduino and relay board with an aluminium foil, and I found that the arduino seems to not reset itself anymore.

However, I still have the serial monitor freezing issue at relay switching off.

I have a very short and well shielded usb cable to pc, but it seems to still have problems.

I'm gonna test now an arduino mega with ethernet shield!! It's a bet now!

That shielding helps ? That makes it even more weird.
I have seen the other posts with relay board problems.
If you only have to drive an optocoupler, this is impossible.
Perhaps the relay is causing a spike that fires back.

The reset is connected to the DTR signal of the serial-via-usb.
Perhaps some spike between the relay and PC will cause it.

Can you do a few tests ?

Is the board resetting only when deactivating the relay ? (diode is not soldered, or too slow).
Is the board resetting if the usb of the Arduino is not connected ? (could be grounding problem)
Could you add a capacitor at the 5V on the optocoupler side and at the relay side (also 5V I guess). I don't see any capacitor in the photo. Is that true ? If so, try first 100nF and add a larger capacitor to it if needed.
Could you add a resistor 10 or 47ohm at the Arduino in the signal wire ?

I think I know where this is going. The shielding you did, is not only shielding, but adds a capacitance. The relay board seems to be not decoupled. That would be very bad design. Even two traces next to each other would be enough to fire back the relay coil spike. I also don't trust those flyback diodes.

Ok, I'll keep all your ideas as gold.
For sure, I already can say that the issue happens ONLY when I switch off the relay ( HIGH state in this board).
And I already try to add a NP 100nf cap in the JD power side of optocoupled (the external power source in the relay board), but it didn't solve..
And if i remember well, I experience the same also when the pc usb is disconnected.. It seems to me that I already tried this (while monitoring the data trough the webserver untill the relay switch off and it get weird).

I still have to try to add a cap between the reset pin and gnd..
I have another doubt. I want to disconnect main power source from pc, and keep only battery (I'm sure it won't help, but I want to try)

Also.. I'm not sure, but the relay board should already have a res to the control wire, I cannot follow well the path, but I guess one res is for the led and the other one for the signal wire.
The relay board is optocoupled, as I can see the bc817 optoisolator. I also tried some time ago to exchange the diode (seemed similar to 1n914/4148) with a 1n4007.

But the relay board now is the main accused for me.
Tomorrow I'll get some npn transistor, and I'll try an homemade relay board too.
Now It' harder than me.. I wanna discover the trouble..

The 1N4007 is slow, and the 1N4148 is very fast. For small currents always use the 1N4148.

We can say for sure that one problem is the release of the relais.
If the pcb copper traces are badly designed, it will be hard to get rid of the spike.
Traces next to each other are inductive coupled.

Perhaps slowing down the ramp is an option.
A coil in the signal line and a capacitor from the signal line to ground.

The reset if you disconnect the PC is another problem.
The pull-up resistor for the reset is 10k, that is a high value.
You could add an extra 10k from the Reset pin to 5V (and 1nF from Reset pin to GND).
It might even depend on the computer how the DTR signal is.

The reset if you disconnect the PC is another problem.

ops, sorry, maybe I explained bad.. I mean when the "pc is not connected to the board" at all, not when I disconnect the cable.

A coil in the signal line and a capacitor from the signal line to ground.

coil in the signal? you mean a ferrite around the control wire?
cap from the signal to ground? how? didn't get this..
Thanks

I ment a low-pass filter with an small ferrite inductor and a capacitor to smooth the ramp of the signal.
For example near the Arduino. Arduino output to ferrite inductor, from inductor a capacitor to ground, and that into the wire.

Perhaps a resistor of 10 ... 100 ohm and a capacitor of 1nF is better, but I don't know if that will influence the current for the optocoupler led.

The low-pass filter should lower the spike by the coil of the relay when swithed off.