I am trying to get humidity and temperature readings (eventually to trigger a misting system), but the values keep returning as 0C and 0%humidity (in general). Originally, I was using an am2320 sensor and using the analog pins. Here, I used the following code:
#include "dht.h"
#define dht_apin A2 // Analog Pin sensor is connected to
#define MIST 7
#define LIGHT 2
dht DHT;
void setup(){
pinMode(MIST, OUTPUT);
pinMode(LIGHT, OUTPUT);
Serial.begin(9600);
delay(500);//Delay to let system boot
Serial.println("DHT11 Humidity & temperature Sensor\n\n");
delay(1000);//Wait before accessing Sensor
}//end "setup()"
void loop(){
//Start of Program
DHT.read11(dht_apin);
Serial.print("Current humidity = ");
Serial.print(DHT.humidity);
Serial.print("% ");
Serial.print("temperature = ");
Serial.print(DHT.temperature);
Serial.println("C ");
delay(5000); //Wait 5 seconds before accessing sensor again.
}
//Fastest should be once every two seconds.
// end loop()
}
Thinking the sensor may have not been working, I tried using a different sensor (DHT11), and following the tutorials got the same result using this code:
#include <dht11.h>
#define DHT11PIN 4
dht11 DHT11;
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println();
int chk = DHT11.read(DHT11PIN);
Serial.print("Humidity (%): ");
Serial.println((float)DHT11.humidity, 2);
Serial.print("Temperature (C): ");
Serial.println((float)DHT11.temperature, 2);
delay(2000);
}
// end loop()
The Arduino is being powered by a wall wart and is also connected to my computer via USB (I read that sometimes the USB doesn't supply enough power). The weird part is that for about 1 minute I got reasonable results (humidity ~50%, temp 22C). Then, I tried adding an if loop to turn on an LED if the humidity dropped below 60% (thinking I could use the same code to operate a relay to turn on a misting system). The LED lit up, but the readout went back to the 0%humidity and 0C. I have since deleted this loop but cannot seem to get reasonable measurements. If I change the signal pin, I sometimes get a new readout 255% humidity and 255C, but I haven't been able to repeat this reliably.
For the wiring: S to pin, + to 5V, - to GND
Note: I have tried every analog pin and "regular" pin, so the code may designate something that is different in my picture. When testing, I have triple checked that the signal pin in the code is correct for my wiring configuration. I've also tried with a different Arduino with the same result. I'm pretty new at this, but the code is pretty much copy-paste and there are only 3 wires. I've read that there are many DHT libraries, so I'm thinking my issue has something to do with that, but I have no clue how to judge the appropriateness of a library for my application.
Order of pins = S, +, - My wiring[[[[[[[[[[[[p
-
1Does your DHT11 board has a pull up resistor on it? The Data pin requires a pull-up resistor (4.7k would be okay) to 5V.hcheung– hcheung2020年05月12日 06:05:16 +00:00Commented May 12, 2020 at 6:05
-
Have you tried a different library, to rule out hardware issues?StarCat– StarCat2020年05月13日 20:51:27 +00:00Commented May 13, 2020 at 20:51
2 Answers 2
You need to add the following line of code in the setup() function:
dht.begin();
Connect the sensor as in this diagramm (including a 10k resistor!):
DHT sensor to UNO
Watch the empty connector opposite to your breadboard plug-in
and use the following test sketch with this library:
// Example testing sketch for DHT humidity/temperature sensor
#include "DHT.h"
#define DHTPIN 4 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11
// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1 to 3.3V instead of 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
// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors. This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println(F("DHTxx test!"));
dht.begin();
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
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);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// 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);
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(f);
Serial.print(F("°F Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));
}
-
2 issues: 1.) The code is giving an "error compiling for board Arduino" (full error message at bottom) 2.) I ony have 3 pins to connect, not four. Should I try a resistor on the signal pin? DataSheet velleman.eu/downloads/29/vma311_a4v01.pdf Full error codesumbody32point5– sumbody32point52020年05月12日 14:13:14 +00:00Commented May 12, 2020 at 14:13
-
In file included from LIBRARY LOCATION/DHT_sensor_library-1.3.8/DHT_U.cpp:15:0: LIBRARY LOCATION/DHT_U.h:36:10: fatal error: Adafruit_Sensor.h: No such file or directory #include <Adafruit_Sensor.h> ^~~~~~~~~~~~~~~~~~~ compilation terminated. exit status 1 Error compiling for board Arduino Uno.sumbody32point5– sumbody32point52020年05月12日 14:21:07 +00:00Commented May 12, 2020 at 14:21
-
@sumbody32point5 do you see any resistor on the sensor's breakout board? If no, then add one between signal and Vcc.Sim Son– Sim Son2020年05月12日 15:12:56 +00:00Commented May 12, 2020 at 15:12
-
You need the additiona llib installed its asking for - read the error message: Adafruit_Sensor.h: No such file or directory Add the Adafruit_Sensor lib Install it with IDE via lib managerCodebreaker007– Codebreaker0072020年05月12日 17:23:44 +00:00Commented May 12, 2020 at 17:23
-
@Codebreaker007 Sorry that was dumb of me - got the library. The output is now "DHTxx test! Failed to read from DHT sensor!" There is a resistor on the breakout board, so I don't think that is the issue. Just to be sure, I measured the resistance berween VCC and data line and got about 10Kohms. I'm using pin 4 as in your code and tried on other pins as well. I got one reading where the output was "Read from DHT sensor," (one single line) but then it went back to "Failed to read from DHT sensor." Is there a way to use a multimeter to verify eveything is working?sumbody32point5– sumbody32point52020年05月12日 19:45:07 +00:00Commented May 12, 2020 at 19:45