How to Use XHT11 Temperature and Humidity Sensor?
1.Introduction
In winter, the humidity in the air is very low, that is, the air is very dry. Coupled with the cold, the human skin is prone to crack from excessive dryness. Therefore, you need to use a humidifier to increase the humidity of the air at home. But how do you know that the air is too dry? Then you need equipment to detect air humidity.
In this lesson, we will learn how to use the XHT11 temperature and humidity sensor. We use the sensor to create a thermohygrometer and also combined with an LCD_128X32_DOT to display the temperature and humidity values.
2.Components Required
|
|
|
|
|
---|---|---|---|---|
Keyestudio Plus Mainboard*1 |
Temperature and Tumidity sensor*1 |
LCD_128X32_DOT*1 |
F-F Dupont Wires |
USB Cable*1 |
3.Component Knowledge
XHT11 temperature and humidity sensor:
It is a temperature and humidity composite sensor with calibrated digital signal output. Its accuracy humidity is ±5%RH, temperature is ±2℃. Range humidity is 20 to 90%RH, and temperature is 0 to 50℃. The XHT11 temperature and humidity sensor applies dedicated digital module acquisition technology and temperature and humidity sensing technology to ensure extremely high reliability and excellent long-term stability of the product.
The XHT11 temperature and humidity sensor includes a resistive-type humidity measurement and an NTC temperature measurement component, which is very suitable for temperature and humidity measurement applications where accuracy and real-time performance are not required.
The operating voltage is in the range of 3.3V to 5.5V. XHT11 has three pins, which are VCC, GND, and S. S is the pin for data output, using serial communication.
Single bus format definition:
Description |
Definition |
---|---|
Start signal |
Microprocessor pulls data bus (SDA) down at least 18ms for a period of time(Maximum is 30ms), telling the sensor to prepare data. |
Response signal |
The sensor pulls the data bus (SDA) low for 83µs, and then pulls up for 87µs to respond to the host’s start signal. |
Humidity |
The high humidity is an integer part of the humidity data, and the low humidity is a fractional part of the humidity data. |
Temperature |
The high temperature is the integer part of the temperature data, the low temperature is the fractional part of the temperature data. And the low temperature Bit8 is 1, indicating a negative temperature, otherwise, it is a positive temperature. |
Parity bit |
Parity bit=Humidity high bit+ Humidity low bit+temperature high bit+temperature low bit |
Data sequence diagram:
When MCU sends a start signal, XHT11 changes from the low-power-consumption mode to the
high-speed mode, waiting for MCU completing the start signal. Once it is completed, XHT11 sends a response signal of 40-bit data and triggers a signal acquisition. The signal is sent as shown in the figure.
Combined with the code, you can understand better.
The XHT11 temperature and humidity sensor can easily add temperature and humidity data to your DIY electronic projects. It is perfect for remote weather stations, home environmental control systems, and farm or garden monitoring systems.
Specification:
Working voltage: +5V
Temperature range: 0°C to 50°C, error of ± 2°C
Humidity range: 20% to 90% RH,± 5% RH error
Digital interface
Schematic diagram:
4.Read the Value
First we learned how to use the serial monitor to print the values of the XHT11 sensor. Please connect the wires according to the wiring diagram below.
Note:
The library file needs to be installed in the code. If the “Dht11” library file has been added, ignore the process of adding the library file below.
Decompress the library files in the folder, that is, put the decompressed “Dht11” folder into “Arduino libraries” under the compiler installation directory.
After successful placement, you need to restart the compiler, otherwise the compilation will not work.
e.g:C:\Program Files\Arduino\libraries
/*
Read_the_temperature_and_humidity_values
http//www.keyestudio.com
*/
#include "DHT.h"
#define DHTPIN 11 // connect the DHT sensor to the digital pins
DHT dht(DHTPIN, DHT11);
void setup() {
Serial.begin(9600);
Serial.println(F("DHTxx test!"));
dht.begin();
}
void loop() {
// wait for a few seconds between two measurment
delay(2000);
// It takes about 250 milliseconds to read the temperature or humidity!
//
float h = dht.readHumidity();
// the temperature is Celsius (default value)
float t = dht.readTemperature();
// Calculate the Fahrenheit temperature (isFahrenheit = true)
float f = dht.readTemperature(true);
// fail to read or not, exit(try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Calculate the Fahrenheit temperature index
float hif = dht.computeHeatIndex(f, h);
// (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(f);
Serial.print(F("°F Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));
}
Upload the code to the Plus Mainboard, connect the wires and power on first. Then open the serial monitor, set the baud rate to 9600, you will see the current temperature and humidity value detected by the XHT11 sensor.
5.Circuit Diagram and Wiring Diagram
Now we start printing the value of the XHT11 temperature and humidity sensor with LCD screen. We will see the corresponding values on the LCD screen. Let’s get started with this project. Please follow the wiring diagram below.
6.Code
Note:The library file needs to be installed in the code.If library files such as “Dht11”and “lcd” has been added, ignore the process of adding the library file below. Decompress the library files in the folder, that is, put the decompressed “Dht11” and “LCD_128X32” folder into “Arduino libraries” under the compiler installation directory.
After successful placement, you need to restart the compiler, otherwise the compilation will not work.
e.g:C:\Program Files\Arduino\libraries
/*
Temperature_and_humidity_meter
http//www.keyestudio.com
*/
#include "DHT.h"
#define DHTPIN 11 // the pin connected the DHT sensor
DHT dht(DHTPIN, DHT11);
#include //add library files
lcd Lcd; //define a Lcd class instance
void setup() {
Lcd.Init(); //initialize
Lcd.Clear(); //clear
dht.begin();
}
char string[10];
//lcd displays humidity and temperature values
void loop() {
Lcd.Cursor(0,0);
Lcd.Display("Humidity:");
Lcd.Cursor(0,9);
Lcd.DisplayNum(dht.readHumidity());
Lcd.Cursor(0,12);
Lcd.Display("%RH");
Lcd.Cursor(2,0);
Lcd.Display("Temperature:");
Lcd.Cursor(2,12);
Lcd.DisplayNum(dht.readTemperature());
Lcd.Cursor(2,15);
Lcd.Display("C");
delay(200);
}
7.Result
Upload the code to the Plus Mainboard, connect the wires and power on first. The LCD_128X32_DOT displays temperature and humidity in the current environment. We can use it as a real-time environmental monitoring tool.