Temperature and Humidity Sensor On An Arduino
Temperature and Humidity Sensor On An Arduino
on an Arduino
Though this tutorial is based exclusively on the usage of Temperature and humidity sensors or
just a temperature sensor such as the KY-028, other tutorials may make usage of these
temperature sensors associated with other sensors that may be the focus of those tutorials
A. DHT11
Introduction
DHT11 vs DHT22
There are two versions of the DHT sensor, which look a bit similar and have the same pinout, but
have different characteristics. Here are the specifications:
DHT11
In the wired version (PCB), the board includes a surface mounted 10K Ohm pull up resistor
for the signal line (between the signal line and the VCC) to keep the signal level high by
default.
• Low cost
• 3 to 5V power and I/O
• 2.5mA max current use during conversion (while requesting data)
• Good for 0-100% humidity readings with 2-5% accuracy
• Good for -40 to 80°C temperature readings ±0.5°C accuracy
• No more than 0.5 Hz sampling rate (once every 2 seconds)
• Body size 15.1mm x 25mm x 7.7mm
• 4 pins with 0.1" spacing
It is possible that the wired version of the DHT22 follows the protocol of assigning the middle pin to the
signal line (however, in the case of this 3-pin version, VCC is the middle pin, ground is the left pin, and
data is the right pin). Make sure you verify the pinout of your sensor.
As seen from the specifications, the DHT22 / AM2302 is more accurate and better over a larger
range of temperatures, and the complete range of relative humidity. Both use a single digital pin
and are slow in that you can't query them more than once every second or two; in fact, the more
performing DHT22 is slower as measurements can only be obtained every 2 seconds, rather than
1 second for the DHT11.
Relative Humidity?
There are 3 definitions of humidity: absolute humidity, relative humidity, and specific humidity.
We will be interested in relative humidity since this is what the DHT11 measures.
Wikipedia states that The relative humidity (RH or Φ) of an air-water mixture is defined as the
ratio of the partial pressure of water vapor pH O in the mixture to the equilibrium vapor pressure
2
*
of water p H 2O over a flat surface of pure water at a given temperature.
pH 2 O
F= *100%
pH* 2O
In other words, relative humidity is the amount of water vapor in the air as a percentage of the
saturation point of water vapor in air (maximum amount of water that can be held by the air),
given the same temperature (temperature has a substantial effect on the humidity level). At the
saturation point, water vapor starts to condense and accumulate on surfaces forming dew.
Condensation, then, occurs at 100% relative humidity.
The saturation point changes with air temperature. Cold air can hold less water vapor before it
becomes saturated, and hot air can hold more water vapor before it becomes saturated.
Note how complex the steps are to display on the serial monitor data, strings, and special
characters in one line. If anybody can find a simpler way, please suggest it as we need the code
to be simpler and shorter.
After it’s installed, upload this example program to the Arduino and open the serial monitor:
#include <dht.h>
#include <LiquidCrystal.h>
dht DHT;
#define DHT11_PIN 6
void setup(){
Serial.begin(9600);
lcd.begin(16, 2);
void loop(){
Serial.print("Temperature = ");
Serial.print(DHT.temperature);
Serial.write(0xC2);
Serial.write(0xB0);
Serial.print("C ");
Serial.print("Humidity = ");
Serial.print(DHT.humidity);
Serial.write(0x25);
Serial.println("");
lcd.setCursor(0,0);
lcd.print("Temp: ");
lcd.print(DHT.temperature*9/5+32);
lcd.print((char)223);
lcd.print("F");
lcd.setCursor(0,1);
lcd.print("Humidity: ");
lcd.print(DHT.humidity);
lcd.print("%");
delay(2000);
The chapter that deals with the case of the Ultrasonic Range Finder on an Arduino will show
how to use the readings from the DHT11 sensor to improve the accuracy of the evaluation of the
distance between the sensors and an obstacle.
KY-028 Digital Temperature Sensor Module
The digital temperature sensor KY-028 for Arduino measures temperature variations through the
variations of the resistance of a thermistor. A potentiometer is used to adjust the detection
threshold on the digital interface.
KY-028 Specifications
The KY-028 consists of a NTC (Negative Temperature Coefficient) thermistor, an LM393 dual
differential comparator, a 3296W trimmer potentiometer, six resistors and two indicator LEDs.
The board features an analog and a digital output.
KY-028 Arduino
A0 A0
G GND
+ 5V
D0 2
KY-028 Arduino Code
When the temperature threshold is reached, the digital interface will send a HIGH signal turning
on the LED on the Arduino (pin 13). Turn the potentiometer clock-wise to increase the detection
threshold and counter-clockwise to decrease it.
The analog interface returns a numeric value that depends on the temperature and the
potentiometer's position.
#include <ACI_10K_an.h>
void setup()
{
pinMode(led, OUTPUT);
pinMode(digitalPin, INPUT);
//pinMode(analogPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
// Read the digital interface
digitalVal = digitalRead(digitalPin);
if(digitalVal == HIGH) // if temperature threshold reached
{
digitalWrite(led, HIGH); // turn ON Arduino's LED
}
else
{
digitalWrite(led, LOW); // turn OFF Arduino's LED
}