Relay Causing COM Port Oddities?

Hey all, I’m trying to deal with this odd issue on an Arduino Uno -

I’ve got a 4 channel relay attached to digital pins 10-13. Whenever the relay is connected, the serial monitor will function correctly for a brief amount of time, then stop. Closing and attemping to re-open the window causes a “Serial port COMx already in use” error, and the REALLY weird part, is that uploading code to the board over USB fails after the serial monitor stops working.

avrdude throws up an error when the board fails to upload:

avrdude: ser_send(): write error; sorry no info avail

I’ve seen another error from avrdude come up as well, something about the COM port and not being able to write. I’ve tried reproducing the error to get the debug info, but have met little success.

The transmit LED is blinking, nothing else is reading COM port data.

Full code:

#include <Wire.h>
#include "TSL2561.h"
#include "Timer.h"
#include "DHT.h"

#define Temp1 A0
#define Temp2 A1

#define mos0 7 // Exhaust
#define mos1 8 // Power Supply 
#define in1 10 // Fogger
#define in2 11 // Lights 2 & 3
#define in3 12 // Light 1
#define in4 13 // TEC big fan

float lumnP0; // Lux variable
float hmdtP0; // humidity variable
float tempP0; // temp sensor 0
float tempP1; // temp sensor 1
float tempP2; // temp sensor 2
float tmpAvg; // average temp.

#define RELAY_ON 0
#define RELAY_OFF 1

#define DHTPIN 2
#define DHTTYPE DHT22

TSL2561 tsl(TSL2561_ADDR_FLOAT);

Timer t;

void setup()
  // Set MOSFET control pins to output:
  pinMode(mos0, OUTPUT);
  pinMode(mos1, OUTPUT);
  // Setup relays
  digitalWrite(in1, RELAY_OFF);
  digitalWrite(in2, RELAY_OFF);
  digitalWrite(in3, RELAY_OFF);
  digitalWrite(in4, RELAY_OFF);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  t.pulse(in2, 43200 * 1000, RELAY_ON); // 12/12 light cycle
  t.pulse(in3, 43200 * 1000, RELAY_ON); // for both light banks
  t.every(1000, DataRead);
  t.every(10000, EnvirCntrl);
   if (tsl.begin()) {
    Serial.println("Found sensor!");
  } else {
    Serial.println("No sensor?");
    while (1);
  tsl.setGain(TSL2561_GAIN_0X); // 16x for dim situations.

void loop()
  uint32_t lum = tsl.getFullLuminosity();
  uint16_t ir, full;
  ir = lum >> 16;
  full = lum & 0xFFFF;  
  float hmdtR0 = dht.readHumidity();
  float tempR0 = dht.readTemperature();
  float tempR1 = analogRead(Temp1);
  float tempR2 = analogRead(Temp2);
  float tempC1 = (((125.0 * tempR1) / 256.0) - 50.0);
  float tempC2 = (((125.0 * tempR2) / 256.0) - 50.0);

  float lumnR0 = tsl.calculateLux(full, ir);

  hmdtP0 = hmdtR0;
  tempP0 = tempR0;
  tempP1 = tempC1;
  tempP2 = tempC2;
  lumnP0 = lumnR0;
  tmpAvg = ((tempP0 + tempP1) / 2);

void DataRead()
  Serial.print("TEMP 0: "); Serial.println(tempP0);
  Serial.print("TEMP 1: "); Serial.println(tempP1);
  Serial.print("TEMP 2: "); Serial.println(tempP2);
  Serial.print("AVG: "); Serial.println(tmpAvg);
  Serial.print("HMDT 0: "); Serial.println(hmdtP0);
  Serial.print("LUMN 0: "); Serial.println(lumnP0);
 void EnvirCntrl()
     if (tmpAvg > 30)
     digitalWrite(mos1, HIGH);
     digitalWrite(in4, RELAY_ON);  
   if (hmdtP0 < 60)
   digitalWrite(in1, RELAY_ON);

Any clues? With this board controlling AC, I feel a bit odd about not being able to monitor anything. :stuck_out_tongue:

How have you got the relays wired up?
Link to the board?

The relays are wired NO, via the neutral wire on the plugs. GND to GND, VCC into the 5v line on the Arduino, shared with some sensors (analog temp).

The relay is by SainSmart:

Product page:


As well, whenever I (can) upload code, the relays flip on and off, as well as when I open the serial monitor.

Managed to duplicate the other avrdude error, same conditions as last time:

avrdude: ser_open(): can't set com-state for \\.\COM8

whenever I (can) upload code, the relays flip on and off, as well as when I open the serial monitor.

You are not connecting them to pin 0 & 1 are you?

Strip the code back and just have a relay clicking on and off like a blinking LED with nothing connected to the relay contacts. Get that working first.

Not knowing more about the relay board, but it sounds like the relay board is loading the 5V supply and everything is trying to run at too low a voltage. Can yo supply power to the relay card from another source?