DHT21 Controlling Relay Problem

Hi all, I am just getting into learning programming.

I am trying to control a relay with the results from a DHT21 temp/humidity sensor. Whenever Humidity reading is below 60% I would like the relay to turn on for 4 seconds, Shut off.

#include "DHT.h"

#define DHTPIN 3     // what digital pin we're connected to
#define RELAY1 8    

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Initialize DHT sensor.
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  pinMode(RELAY1, OUTPUT);
  Serial.begin(9600);
  Serial.println("Waiting for first reading");

  dht.begin();
}

void loop() {
  // Wait a few seconds between measurements.
  delay(10000);
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);
  

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

    {
  if (h <60)   //if humidity is less that 60% that turn relay HIGH for 4 seconds
  {
    digitalWrite (RELAY1,HIGH);
    delay(4000);
  } else {
    digitalWrite, (RELAY1,LOW);
  }
 }
 
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hic);
  Serial.print(" *C ");
  Serial.print(hif);
  Serial.println(" *F");



}

There is no errors, but…

  • When I turn on the Arduino, Relay1 turns on for 10sec then turns off (according to LED built into relay)
  • If i cover the DHT21 with my hand and raise the humidity above 60%, turn the arduino on, RELAY1 stays on until I take my hand off the sensor and humidity drops. If i cover it again without resetting arduino… relay wont turn back on.

Can anyone help me out with telling me what i am missing??

Thanks

Are you sure your relay is active HIGH? Can you post a link to it?

one of these guys..

http://microcontrollershop.com/product_info.php?products_id=5919

I shouldnt need a transistor or resistor right??

https://www.amazon.com/gp/product/B00KTELP3I/ref=oh_aui_detailpage_o00_s00?ie=UTF8&psc=1

sorry this is the exact 8 piece board i am using.

Yes those are active LOW. Some user comments from the Amazon site:

Writing a HIGH to each relay input will then keep the relay in the Normally Closed (NC) state with the relay coil OFF. Writing a LOW to each relay input will switch the relay coil ON and place the relay in the Normally Open (NO) state and turn on the LED which is commonly referred to as "closing the relay".

So it comes on at first because your on/off logic is backwards.

Thanks for the reply. I ended up switching the HIGH’s and Low’s. Also changed the code around a bit.

Everything seems to be working fine except after the relay turns on, it doesnt turn back off…

#include "DHT.h"

#define DHTPIN 3 // what digital pin we're connected to
#define relay1 8 //Connect relay1 to pin 8
#define relay2 9 //Connect relay2 to pin 9
#define relay3 10 //Connect relay3 to pin 10
#define relay4 11 //Connect relay4 to pin 11
#define relay5 12 //Connect relay5 to pin 12
#define relay6 13 //Connect relay6 to pin 13
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321

// Initialize DHT sensor.
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  Serial.println("Waiting for first reading");
  pinMode(relay1, OUTPUT); //Set relay1 as an output
  pinMode(relay2, OUTPUT); //Set relay2 as an output
  pinMode(relay3, OUTPUT); //Set relay3 as an output
  pinMode(relay4, OUTPUT); //Set relay4 as an output
  pinMode(relay5, OUTPUT); //Set relay5 as an output
  pinMode(relay6, OUTPUT); //Set relay6 as an output
  digitalWrite(relay1, HIGH); //Switch relay1 off
  digitalWrite(relay2, HIGH); //Swtich relay2 off
  digitalWrite(relay3, HIGH); //Switch relay3 off
  digitalWrite(relay4, HIGH); //Swtich relay4 off
  digitalWrite(relay5, HIGH); //Switch relay5 off
  digitalWrite(relay6, HIGH); //Swtich relay6 off
  
  dht.begin();
}

void loop() {
  // Wait a few seconds between measurements.
  delay(7000);
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);
  

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

//if humidity is less that 60% that turn relay HIGH for 4 seconds
    {
      
  if (h <60)   
  {
    digitalWrite (relay1,LOW);
    delay(4000);
    digitalWrite, (relay1,HIGH);
  } else {digitalWrite, (relay1,LOW);
  }
 }
 
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hic);
  Serial.print(" *C ");
  Serial.print(hif);
  Serial.println(" *F");



}

I try and send it back to HIGH twice. Once in the void setup () at the begining and once after the delay in the if function

My apoligies…

if (h <60)   
  {
    digitalWrite (relay1,LOW);
    delay(10000);
    digitalWrite, (relay1,HIGH);
  } else {digitalWrite, (relay1,HIGH);

dont know how 2 , got added to the digitalWrite’s

It stays on because h<60 remains constantly true.

hi
It’s because of a mistake in (,)

digitalWrite (RELAY1,HIGH);
delay(4000);
} else {
digitalWrite, (RELAY1,LOW);

Erase (,) at the top line