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 for Arduino. This sensor, also known as AM2302, 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.
DHT22 Features
Power supply from 3.3V to 6V
Current consumption 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
If you want to learn more about the DHT22 features, see this DHT22 datasheet.
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 humidity sensors.
After downloading and importing the library, you can start programming the sketch.
Temperature and Humidity reading with M-Duino 21+
This example shows 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");
}
If you want to learn more about our PLC controllers for industrial automation, subscribe to our blog. We are waiting for you! :)
DHT22 / AM2302 temperature and humidity sensor