Giter VIP home page Giter VIP logo

dht11's Issues

Error reading both temp and humidity

I was playing around with a DHT11 and a ESP8266 NodeMCU ESP-12E and could not get both temp and humidity to display. I ran the script with just temp and just humidity, and they worked fine. After much consternation, I eliminated the error by putting a delay(500) between each reading. I did not try any shorter delays. Here is what my sketch looked like:

#include <DHT11.h>

// Create an instance of the DHT11 class and set the digital I/O pin.
DHT11 dht11(2);

void setup() {
// Initialize serial communication at 115200 baud.
Serial.begin(115200);
}

void loop() {
// Read the temperature from the sensor.
float temperature = dht11.readTemperature();

delay(500); // Required to get both readings consecutively

// Read the humidity from the sensor.
float humidity = dht11.readHumidity();

// If the temperature and humidity readings were successful, print them to the serial monitor.
if (temperature != -1 && humidity != -1) {
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" C");

Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");

} else {
// If the temperature or humidity reading failed, print an error message.
Serial.println("Error reading data");
}

// Wait for .5 seconds before the next reading.
delay(500);
}

Enhancement: Enable fetching of both humidity and temperature at the same time

Loving the simplicity of this library. However, as the DHT11 is quite slugish and doesn't like to be polled faster than 1Hz, if you try and call readTemperature() and then readHumidity() too close together the second call returns 253.

Might this be corrected if the constructor had 2 variables say _T and _H, and a new method readSensor() was added? readSensor() contains the current content of readHumidity function but instead of returning data[0] it sets _H = data[0] and _T = data[2]

readTemperature() then becomes

readTemperature(){
return _T;
}

or similar. Would this work with the way you have designed this library? or would it break something you have planned?

The library is broken and cannot properly read humidity from the sensor.

The library is broken and can't read humidity from the sensor at all BUT it can read temperatures (and that's weird). I recorded a video (https://github.com/dhrubasaha08/DHT11/assets/43276401/c08f88ea-b9ee-4187-ba22-5e2f3c7b2dd0) showing what it looks like, using the example from the file (ReadTempAndHumidity.ino) and in the second part I used the simplest library I could find: https://github.com/adidax/dht11 and used the following code for this library:

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

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.