DHT22 temp sensor and fan relay

I am trying to set up my DHT22 temp sensor to control a fan. However, I am having trouble getting to relay to actuate within certain temperature ranges. Any suggestions or code examples?

thanks in advance

#include <DHT.h>;

#define DHTPIN 7
#define DHTTYPE DHT22
DHT dht (DHTPIN, DHTTYPE);

const int RELAY_PIN5 = A5;
const int RELAY_PIN0 = A0;

int chk;
float hum;
float temp;

void setup()
{
Serial.begin(9600);
dht.begin();

pinMode(RELAY_PIN5, OUTPUT);

digitalWrite( RELAY_PIN5, LOW);
}

void loop(){

if (temp > 78){

digitalWrite(RELAY_PIN5, HIGH);
}

else{

digitalWrite(RELAY_PIN5, LOW);
}

delay(3000);

hum = dht.readHumidity();
temp = dht.readTemperature();

Serial.print("Hum: ");
Serial.print(hum);
Serial.print(" %");

Serial.println();

Serial.print("Temp: ");
Serial.print((int)round(1.8*temp+30));
Serial.print(" Degrees");

Serial.println();

}

or even just turn fan on and off! any ideas?

Hi,
your code is correct, you should turn on the fan when the temperature is above 78 and turn it off when it is equal to or below 78.
The problem is logical/mathematical.
You didn't explain it, but I believe your 78 is 78°F.
Correct?

But your DHT22 sensor sends the temperature in °C.
Despite its transformation to °F in this line:
"Serial.print((int)round(1.8 * temp + 30));" printed the value in
°F, but the variable remained with the value in °C.

Try this code below:

#include <DHT.h>;
#define DHTPIN 7
#define DHTTYPE DHT22
DHT dht (DHTPIN, DHTTYPE);
const int RELAY_PIN5 = A5;
const int RELAY_PIN0 = A0;
int chk;
float hum;
float temp;
//-----------------------------------------------------------------------------
void setup()
{
  Serial.begin(9600);
  dht.begin();
  pinMode(RELAY_PIN5, OUTPUT);
  digitalWrite( RELAY_PIN5, LOW);
}
//-----------------------------------------------------------------------------
void loop() {
  hum = dht.readHumidity();
  temp = (int)round(1.8 *  dht.readTemperature() + 30);
  
  if (temp > 78) {
    digitalWrite(RELAY_PIN5, HIGH);
  }
  else {
    digitalWrite(RELAY_PIN5, LOW);
  }
  delay(3000);
  Serial.print("Hum: ");
  Serial.print(hum);
  Serial.print(" %");
  Serial.println();
  Serial.print("Temp: ");
  Serial.print(temp);
  Serial.print(" Degrees");
  Serial.println();
}

PS: // Read temperature as Fahrenheit (isFahrenheit = true)
*** float f = dht.readTemperature(true);***

See if this mod works:

#include <DHT.h>;

#define DHTPIN 7
#define DHTTYPE DHT22
DHT dht (DHTPIN, DHTTYPE);

const int RELAY_PIN5 = A5;
const int RELAY_PIN0 = A0;

int chk;
float hum;
float temp;

void setup()
{
Serial.begin(9600);
dht.begin();

pinMode(RELAY_PIN5, OUTPUT);

digitalWrite( RELAY_PIN5, LOW);
}

void loop(){
hum = dht.readHumidity();
temp = dht.readTemperature(true);
if (temp > 78.0){
  digitalWrite(RELAY_PIN5, HIGH);
}

else if(temp < 77.0){
  digitalWrite(RELAY_PIN5, LOW);
}
Serial.print("Hum: ");
Serial.print(hum);
Serial.print(" %");
Serial.println();

Serial.print("Temp: ");
Serial.print(temp);
Serial.print(" Degrees");
Serial.println();

}

Thank you! the temperature portion is now working correctly. however, the humidity relay does not actuate when humidity is above 50%. any ideas?

#include <DHT.h>;

#define DHTPIN 7
#define DHTTYPE DHT22 
DHT dht (DHTPIN, DHTTYPE);


const int FAN = A5;
const int PUMP = A0;
const int DEHUM = A4;

int chk;
float hum;
float temp;

void setup(){
  
  Serial.begin(9600);
  dht.begin();
  
  pinMode(FAN, OUTPUT);
  digitalWrite(FAN, HIGH);
  digitalWrite(DEHUM, HIGH);
  
}

