Hi,
I am in the process of designing an Arduino compatible code to control an exhaust fan and a sprinkler controller to automate my greenhouse. I am using 6 nos of DHT11 temperature/humidity sensors at different locations inside the greenhouse to monitor temperatures and humidities . Now my question is how to average the 6 temperature values (one from each sensor) and 6 humidity values . I need only average it once in every 5 minutes or so (or even longer). If the average of the temperatures from 6 sensors is above certain value an Arduino pin should go high which in turn will switch on the exhaust fan till the temperature comes down below the preset value. Similarly for the average humidity if the value is below certain percentage another Arduino pin will go high which will switch on the sprinkler till the humidity reaches the preset value.
I am not a big programmer hence any help from expert forum members is very much appreciated. Thanks in advance.
Do it just like you would do it by hand.
Read the 6 sensor values.
Put the 6 values into an array or into 6 separate variables.
Add them all together and divide the total by 6.
lar3ry:
Do it just like you would do it by hand.
Read the 6 sensor values.
Put the 6 values into an array or into 6 separate variables.
Add them all together and divide the total by 6.
"Exactly I want to know how to write the code for that and how to flag pins high to control exhaust fan and sprinkler. "
(Nivar)
Can you explain what part you are stuck on, reading the sensors, adding up 6 values, or dividing by 6?
Do you have any existing code talking to the DHT11? If not get an example up and running first. Do you have any library for the DHT11 installed yet?
Hi Mark,
Yes my code can communicate with all the DHT sensors . Where I am unable to write the code is for getting the average of 6 DHT sensors for both temperature and humidity and then flagging two pins on the chip ( when parameters are above /below preset limits) for controlling exhaust fan and sprinkler respectively . My current program is reproduced below for your reference. Thanks for your time .
(Nivar)
#include "DHT.h"
#define DHT1PIN 2 // what pin we're connected to
#define DHT2PIN 3
#define DHT3PIN 4
#define DHT4PIN 5
#define DHT5PIN 6
#define DHT6PIN 7
#define DHT7PIN 8
// Uncomment whatever type you're using!
#define DHT1TYPE DHT11 // DHT 11
#define DHT2TYPE DHT11
#define DHT3TYPE DHT11
#define DHT4TYPE DHT11
#define DHT5TYPE DHT11
#define DHT6TYPE DHT11
#define DHT7TYPE DHT22// DHT 22 (AM2302)
// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
DHT dht1(DHT1PIN, DHT1TYPE);
DHT dht2(DHT1PIN, DHT2TYPE);
DHT dht3(DHT1PIN, DHT3TYPE);
DHT dht4(DHT1PIN, DHT4TYPE);
DHT dht5(DHT1PIN, DHT5TYPE);
DHT dht6(DHT1PIN, DHT6TYPE);
DHT dht7(DHT1PIN, DHT7TYPE);
void setup() {
Serial.begin(9600);
Serial.println("DHTxx test!");
dht1.begin();
dht2.begin();
dht3.begin();
dht4.begin();
dht5.begin();
dht6.begin();
dht7.begin();
}
void loop() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h1 = dht1.readHumidity();
float t1 = dht1.readTemperature();
float h2 = dht2.readHumidity();
float t2 = dht2.readTemperature();
float h3 = dht3.readHumidity();
float t3 = dht3.readTemperature();
float h4 = dht4.readHumidity();
float t4 = dht4.readTemperature();
float h5 = dht5.readHumidity();
float t5 = dht5.readTemperature();
float h6 = dht6.readHumidity();
float t6 = dht6.readTemperature();
float h7 = dht7.readHumidity();
float t7 = dht7.readTemperature();
// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t1) || isnan(h1)) {
Serial.println("Failed to read from DHT #1");
} else {
Serial.print("Humidity 1: ");
Serial.print(h1);
Serial.print(" %\t");
Serial.print("Temperature 1: ");
Serial.print(t1);
Serial.println(" *C");
}
if (isnan(t2) || isnan(h2)) {
Serial.println("Failed to read from DHT #2");
} else {
Serial.print("Humidity 2: ");
Serial.print(h2);
Serial.print(" %\t");
Serial.print("Temperature 2: ");
Serial.print(t2);
Serial.println(" *C");
}
if (isnan(t1) || isnan(h3)) {
Serial.println("Failed to read from DHT #3");
} else {
Serial.print("Humidity 3: ");
Serial.print(h3);
Serial.print(" %\t");
Serial.print("Temperature 3: ");
Serial.print(t3);
Serial.println(" *C");
}
if (isnan(t1) || isnan(h4)) {
Serial.println("Failed to read from DHT #4");
} else {
Serial.print("Humidity 4: ");
Serial.print(h4);
Serial.print(" %\t");
Serial.print("Temperature 4: ");
Serial.print(t4);
Serial.println(" *C");
}
if (isnan(t1) || isnan(h5)) {
Serial.println("Failed to read from DHT #5");
} else {
Serial.print("Humidity 5: ");
Serial.print(h5);
Serial.print(" %\t");
Serial.print("Temperature 5: ");
Serial.print(t5);
Serial.println(" *C");
}
if (isnan(t1) || isnan(h6)) {
Serial.println("Failed to read from DHT #6");
} else {
Serial.print("Humidity 6: ");
Serial.print(h6);
Serial.print(" %\t");
Serial.print("Temperature 6: ");
Serial.print(t6);
Serial.println(" *C");
}
if (isnan(t1) || isnan(h7)) {
Serial.println("Failed to read from DHT #7");
} else {
Serial.print("Humidity 7: ");
Serial.print(h7);
Serial.print(" %\t");
Serial.print("Temperature 7: ");
Serial.print(t7);
Serial.println(" *C");
}
Serial.println();
}
Added the code tag as requested .I am sorry as an infrequent user I was not aware of the code tag . Thanks for reminding me about the need of the same.
(Nivar)
Thanks Jremington .I hope similarly I have to average the "temps" also. Now after this I want to set two pins on the board high to control a fan and sprinkler above certain preset value for temperature and below certain value for humidity . It will be great if you can add that code also for me. Hope all these codes comes within the " void loop" only . Is there any order for that or can I add the same at the end of the current program?
Regards,
(Nivar)
You will have a lot more fun with this hobby if you learn a bit of C/C++ programming. I suggest to work through one of the many on line tutorials, or get a book and read about if statements, control structures, etc.
Arduino comes with a bunch of built in tutorials (from the IDE select file>examples>) that illustrate these very basic principles.
If you want to hire someone to write code for you, post on the Gigs and Collaborations forum section.