Serial output freezes - solved by delay before firing relay

Couldn't find this documented elsewhere, so thought an note here might help (and perhaps bring a more refined explanation than my provisional diagnosis).

A simple relay-firing sketch worked fine. The relay (two coil latching 5v, protected by diodes on each coil, each driven via a darlington pair) would fire on and off under control of my Mega 2560 - it takes 40mA and with the odd LED that was still well within the limits of the board.

BUT - in a more complex application using the same circuitry, my (fairly verbose) debugging info written as Serial output would suddenly freeze - in the middle of a line. And it then needed a reset to get Serial output working again.

The answer seems to be that program control moves on to the next line after a Serial.print command without waiting for the line to complete writing: if it is a long line at a low baud rate, it may still be writing when a subsequent line of the program fires the relay (as was the case for me). And something in the relay firing (perhaps just the extra current drain, or could it be something more compled) seems to kill Serial output if that is happening at the same time.

Adding a 200ms delay before each relay operation worked to fix the problem for me. Can anyone offer a more authoritative explanation or a more refined delay value?

If you post your code.

How to use this forum

Data you write to the Serial port is buffered and transmitted subsequently via an interrupt - as long as the buffer is not full, the write()/print() function would return immediately without waiting for transmission to complete. You could call Serial.flush() to wait until the data had all been read from the buffer. Note that this would return when the last byte had been loaded into the serial UART, not when the transmission over the wire had completed. Also note that in earlier versions of the IDE, Serial.flush() did something completely different. I have no idea how likely it is that the serial output mechanism would be affected by the operation of the relay, but in the absence of any information it seems plausible and would fit the evidence.

Didn't see that code was really relevant, but in case it helps, here is code that freezes the Serial:

byte CONST_PIN_RELAY_ON=7;
byte CONST_PIN_RELAY_OFF=6;
  
byte CONST_MSECS_PULSE=20;
byte thisCounter=0; 
  
void pulseRelay(boolean pulseDirectionON){
	if (pulseDirectionON){
	  Serial.println(F("pulseRelay ON"));
	  digitalWrite(CONST_PIN_RELAY_ON, 1);   
	  delay(CONST_MSECS_PULSE);    
	  digitalWrite(CONST_PIN_RELAY_ON, 0); 
	} else {
	  Serial.println(F("pulseRelay OFF"));
	  digitalWrite(CONST_PIN_RELAY_OFF, 1);   
	  delay(CONST_MSECS_PULSE);    
	  digitalWrite(CONST_PIN_RELAY_OFF, 0); 
	}
}

void setup(){
	Serial.begin(9600);
	pinMode(CONST_PIN_RELAY_ON, OUTPUT);
	pinMode(CONST_PIN_RELAY_OFF, OUTPUT);
}

void loop() {
      Serial.print(F("\nLoop count="));
      Serial.println(thisCounter);
      Serial.println(F("1 Here is a long line which goes on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on"));
      pulseRelay(1);
      Serial.println(F("2 Here is a long line which goes on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on"));
      pulseRelay(0);
      Serial.println(F("3 Here is a long line which goes on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on"));
      thisCounter++;
}

I tested your code and it didn't freeze, even after 100 iterations. After that I got bored watching it. :slight_smile:

The answer seems to be that program control moves on to the next line after a Serial.print command without waiting for the line to complete writing:

Yes, it does exactly that.

if it is a long line at a low baud rate, it may still be writing when a subsequent line of the program fires the relay (as was the case for me). And something in the relay firing (perhaps just the extra current drain, or could it be something more compled) seems to kill Serial output if that is happening at the same time.

Could be, so you really have an electrical problem. You should fix it, rather than adding delays.

Nick - Thanks for running the test.

Can I be clear- was this just a test of the sketch (which runs fine for me too - as long as I don't fit a relay) or was it a physical test with a relay attached?

My hypothesis is that well before hitting the current limits of the board (tested so far only on a Mega 2560), a relay pulse can disrupt Serial output if that is still going on when the relay fires.

If my hypothesis is valid, then it sounds like a generic risk that others might benefit from knowing about. But it clearly could be something specific to my circuitry that has eluded me so far, so if you have run with a relay but without problems that would send me off to do more research.

John

I didn't attach a relay, because I would have no way of having your exact relay, and whatever transistor you used, etc.

Clearly you have an electrical issue.

A simple relay-firing sketch worked fine. The relay (two coil latching 5v, protected by diodes on each coil, each driven via a darlington pair) would fire on and off under control of my Mega 2560 - it takes 40mA and with the odd LED that was still well within the limits of the board.

40mA for a single pin is right at the limit - you're not exceeding the board's capacity, but you're not doing that pin any favours.

JohnGeddes:
The relay (two coil latching 5v, protected by diodes on each coil, each driven via a darlington pair) would fire on and off under control of my Mega 2560 - it takes 40mA and with the odd LED that was still well within the limits of the board.

Not "well within". Absolute maximum rating. Are you sure it isn't 41 mA?

To clarify: I am powering the relay coils from the Mega's own 5v - but each via a transistor, so the quoted 40mA for each coil isn't being taken from the Arduino pins.

But clearly, the power from the coil is adding to the drain on the Mega board. I thought I had read that this was OK for a total of 250 mA (but can't remember where that came from) - but it sounds as if Mega+5100 Ethernet Shield+a couple of LED's doesn't leave enough safety margin for my relay when powered from USB.

The spec page for the Mega (http://arduino.cc/en/Main/arduinoBoardMega2560) mentions a maximum of 500mA via the USB port, but doesn't seem to mention a maximum safe power consumption from the board, either when powered from USB or with a separate supply. Can anyone advise what these limits are, please?

John Geddes

If you had a scope you might see if the coil action was momentarily drawing more power than you expected. Personally I would power them separately (not through the Mega).