DHT Sensors controlling 8 Relay Shield

Our range of tiny modules are designed to make it easy for you to quickly add functionality to your projects. [Module List]
Post Reply
markjj
Posts: 2
Joined: Mon Oct 21, 2013 9:33 am

DHT Sensors controlling 8 Relay Shield

Post by markjj » Mon Oct 21, 2013 9:56 am

Good Evening,

How do I switch multiply relays concurrently with the 8 Relay Shield?

I've purchased my first Arduino (Leonardo), a Freetronic 8 Relay Shield and 3 x DHT11 temperature and humidity sensors. The sensors are connected on A0, A1 & A2. I have finally figured out how to switch relays on or off at a set temperature.

Any idea's on why only one relay at a time can be in the energised state?

Here is the code: (Needs a tidy up as its bits and peace's from different examples)

Code: Select all

#include "Wire.h"
#include <dht.h>  //3sensor

#define I2C_ADDR  0x20  // 0x20 is the address with all jumpers removed
#define dht_dpin0 A0 //no ; here. Set equal to channel sensor is on
#define dht_dpin1 A1 //no ; here. Set equal to channel sensor is on
#define dht_dpin2 A2 //no ; here. Set equal to channel sensor is on

dht DHT;  //3sensor

int ledPin = 13;  // LED connected to digital pin 13

void setup(){
  
  Serial.begin( 38400 );  //Serial.begin(9600);
  
  delay(300);//3sensor  - Let system settle
  Serial.println("Humidity and temperature\n\n"); //3sensor
  delay(700);//3sensor - Wait rest of 1000ms recommended delay before
  Serial.println("RELAY8 demonstration starting up");
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output

  Wire.begin(); // Wake up I2C bus

  // Set I/O bank A to outputs
  Wire.beginTransmission(I2C_ADDR);
  Wire.write(0x00); // IODIRA register
  Wire.write(0x00); // Set all of bank A to outputs
  Wire.endTransmission();

  Serial.println("Ready. Type 0 to turn off relays, 1 - 8 to activate a relay.");
}

void loop(){

//Automatic control of relays 1 & 2 based on Sensor 0 reading  
    //Sensor 0 read out
    int SetTemp0 = 30;
    DHT.read11(dht_dpin0);
    Serial.print("Sensor A0: Current humidity = ");
    Serial.print(DHT.humidity);
    Serial.print("%  ");
    Serial.print("temperature = ");
    Serial.print(DHT.temperature); 
    Serial.println(" C  \n");
    //Sensor temperature options
    	if (DHT.temperature >= SetTemp0) {
		digitalWrite(ledPin,HIGH);
                Serial.print("Temperature is at or above ");
                Serial.print(SetTemp0);
                Serial.print(" C \n\n");
                sendValueToLatch(1);
	} else {
		digitalWrite(ledPin,LOW);
                Serial.print("Temperature is below ");
                Serial.print(SetTemp0);
                Serial.print(" C \n\n");
                sendValueToLatch(2);
	}
	delay(500);
    delay(1500);//Don't try to access too frequently... in theory
    
//Automatic control of relays 3 & 4 based on Sensor 1 reading  
    //Sensor 1 read out
    int SetTemp1 = 35;
    DHT.read11(dht_dpin1);
    Serial.print("Sensor A1: Current humidity = ");
    Serial.print(DHT.humidity);
    Serial.print("%  ");
    Serial.print("temperature = ");
    Serial.print(DHT.temperature); 
    Serial.println(" C  \n");
    //Sensor temperature options
    	if (DHT.temperature >= SetTemp1) {
		digitalWrite(ledPin,HIGH);
                Serial.print("Temperature is at or above ");
                Serial.print(SetTemp1);
                Serial.print(" C \n\n");
                sendValueToLatch(3);
	} else {
		digitalWrite(ledPin,LOW);
                Serial.print("Temperature is below ");
                Serial.print(SetTemp1);
                Serial.print(" C \n\n");
                sendValueToLatch(4);
	}
	delay(500);
    delay(1500);//Don't try to access too frequently... in theory
  
//8 Relay Control
   int command = 0;
   if (Serial.available()) {
    command = Serial.read();
    
    if( command == '0' )
    {
      sendValueToLatch(0);
      Serial.println("Resetting all relays");
    }
    if( command == '1' )
    {
      sendValueToLatch(1);
      Serial.println("Activating relay 1");
    }
    if( command == '2' )
    {
      sendValueToLatch(2);
      Serial.println("Activating relay 2");
    }
    if( command == '3' )
    {
      sendValueToLatch(4);
      Serial.println("Activating relay 3");
    }
    if( command == '4' )
    {
      sendValueToLatch(8);
      Serial.println("Activating relay 4");
    }
    if( command == '5' )
    {
      sendValueToLatch(16);
      Serial.println("Activating relay 5");
    }
    if( command == '6' )
    {
      sendValueToLatch(32);
      Serial.println("Activating relay 6");
    }
    if( command == '7' )
    {
      sendValueToLatch(64);
      Serial.println("Activating relay 7");
    }
    if( command == '8' )
    {
      sendValueToLatch(128);
      Serial.println("Activating relay 8");
    }
  }
}

void sendValueToLatch(int latchValue)
{
  Wire.beginTransmission(I2C_ADDR);
  Wire.write(0x12);        // Select GPIOA
  Wire.write(latchValue);  // Send value to bank A
  Wire.endTransmission();
}
Any help would be most appreciated. Thanks

andrew
Freetronics Staff
Freetronics Staff
Posts: 978
Joined: Sun Jul 14, 2013 7:06 am
Location: Melbourne, Australia
Contact:

Re: DHT Sensors controlling 8 Relay Shield

Post by andrew » Mon Oct 21, 2013 9:32 pm

You can control one or more outputs at the same time.
Notice in the example sketch from:
http://www.freetronics.com/pages/relay8 ... tart-guide

that we send either 1, 2, 4, 6, 16, 32, 64, or 128 (which is the "latchvalue") to the MCP23017 controller on the shield using the following function:

Code: Select all

void sendValueToLatch(int latchValue)
{
  Wire.beginTransmission(I2C_ADDR);
  Wire.write(0x12);        // Select GPIOA
  Wire.write(latchValue);  // Send value to bank A
  Wire.endTransmission();
}
However you can add those values to control more than one output at a time. For example, if you wanted to turn on outputs 1 and 8, the latch value would be 1+128 (so send 129). Or if you want them all on except for 4, use 247 (which is 1+2+4+16+32+64+128). Etc.

markjj
Posts: 2
Joined: Mon Oct 21, 2013 9:33 am

Re: DHT Sensors controlling 8 Relay Shield

Post by markjj » Tue Oct 22, 2013 9:48 am

Thanks Jon - Works just fine!

andrew
Freetronics Staff
Freetronics Staff
Posts: 978
Joined: Sun Jul 14, 2013 7:06 am
Location: Melbourne, Australia
Contact:

Re: DHT Sensors controlling 8 Relay Shield

Post by andrew » Tue Oct 22, 2013 8:37 pm

markjj wrote:Thanks Jon - Works just fine!
Great. If you have any more questions, let me know.

Post Reply