Browse our Blog. You will find multiple applications, solutions, code examples. Navigate using the tag cloud or search using specific criteria

How to program a DHT22 temperature and humidity sensor

Introduction

In this post we will show you how to work with the industrial controller M-Duino 21+ by connecting a DHT22 temperature and humidity sensor. This sensor has an internal processor which performs the measurement process, providing you with a digital signal so it is very easy to obtain the measurement from a microcontroller such as Arduino.

How to program a DHT22 temperature and humidity sensor

Features

  • Power supply from 3.3V to 6V.

  • Current consumtion of 2.5mA.

  • Output - Digital signal.

  • Temperature measurement between -40 and 125ºC, with an accuracy of 0.5ºC to 25ºC.

  • Temperature measurement resolution: 8-bit, 0,1ºC 

  • Humidity measurement between 0 and 100%, with an accuracy of 2-5% for temperatures between 0 and 50ºC.

  • Temperature measurement resolution: 8-bit, 0,1%

  • Sampling frequency of 2 samples/s: 2Hz.

  • Connection by resistance with a value between 4.7K and 10K

How to program a DHT22 temperature and humidity sensor

DHT Library for Arduino 

In this case we are using the Adafruit library which you can download for free here.

With this library, you can easily read both sensors and not worry about the communication protocol between the Arduino based industrial PLC and the sensors. 

After downloading and importing the library, you can start programming the sketch.

Temperature and Humidity reading with M-Duino 21+

 This example show you how to read humidity and temperature (Celsius and Fahrenheit).

#include "DHT.h" #define DHTPIN 2     //Pin where is the sensor connecte

#define DHTTYPE DHT22   // Sensor DHT22

DHT dht(DHTPIN, DHTTYPE);

void setup() {
Serial.begin(9600);
Serial.println("Loading...");
dht.begin();
}

void loop() {
delay(2000);
float h = dht.readHumidity(); //Reading the humidity
float t = dht.readTemperature(); //Reading the temperature in Celsius degree
float f = dht.readTemperature(true); //Reading the temperature in Fahrenheit degrees

//--------Sending the reading through Serial port-------------

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

Temperature and Humidity reading with M-Duino 21+

If you want to learn more about our PLC controllers for industrial automation, subscribe to our blog. We are waiting for you! :)

Do you want more information?

Open Source technology allows you to develop your installations.

Just fill the form and we will contact you as soon as we can.

Send