X
Home > Blog > STEM for ESP32 > HOW to Get Temperature And Humidity Value with ESP32

HOW to Get Temperature And Humidity Value with ESP32

By r December 27th, 2024 141 views

1. Overview

KD2095

XHT11 sensor is a low-cost and entry-level temperature and humidity sensor, which consists of a resistive humidity sensor and an NTC temperature sensor. It is designed 4-pin single-row pin and adopts single-wire serial interface, so we just add the appropriate pull-up resistance to read values. Moreover, its signal transmission distance can reach more than 20 meters.

2. Parameters

Operating voltage: DC 3.3 ~ 5V
Operating current: 2.1 mA
Maximum power: 0.015 W
Humidity detection range: 5 ~ 95%RH (accuracy of ±5%RH under 25°C)
Temperature detection range: -25°C ~ +60°C
Operating temperature: -10°C ~ +50°C
Input signal: Digital signal
Dimensions: 48 x 24 x 18 mm (without housing)
Positioning hole: Diameter of 4.8 mm
Interface: Telephone socket

3. Principle

XHT11 adopts single-bus digital signal transmission, that is, data input and output at a pin. When the host sends a starting signal through this pin, XHT11 begins to transmit temperature and humidity data back to the host in the form of timing signals that are represented by a specific time intervals and signal levels.

Operation process:

A complete data transmission is 40 bits, with high bit outgoing first.

Data format: 8bit humidity integers + 8bit humidity decimals + 8bit temperature integers + 8bit temperature decimals + 8bit checksums

When transmitting correctly, checksum equals the last 8 digits of the results of “8bit humidity integers + 8bit humidity decimals + 8bit temperature integers + 8bit temperature decimals”.

Once the user MCU sends a starting signal, XHT11 switches from low-power mode to high-speed mode. After the starting signal ends, XHT11 sends a response signal. It outputs 40bit data and triggers a signal acquisition. In this case, the user can choose to read part of the data.

At Slave mode, XHT11 triggers a temperature and humidity collection after receiving the starting signal. After that, it will be back to low-speed mode.

XHT11 does not take the initiative to collect the temperature and humidity if it receives no starting signal from the host.

Communication process:

a26

The idle state of the bus is high, so the host pulls it down to wait for the XHT11 response. Note that this pull-down must last at least 18ms to ensure the starting signal is detected by XHT11.

After receiving the starting signal from the host and waiting for its end, XHT11 sends 80us low level response signals.

After sending the starting signal, the host waits 20-40us and then reads the response signal from XHT11, and it can switch to input mode or output high. Meanwhile, the bus is pulled up by a pull-up resistor.

a27

The bus is at low, indicating that XHT11 sends a response signal. After that, the bus is pulled up 80us and ready to send data.

Each bit of data starts with a 50us low time slot, and the length of the high level determines whether the data bit is 0 or 1. If the read response signal is high but XHT11 is not responding, please check wiring is connected properly.

When the last bit of data has been transmitted, XHT11 pulls down the bus by 50us. Subsequently, the bus is pulled up by a pull-up resistor to enter the idle state.

4. Wiring Diagram

The XHT11 temperature and humidity sensor is a digital module. It needs to be connected to a blue socket of the main board.

IOT-blue

Here we connect the module to socket 4. From the port view of the kidsIOT board, socket 4 is the digital port io27.

b7

5. Test Code

Before uploading code, import the library file of the temperature and humidity sensor to the Arduino IDE first, otherwise an error will be reported.

/*  
  Project name: 10_xht11
  Function: Read the analog value of the sound sensor
  Author: keyestudio    
  Hardware connection:  
    - Connect to port 4 
  Library:  
    - DHT-sensor-library-master
  Cautions:  
    - Connect to port 4   
    - Before uploading test code, please correctly connect to the development board and port
    - Set the baud rate to 9600 before using serial monitor printing
    - Before uploading test code, the library must be imported
*/

#include 
//Define the XHT11 read data pin as IO27
#define xht11_Pin 27  
//Create an instance of the DHT class named dht. The DHT class is provided by the DHT.h library for communication with the DHT11(XHT11) sensor.
//XHT11 is fully compatible with DHT11's code
DHT dht(xht11_Pin,DHT11)

void setup() {
  //Set the baud rate
  Serial.begin(9600);
  //Initialize the XHT11 sensor
  dht.begin();
}

void loop() {
  // Reading  humidity
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);  

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print("   Temperature: ");
  Serial.print(t);
  Serial.print("°C ");
  Serial.print(f);
  Serial.println("°F");
  // Wait a few seconds between measurements.
  delay(2000);
}

6. Test Result

After uploading the test code, open the serial monitor and set the baud rate to 9600, and the serial monitor prints the XHT11 sensor values, including humidity and temperature in degrees Celsius and Fahrenheit. These results refreshes every 2 seconds.

a28

7. Code Explanation

7.1 #include 

#include calls the DHT.h library file so that we can use the functions in it.

7.2 DHT dht(xht11_Pin,DHT11);

This code creates an instance of the DHT class named dht, which is used to input the pin and type of sensor. It is provided by DHT library that communicates with DHT11/DHT22. Here we adopts XHT11, so we enter DHT11 in the second variable (XHT11 and DHT11 code are shared).

7.3 dht.begin();

This code initializes the XHT11 sensor, checks whether the sensor connection is normal and whether the communication is ready.

7.4 float h = dht.readHumidity();

This is a function that reads the humidity. In this code, the humidity value is assigned to a float variable called h.

float represents data with decimals, while int is with only integers.

Please refer to the official website for details: Language Reference | Arduino Documentation

a29

7.4 float t = dht.readTemperature();

This is a function that reads the temperature. In this code, the temperature value is assigned to a float variable called t, whose unit is degree Celsius.

7.5 float f = dht.readTemperature(true);

It also reads the temperature value. Yet it is different from reading the temperature value above. There is a true in parentheses, so that the units of temperature are degrees Fahrenheit.

7.6 isnan()

isnan is a Boolean function that is used to determine whether the given value is NaN. NaN stands for a non-number and usually occurs in an arithmetic exception, such as dividing by zero or calculating infinity.

HOW to Detect Sound by Microphone with ESP32
Previous
HOW to Detect Sound by Microphone with ESP32
Read More
Comprehension! HOW to Monitor Smart Home Environment with ESP32
Next
Comprehension! HOW to Monitor Smart Home Environment with ESP32
Read More
Message Us