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??
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.