Information on "SimpleDHT11"

Hi, I've just cut and pasted the following code:

#include <SimpleDHT.h>

// for DHT11, 
//      VCC: 5V or 3V
//      GND: GND
//      DATA: 2
int pinDHT11 = 2;
SimpleDHT11 dht11;

void setup() {
  Serial.begin(9600);
}

void loop() {
  // start working...
  Serial.println("=================================");
  Serial.println("Sample DHT11...");
  
  // read without samples.
  byte temperature = 0;
  byte humidity = 0;
  if (dht11.read(pinDHT11, &temperature, &humidity, NULL)) {
    Serial.print("Read DHT11 failed.");
    return;
  }
  
  Serial.print("Sample OK: ");
  Serial.print((int)temperature); Serial.print(" *C, "); 
  Serial.print((int)humidity); Serial.println(" %");
  
  // DHT11 sampling rate is 1HZ.
  delay(1000);
}

Here's a sample of the output:

ead DHT11 failed.=================================
Sample DHT11...
Read DHT11 failed.=================================
Sample DHT11...
Read DHT11 failed.=================================
Sample DHT11...
Read DHT11 failed.=================================
Sample DHT11...
Sample OK: 18 *C, 50 %
=================================
Sample DHT11...
Read DHT11 failed.=================================
Sample DHT11...
Read DHT11 failed.=================================
Sample DHT11...
Read DHT11 failed.=================================
Sample DHT11...
Read DHT11 failed.=================================
Sample DHT11...
Read DHT11 failed.=================================
Sample DHT11...
Read DHT11 failed.=================================
Sample DHT11...
Read DHT11 failed.=================================
Sample DHT11...
Read DHT11 failed.=================================
Sample DHT11...
Read DHT11 failed.=================================
Sample DHT11...
Read DHT11 failed.=================================
Sample DHT11...
Read DHT11 failed.=================================
Sample DHT11...
Read DHT11 failed.=================================
Sample DHT11...
Sample OK: 17 *C, 48 %
=================================
Sample DHT11...

Why is it failing so often?

Also, where could i get more info on the SimpleDHT11 class. I can find very little info on it. I want to know more about the four components in the read function. I'm probably using the wrong terms here.

As you may have guessed I'm only starting out so dumb down the questions/comments!

You have the SimpleDHT11 library. You can read the source code to see what it is doing. If they provide examples you can read those to see how the library is typically used.

Maybe the connection is loose?

Thanks John, I haven't been able to get consistent reliable results from the device, I suspect it may be faulty.

But in relation to the code, does "return;" in the "if" statement below restart the loop function as opposed to having an affect on the "if" statement itself?

Finally, when is the "if" statement true? I would have thought that if data was returned then the "if" statement would be true but it appears to be the other way around. Why so?

Because the sensor is so unreliable it's hard for me to test it.

if (dht11.read(pinDHT11, &temperature, &humidity, data)) {
   Serial.print("Read DHT11 failed.");
   return;
 }

SimpleDHL11.h:

#include <Arduino.h>

/*
    Simple DHT11

    Simple, Stable and Fast DHT11 library.

    The circuit:
    * VCC: 5V or 3V
    * GND: GND
    * DATA: Digital ping, for example, 2.

    23 Jan 2016 By winlin <winlin@vip.126.com>

    https://github.com/winlinvip/SimpleDHT#usage

*/
class SimpleDHT11 {
public:
    // to read from dht11.
    // @param pin the DHT11 pin.
    // @param ptemperature output, NULL to igore.
    // @param phumidity output, NULL to ignore.
    // @param pdata output 40bits sample, NULL to ignore.
    // @remark the min delay for this method is 1s.
    int read(int pin, byte* ptemperature, byte* phumidity, byte pdata[40]);
private:
    // confirm the OUTPUT is level in us, 
    // for example, when DHT11 start sample, it will
    //    1. PULL LOW 80us, call confirm(pin, 80, LOW)
    //    2. PULL HIGH 80us, call confirm(pin, 80, HIGH)
    // @return 0 success; oterwise, error.
    // @remark should never used to read bits, 
    //  for function call use more time, maybe never got bit0.
    // @remark please use simple_dht11_read().
    int confirm(int pin, int us, byte level);
    // @data the bits of a byte.
    // @remark please use simple_dht11_read().
    byte bits2byte(byte data[8]);
    // read temperature and humidity from dht11.
    // @param pin the pin for DHT11, for example, 2.
    // @param data a byte[40] to read bits to 5bytes.
    // @return 0 success; otherwise, error.
    // @remark please use simple_dht11_read().
    int sample(int pin, byte data[40]);
    // parse the 40bits data to temperature and humidity.
    // @remark please use simple_dht11_read().
    int parse(byte data[40], byte* ptemperature, byte* phumidity);
};

#endif

Thanks.

Padraig_:
Why is it failing so often?

Maybe you forgot to connect a pull-up resistor between VCC and data pin?

I tried it with and without the resistor and it didn't appear to make a difference. Although on the photo from the supplier there was no resistor.