Very bizarre serial port communication loss

I am having a very bizarre serial port communication problem. I have written the sketch below which monitors a small voltage input from two 3VDC solar cells on analog ports 1 and 2. if the voltage difference between the solar cells is more than 30 one way, it brings digital pin 2 low, if more than 30 the other way, it brings digital pin 3 low. Digital pins 2 and 3 go to a normal 8 relay 5V input relay board. I then use a 9VDC power supply switched through the contacts of the relays to drive a motor one way or another. I connect a UNO R3 board to a computer and upload the sketch with no problem. I then start the serial monitor and it shows me the delta between the two voltages on analog pins 1 and 2. I can shade one solar cell and the delta changes accordingly. Everything works fine and as expected until every time I turn on the 9VDC power supply. Once the motor fires up to move back and forth I loose the serial communication to the arduino board. The program continues to run and the motor works fine but every time I turn the 9vdc power supply on I either get the “serial port in use” or the computer totally forgets the serial port assignment. This is readily repeatable with two different arduino boards and three different computers (two XP and one Windows 7). Since the 9VDC running through the relay contacts is totally isolated from the arduino board and associated electronics, this makes no sense at all to me. Once I reboot the computer everything is back to normal until I power up the 9VDC power supply. HELP…

int analogPin1 = 1;     // read voltage for UP
int analogPin2 = 2;     // read voltage for DOWN
int upPin = 2;
int downPin = 3;

int up = 0; // variable to store the value read
int down = 0;

long lastMillis=0; // for serial write timer

void setup()
  Serial.begin(9600);          //  setup serial
  pinMode(upPin, OUTPUT);
  pinMode(downPin, OUTPUT);
  digitalWrite(upPin, HIGH);
  digitalWrite(downPin, HIGH);

void loop()
  up = analogRead(analogPin1);    // read the input pin
  down = analogRead(analogPin2);
 if (millis()>lastMillis+1000) {
  Serial.print(" up-down delta= ");
  Serial.println(" ");
  // UP PIN LINES --------------------------------------------------------------------------

  if ((up-(down))>30) {digitalWrite(upPin, LOW);} else {digitalWrite(upPin, HIGH);};  //move up
  if (((down)-up)>30)  {digitalWrite(downPin, LOW);} else {digitalWrite(downPin, HIGH);};  //move down


Moderator edit:
</mark> <mark>[code]</mark> <mark>

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

What about the relay ? Is it a module ? A cheap one from Ebay ? Are there non-etched copper hairs that connect the Arduino to the 9V ?

Almost certainly the 9v battery can't provide enough current and when the voltage drops the Arduino resets causing the serial connection to be broken.


Just to clarify a few things:

Thanks, Harry

everything is fine until the relays start energizing.

Do you have caps across the relay to prevent voltage spikes?

Yes the relay board is a cheap eBay (Amazon actually) board

I'm guessing no.

A spark from the Relay output pins that crosses the gap and bounces back to the Arduino ? I still think that a very thin copper line between output of the relay and the rest of the board is a realistic option. However there seems to be no cap on the relay board at all, at least a capacitor to the 5V and GND is needed. Can you check the board very good with a magnifier ? For solder that shortcuts or copper traces that shortcut? Or use a multimeter and measure the resistance from the relay output to the rest of the board.

harryarmst: I could understand the comment on the voltage drop, however I am using a separate 9VDC power supply to run through the relay contacts to power the motors. I tried a 5VDC power supply and it does the same thing.

Draw a diagram showing how everything is connected and post a photo of it.