the point of my code is to turn on a LED when the humidity level reaches a certain amount. However it reads the amount of humidity but the LED is not turning on. Why isn't it working??
#include <SimpleDHT.h>
// for DHT11,
// VCC: 5V or 3V
// GND: GND
// DATA: 2
int pinDHT11 = 2;
SimpleDHT11 dht11;
void setup() {
Serial.begin(9600);
pinMode(9, OUTPUT);
}
void loop() {
// start working...
Serial.println("=================================");
Serial.println("Sample DHT11...");
// read without samples.
byte temperature = 0;
byte humidity = 0;
int err = SimpleDHTErrSuccess;
if (humidity > 70 ) {
digitalWrite(9, HIGH);
}
else if ((err = dht11.read(pinDHT11, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT11 failed, err="); Serial.println(err);delay(1000);
return;
}
Serial.print("Sample OK: ");
Serial.print((int)temperature); Serial.print(" *C, ");
Serial.print((int)humidity); Serial.println(" H");
// DHT11 sampling rate is 1HZ.
delay(1500);
}
-
Can you please share the schematicMaaz Sk– Maaz Sk2020年11月26日 05:46:48 +00:00Commented Nov 26, 2020 at 5:46
-
4why do you expect it to light? ... you set the humidity to zero just before you check if it is above 70jsotola– jsotola2020年11月26日 08:10:07 +00:00Commented Nov 26, 2020 at 8:10
1 Answer 1
Your code does not work because in fact error statement reads the value of temperature and humidity sensor.
You set the humidity
and temperature
variables to 0. If statement compares this variables to 70, before actually reading sensor data.
I haven't tested but this should work.
#include <SimpleDHT.h>
int pinDHT11 = 2;
SimpleDHT11 dht11;
void setup()
{
Serial.begin(9600);
pinMode(9, OUTPUT);
}
void loop()
{
// start working...
Serial.println("=================================");
Serial.println("Sample DHT11...");
// read without samples.
byte temperature = 0;
byte humidity = 0;
int err = SimpleDHTErrSuccess;
if ((err = dht11.read(pinDHT11, &temperature, &humidity, NULL))
!= SimpleDHTErrSuccess)
{
Serial.print("Read DHT11 failed, err=");
Serial.println(err);
delay(1000);
return;
}
Serial.print("Sample OK: ");
Serial.print((int)temperature);
Serial.print(" *C, ");
Serial.print((int)humidity);
Serial.println(" H");
if (humidity > 70 )
{
Serial.println("Humidity is above 70%");
digitalWrite(9, HIGH);
}
else
{
Serial.println("Humidity is below 70%");
}
// DHT11 sampling rate is 1HZ.
delay(1500);
}
-
hi, unfortuantly this code does not work, but thank you. It delivers me with the error message 'exit status 1 no matching function for call to 'SimpleDHT11::read(byte*, byte*, NULL)'Miri– Miri2020年12月02日 23:57:58 +00:00Commented Dec 2, 2020 at 23:57
-
@Miri, It’s fixed in the answer now by adding
DHT11pin
to thedht11.read()
call. This is very basic troubleshooting and you could have fixed this yourself.StarCat– StarCat2020年12月26日 13:15:49 +00:00Commented Dec 26, 2020 at 13:15