In compliance with high industry standards

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


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


  • 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


void setup() {

void loop() {
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(" %t");
Serial.print("Temperature: ");
Serial.print(" *C ");
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! :)

Looking for your ideal PLC?

Take a look at this product comparison with other industrial controllers.

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.

PLC comparison >>

Do you want more information?

Just fill the form!

Tell me more!