I'm new to the Arduino world. While playing with the tutorials for specifically the DHT11, I have found that the polling frequency is, determining on the source, 1 second, or 2 seconds. This is my question...
Can I poll the temperature, then immediately poll the humidity, then wait 1 second, or do I need to wait 1 second between polling the temp and humidity?
Example:
int humidity = dht.readHumidity();
delay(1000);
int temp = dht.readTemperature();
delay(1000);
or
int humidity = dht.readHumidity();
int temp = dht.readTemperature();
delay(1000);
There are two specification where the DHT11 is better than the DHT22. That’s the sampling rate which for the DHT11 is 1Hz or one reading every second, while the DHT22 sampling rate is 0,5Hz or one reading every two seconds and also the DHT11 has smaller body size. The operating voltage of both sensors is from 3 to 5 volts, while the max current used when measuring is 2.5mA.
When using either the DHT11 or DHT22 I use a single delay so it looks like this:
//===================START======================
//References
#include <DHT.h>
#include <DHT_U.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// set the LCD address to 0x27 for a 20 chars and 4 line display
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
// Uncomment whatever DHT type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
//Define Arduino input pin
#define DHTPIN 2
// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);
//====================SETUP=====================
void setup(){
Serial.begin(9600);
// Initialize the DHT Sensor
dht.begin();
// Initialize LCD Screen
lcd.init();
lcd.init();
lcd.backlight();
}
//====================Void Loop====================
void loop(){
//Wait a few seconds
delay(2000);
float h = dht.readHumidity();
// Read temperature as Celsius
float t = dht.readTemperature();
// Read temperature as Fahrenheit
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Compute heat index
// Must send in temp in Fahrenheit!
float hi = dht.computeHeatIndex(f, h);
// Serial print the serial monitor data
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(hi);
Serial.println(" *F");
Serial.print("DATA,TIME,"); Serial.print(t); Serial.print(","); Serial.println(h);
// LCD Data
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Humidity = ");
lcd.print( h);
lcd.print("%");
lcd.setCursor(0,1);
lcd.print("Temp. = ");
lcd.print(t);
lcd.write(char(223));
lcd.print(" C");
lcd.setCursor(0,2);
lcd.print("Temp. = ");
lcd.print(f);
lcd.write(char(223));
lcd.print(" F");
lcd.setCursor(0,3);
lcd.print("Heat Index = ");
lcd.print (hi);
lcd.write(char(223));
lcd.print("F");
}
I've played around with mine, and added your code to test for "isnan", and quite honestly, see no difference in readings. Regardless of what I set delay to, or commenting out the delay, I never see the "isnan" error. It just purrs along spitting out what appear to be valid readings.