problem with relay

hi!

I took the code from this instructable and adapt it for my project. (http://www.instructables.com/id/Arduino-Automatic-Temperature-Humidity-Controller-/?ALLSTEPS)

the thing is that everything works fine when I control 1 relay, but controlling both relays at the same time is not working for me. Both relays behave in the same way in my sketch, and when one is ON, the second should be ON as well, but this doesnt happen. While one of them is ON, the other one is OFF and the indicator led for this relay that is OFF, is blinking very weakly at the same pace as the RX led in the arduino.

somebody sees the mistake?

#include "DHT.h"

#define threshold 22        //Setting the temperature threshold
#define DHTPIN 13           // what pin we're connected to
#define DHTTYPE DHT11       // DHT 11

const int relay1 =  8;     // the number of the relay 1 pin
const int relay2 =  9;     // the number of the relay 2 pin

DHT dht(DHTPIN, DHTTYPE);


void setup()
{
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  delay(1000);

  Serial.begin(9600);
  dht.begin();
  delay(1000);
}

void loop()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  // check if returns are valid, if they are NaN (not a number) then something went wrong!
    if (isnan(t) || isnan(h)) {
    Serial.println("Failed to read from DHT");
  } else {

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

    if (t < threshold){
      digitalWrite(relay1, LOW);
      digitalWrite(relay2, LOW);
      }    
    else
      digitalWrite(relay1, HIGH);
      digitalWrite(relay2, HIGH);
      }
}

We need to see a schematic and image showing "your" wiring.
.

image attached.

schematic in the instructable.

temperature sensor works correctly.

relay, when handled each separately works perfect. when I try to use both at the same time it doesnt work correctly

What power supply are you using?
.

regardless the board is connected to the computer via USB, powered by a 9V battery or a 12V 1A power supply, the problem persists

Measure the 5volt supply with a DVM when both relays are supposed to be on.

4,77

camilozk:
somebody sees the mistake?

    if (t < threshold)
    {
      digitalWrite(relay1, LOW);
      digitalWrite(relay2, LOW);
    }
    else
      digitalWrite(relay1, HIGH);
    digitalWrite(relay2, HIGH);

Here, you have no braces around the else-block.

So you always set relay2 to HIGH.

thank you very much!!!!

Also, the relay module needs a separate 5V supply.

Solution (also provides isolation):

  1. Remove the black GND wire (leave the 5V red wire connected).
  2. Remove the relay board's jumper.
  3. Connect a separate 5V supply to the JD-VCC and GND pins.

thanks dlloyd

why do you think I need a separate 5V power supply?

With both relays energized, it will draw about 200mA total, (90mA for coil, 10mA fir IRLED driver)x2.
A separate supply will prevent thermal overload of your Arduino's regulator.

got it. which is the max current for the regulator?

(and why I am not finding this info online...)

Not sure what the exact figure would be - it's highly dependent upon the voltage at Vin. If Vin is lowered (i.e. 7.5V) then more current will be available than with 9 or 12VDC. See reply#3.

ok. I found now some people saying that 450ma should be available when powering the UNO via usb. I am using a 12V 1A power supply, so I think I am pretty safe with the relays using 200ma

thanks!!

It's more than the current rating, it's the power rating you should be following.
If you are drawing 200ma with 12v -.7 input, the power is 2.3 watts.
The regulator will run HOT.

Edit Plus there is power from other circuit components . . .
.

could you explain the numbers differently? I do not follow

Hi,
Info about power considerations (and much more) on the ArduinoInfo.Info WIKI HERE:

terry's link does a great job!
Read his section on "Powering an Arduino".
Remember there is power consumed: on the Arduino board, your relays and any other electronic components in your circuit.

great guys. thank you very very much for your support!