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);
}

2
  • Can you please share the schematic – Maaz Sk Nov 26 '20 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 – jsotola Nov 26 '20 at 8:10
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);
}
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)' – Miri Dec 2 '20 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. – StarCat Dec 26 '20 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.