Serial monitor crash when relays switch on main power

I’m pretty new to arduino so hopefully my issue is easily resolved, I couldn’t however find the solution on my own this time.

I’m trying to build an automated mushroom incubator for growing oyster mushrooms in an aquarium. In doing this, humidity is the first parameter I want to control. Useing the DHT11 sensor to pick up temperature and humitidy inside the aquarium I written a basic code (see below) to make the arduino switch on a relay controlling a humidifier connected to the wall socket (I am using an low-energy lamp for testing the code).

Everything seems to work well when the lamp is not plugged into the wall socket. When I plug it in and run the program the communication with the serial monitor stops almost instantly as the relay switches on and the serial port im using (COM5) disappears from Tools > Serial Port. The port number also changed from 4 to 5 yesterday for some reason I do not understand. After the serial monitor crashes the program seem to continue to run without problems as it stops switching on the relays when i breathe on the sensor.

Yesterday I thought this would be an interference problem with the main power, and changed all cables between the arduino and relay board to shielded signal cables (50cm), but the issue remains.

Code below:

#include "DHT.h"
#define DHTPIN 7 //pin för sensor output
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

#define RELAY_ON 1
#define RELAY_OFF 0
#define RELAY_1 2 //pin för relä 1

void setup() {
  Serial.begin(9600);
  dht.begin();
 
  Serial.println("Welcome to Mushroom Incubator!");
  
  pinMode(RELAY_1, OUTPUT);
  digitalWrite(RELAY_1, RELAY_OFF);
}

void loop() {
  delay(2000); //vänta X sec mellan mätningar
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
    }

  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.println(" *C ");

  if (h < 70) {
    Serial.println("Humidity LOW, relay switched ON");
    digitalWrite(RELAY_1, RELAY_ON);
    delay(3000); //kör i X sec
    Serial.println("Relay switched OFF, waiting for new sensor values");
    Serial.println("");
    digitalWrite(RELAY_1, RELAY_OFF); //kör i X sek, stäng av, vänta på ny sensor output
  }
 
  else {
    Serial.println("Humidity HIGH, relay switched OFF");
    Serial.println(" ");
    digitalWrite(RELAY_1, RELAY_OFF);
  }
}

Here is a picture of my current setup:
http://s1274.photobucket.com/user/lilliemarckjakob/media/MushroomIncubator009liten_zpse7339176.jpg.html

Thanks!

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

I suspect you are trying to power the relay coils from the Arduino and it cannot supply enough current. If so, there is a high risk of damaging the Arduino.

...R

Are you powering the arduino from your USB port via the computer ?
If so, it could be that the relay is drawing more current then the USB port is providing.

Possible options:

  • try a decent power supply on the arduino. (not USB from computer)

  • power the relay with its own power source.
    (I use the exact same relay board with an old phone adapter: 5v / 700ma)

On another note: the fact your com port changed, could indicate something is either wrong with your windows or USB cable.

Also, I have a near same setup as yours to do some climate controlling, and have been getting interference from a relay powering a fan/motor. (caused my LCD to go blank, and the program kept running).

A member of this forum suggested me to place 100nF capacitors between the signal cables controlling the relay and ground. So far so good. (topic)

Thanks for the quick replys!

Yes, I’m powering the arduino with the USB, and the relays from the arduino 5v pin. I will try using another power source.

Robin2
Can the arduino be damaged by too little voltage? What are the risks is your suspicion is correct?

I will try to post a schematic later tonight when I find the time if the issue haven’t been resolved!

Thanks again!

Its vital to route the high current load wiring (lamp mains cable?) well away from all signal cables - running wires in parallel to the mains will guarantee interference in my experience, shielded or not, because induction is not affected by shielding (which only protects against capacitive pick-up).

If you have ground loops that's a very efficient route for induced voltages to affect your circuit. Adopt star-grounding for all the low-voltage wiring.

Do you know if your mains load is inductive? Snubber network may be required.

Gardener: Robin2 Can the arduino be damaged by too little voltage? What are the risks is your suspicion is correct?

Generally it is excessive current that causes damage. If your relays draw so much current that they cause the Arduino voltage to drop then that high current is what will cause the damage.

If you had something else in mind when you asked your question please let me know.

...R

That lamp seems to be a PL lamp. Those were the first power saving lamps that could be put in a normal E27 lamp fitting (that i know of). If so, then it is highly inductive, as it is nothing else than a small TL that was bent multiple times to fit the jar it is mounted in. They evolved from standard (bi-metal) starter to electronic starter to a complete electronic module. The first two were very inductive due to the nature of a starter. The last one was less so. This was over 25 years ago, though.

Alright, I have now got a 5V 1000mA transformator to power my relays.

When I'm powering the relays from the arduino, everything works fine when relays are not switching mains (wall socket not plugged in). The power-LED is lighting and the relay switches on and off as intended.

When I try to power the relay from the transformator I bought (5V 1000mA) the power-LED is lighting, but the relay does not switch on. IThe arduino is still powered from my USB port.

This is the data chart for the relays on my board: http://www.parallax.com/sites/default/files/downloads/27115-Single-Relay-Board-Datasheet.pdf

This is the relay board (couldn't find a proper data chart): http://www.hobbytronics.co.uk/4-channel-relay-board?keyword=relay

The transformator model is MW3K10GS from MINWA electronics (couldn't find a data chart on their site): http://www.soselectronic.com/?str=371&artnum=91705&name=minwa-mw3k10gs#tabs-1

From what I can see, 1000mA should be more than enough as the relay is using less than 100mA each (and I'm only testing this on one of them atm). I feel very puzzled about this. Does anyone know what I'm doing wrong connecting the relays to a separate powersource? Is it that I need to connect the arduino and relay 5v DC to a common ground? i.e. cable from - on transformator to gnd on my arduino?

MAS3: That lamp seems to be a PL lamp. Those were the first power saving lamps that could be put in a normal E27 lamp fitting (that i know of). If so, then it is highly inductive, as it is nothing else than a small TL that was bent multiple times to fit the jar it is mounted in. They evolved from standard (bi-metal) starter to electronic starter to a complete electronic module. The first two were very inductive due to the nature of a starter. The last one was less so. This was over 25 years ago, though.

Seems you were quite right ;) tried with both transformator/LED and light bulb setup and everything worked fine except for one time. My PC is not super healthy, so I suspect that's what happend this time. Also tried to change to the previous lamp while running and then it instantly breaks seral monitor communication. Thanks for all the help:)!

Is it that I need to connect the arduino and relay 5v DC to a common ground?

That's a good question.

Usually, the answer would be yes. But since the input to the relay module uses an optoisolator, you don't need to, because their is no opportunity for current to flow between the arduino and the relay board.