void loop(){

    hum = dht.readHumidity();
    
    if (hum > 50){
   
  digitalWrite(DEHUM, HIGH);
  }
   
   else{ 
     
  digitalWrite(DEHUM, LOW);
    
 }
    temp = dht.readTemperature();
    
    delay(500);
    
    Serial.print("Hum: ");
    Serial.print(hum);
    Serial.print(" %");
    
    Serial.println();
    
    Serial.print("Temp: ");
    Serial.print((int)round(1.8*temp+32));
    Serial.print(" Degrees");
    
    Serial.println();
    
    delay(1000);
    
 if (temp < 23.8889){
   
  digitalWrite(FAN, HIGH);
}
     
 else{
    
  digitalWrite(FAN, LOW);
  
  delay(250);
    
 }
} 

I don't see where DEHUM is set as OUTPUT, change void setup() to:

void setup(){
  
  Serial.begin(9600);
  dht.begin();
  
  digitalWrite(FAN, HIGH);  // set pin HIGH or LOW first,
  digitalWrite(DEHUM, HIGH);
  pinMode(FAN, OUTPUT); // then set as OUTPUT
  pinMode(DEHUM, OUTPUT);
  
}

I'm trying to turn a water pump ON for 30 seconds and then OFF for 30 minutes.

here is what I have so far:

#include <DHT.h>;

#define DHTPIN 7
#define DHTTYPE DHT22 
#define IRR_ON 30000
#define IRR_OFF 18000000
DHT dht (DHTPIN, DHTTYPE);


const int FAN = A5;
const int PUMP = A0;
const int DEHUM = A4;

int chk;
float hum;
float temp;

void setup(){
  
  Serial.begin(9600);
  dht.begin();
  
  pinMode(FAN, OUTPUT);
  pinMode(DEHUM, OUTPUT);
  pinMode(PUMP, OUTPUT);
  digitalWrite(FAN, HIGH);
  digitalWrite(DEHUM, HIGH);
  
}

void loop(){
  
  
    hum = dht.readHumidity();
    
    delay(250);
    
    temp = dht.readTemperature();
    
    
    if (hum < 50){
   
  digitalWrite(DEHUM, HIGH);
  }
   
   else{ 
     
  digitalWrite(DEHUM, LOW); 
   }
  
  delay(15000);
  
  if (temp <= 23.8889){
   digitalWrite(FAN, HIGH);
  }
  
 else{
  digitalWrite(FAN, LOW);
 }
 
  if (millis() < IRR_OFF){
   digitalWrite(PUMP, HIGH);
  }
  
  if(millis() < IRR_ON){
   digitalWrite (PUMP, LOW);
  }
    
  delay(2000);
  {
    
    Serial.print("Hum: ");
    Serial.print(hum);
    Serial.print(" %");
    
    Serial.println();
    
    Serial.print("Temp: ");
    Serial.print((int)round(1.8*temp+32));
    Serial.print(" Degrees");
    
    Serial.println();
  
  delay(250);
  
 }
}

having trouble getting pump to turn on for 30 seconds and off for 30 min

#include <DHT.h>;

#define DHTPIN 7
#define DHTTYPE DHT22 
#define IRR_ON 30000
#define IRR_OFF 18000000
DHT dht (DHTPIN, DHTTYPE);


const int FAN = A5;
const int PUMP = A0;
const int DEHUM = A4;

int chk;
float hum;
float temp;

void setup(){
  
  Serial.begin(9600);
  dht.begin();
  
  pinMode(FAN, OUTPUT);
  pinMode(DEHUM, OUTPUT);
  pinMode(PUMP, OUTPUT);
  digitalWrite(FAN, HIGH);
  digitalWrite(DEHUM, HIGH);
  
}

void loop(){
  
  
    hum = dht.readHumidity();
    
    delay(250);
    
    temp = dht.readTemperature();
    
    
    if (hum < 50){
   
  digitalWrite(DEHUM, HIGH);
  }
   
   else{ 
     
  digitalWrite(DEHUM, LOW); 
   }
  
  delay(15000);
  
  if (temp <= 23.8889){
   digitalWrite(FAN, HIGH);
  }
  
 else{
  digitalWrite(FAN, LOW);
 }
 
  if (millis() < IRR_OFF){
   digitalWrite(PUMP, HIGH);
  }
  
  if(millis() < IRR_ON){
   digitalWrite (PUMP, LOW);
  }
    
  delay(2000);
  {
    
    Serial.print("Hum: ");
    Serial.print(hum);
    Serial.print(" %");
    
    Serial.println();
    
    Serial.print("Temp: ");
    Serial.print((int)round(1.8*temp+32));
    Serial.print(" Degrees");
    
    Serial.println();
  
  delay(250);
  
 }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.