Home CodeArduino micro:bit and DHT11 Temperature and Humidity Sensor example

micro:bit and DHT11 Temperature and Humidity Sensor example

by shedboy71

In this example we will show a basic example of connecting a DHT11 Temperature and Humidity Sensor to your Arduino. This is a very nice little, low cost sensor which requires only a pull up resistor to VDD. This can easily be constructed on a mini bread board and then joined up to your Arduino. using some connecting cables

 

The DHT sensors are made of two parts, a capacitive humidity sensor and a thermistor. There is a chip inside that does some a/d conversion and outputs a digital signal with the temperature and humidity.

Layout

I actually used the module pictured earlier but in case you just have a sensor and a 4k7 resistor, this is what you would need to make

Code

You will need the DHT11 library from the DHT library. You could roll your own code but this saves a whole lot of time.

Download, extract and copy this to your Arduino libraries folder location. This is a simple example which outputs the humidity and temperature, place your fingers on the DHT11 and you will see the readings fluctuate

[codesyntax lang=”cpp”]

#include "DHT.h"

#define DHTPIN 3     // what pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11


DHT dht(DHTPIN, DHTTYPE);

void setup() 
{
  Serial.begin(9600);
  Serial.println("DHT11 test!");
  dht.begin();
}

void loop() 
{
  // Wait a few seconds between measurements.
  delay(2000);

  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) 
  {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }


  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.println(" *F");

}

[/codesyntax]

 

Output

Open the serial monitor and you should see something like this

Humidity: 37.00 % Temperature: 20.00 *C 68.00 *F
Humidity: 36.00 % Temperature: 21.00 *C 69.80 *F
Humidity: 36.00 % Temperature: 21.00 *C 69.80 *F
Humidity: 36.00 % Temperature: 22.00 *C 71.60 *F
Humidity: 36.00 % Temperature: 22.00 *C 71.60 *F
Humidity: 35.00 % Temperature: 23.00 *C 73.40 *F
Humidity: 35.00 % Temperature: 23.00 *C 73.40 *F
Humidity: 35.00 % Temperature: 24.00 *C 75.20 *F
Humidity: 35.00 % Temperature: 24.00 *C 75.20 *F
Humidity: 34.00 % Temperature: 25.00 *C 77.00 *F

 

 

Links

Free Shipping DHT11 Temperature and Relative Humidity Sensor Module With Cable for Arduino

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.