0

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);
}
asked Nov 26, 2020 at 4:14
2
  • Can you please share the schematic Commented Nov 26, 2020 at 5:46
  • 4
    why do you expect it to light? ... you set the humidity to zero just before you check if it is above 70 Commented Nov 26, 2020 at 8:10

1 Answer 1

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);
}
StarCat
1,6711 gold badge7 silver badges15 bronze badges
answered Nov 26, 2020 at 9:45
2
  • 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)' Commented Dec 2, 2020 at 23:57
  • @Miri, It’s fixed in the answer now by adding DHT11pin to the dht11.read() call. This is very basic troubleshooting and you could have fixed this yourself. Commented Dec 26, 2020 at 13:15

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.