Interfacing dht11 (temperature and Humidity Sensor) with arduino

Interfacing DHT11 (Temperature and Humidity Sensor) with Arduino

Interfacing Temperature and Humidity Sensor (DHT11) with Arduino

DHT11 is a digital temperature and Humidity sensor. The sensor comes in 4 pin package as well as 3 pin board module.

Interfacing 4-pin dht11 (temperature and Humidity Sensor) with arduino
4-pin Sensor
Interfacing 3-pin dht11 (temperature and Humidity Sensor) with arduino
3-pin Sensor

Connections:

4-pin dht11 pin diagram

4-pin DHT113-pin DHT11Arduino UNO/ Mega
VCCVCC5V
GNDGNDGND
DATAS or Signal2

Interfacing dht11 (temperature and Humidity Sensor) with arduino

 

Library Installation on Arduino IDE:

New versions of Arduino IDE (software) contains builtin libraries support. The user just needs to select a particular library and click Install. See step by step, how to add a DHT11 library.

Step # 01: In Arduino IDE, select Sketch > Include Library > Manage Libraries

Arduino Library Manager to install libraries in arduino

Arduino Library Manager to install libraries in arduino

Step # 02: Write DHT in search box

Arduino Library Manager to install libraries in arduino

Step # 03: You will see two or more libraries in the box, install the “DHT Sensor Library” and “SimpleDHT” both libraries, if already installed, update them to latest version.

 

Execute the code:

Copy the following code and upload it to Arduino.

#include <SimpleDHT.h>

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

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

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

// DHT sampling rate is 1HZ.
delay(2500);
}

Results/ Output:

Interfacing dht11 (temperature and Humidity Sensor) with arduino

Arduino code that doesn’t show errors:

#include <SimpleDHT.h>

int pinDHT11 = 2;
SimpleDHT11 dht11;

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

void loop() {
byte temperature = 0;
byte humidity = 0;
int err = SimpleDHTErrSuccess;
if ((err = dht11.read(pinDHT11, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
return;
}

Serial.print((int)temperature); Serial.print(” *C, “);
Serial.print((int)humidity); Serial.println(” H”);

// DHT sampling rate is 1HZ.
delay(2500);
}

Results/  Output:

Interfacing dht11 (temperature and Humidity Sensor) with arduino

One comment

  1. This code REALLY made my day. My DHT works like a charm.

Leave a Reply

Your email address will not be published. Required fields are marked *