GebraBit

HTU2xD sensor project with Arduino

متن سربرگ خود را وارد کنید

HTU20x-project-2048x1463

HTU2xD sensor project with Arduino

  1. Home
  2. »
  3. Projects
  4. »
  5. HTU2xD sensor project with Arduino

What's the purpose of this project?

The purpose of this project is to equip you with the knowledge and practical experience needed to integrate the HTU2xD sensor into your Arduino projects. By learning how to measure and interpret temperature and humidity data, you will be able to create responsive systems that monitor and react to environmental changes, which can be applied to various fields such as weather stations, smart home automation, and agricultural monitoring.

What are we going to learn in this tutorial?

In this tutorial, you’ll learn how to:

  • Connect the HTU2xD (HTU21D/HTU31D) temperature and humidity sensor to an Arduino and establish I2C communication.

  • Utilize an existing library for HTU2xD compatibility with Arduino, gaining insights into I2C data handling.

  • Measure temperature in °C and humidity in %RH using the sensor, interpreting the data for practical applications.

  • Implement sensor-based projects for environmental monitoring, building skills to create responsive systems based on temperature and humidity levels.

This hands-on guide provides insights into sensor integration and real-time data monitoring with Arduino.

What do we need to start this project?

As you probably know, we need some hardware and software to do this project. The titles of these hardware and software are provided to you in the table below and you can prepare/download by clicking on each of them and get ready to start.

Required hardware
Required software
Arduino UNO

First as shown in the image below, we connect the GebraBit HTU20D module to the Arduino UNO as follows:

Then download and add the GebraBit HT2XD library to your Arduino IDE.

If you don’t know how to add GebraBit libraries to Arduino IDE, refer to the tutorial link below.

HTU2XD library and driver

In addition to the modular design of various sensors and ICs, GebraBit tries to provide variety of structured and hardware-independent libraries in C language for the ease of users in setting up and developing software.

For this purpose, after preparing each GebraBit module, the users can refer to the “tutorial” section of the desired module and download the dedicated library, which contains the “ .h” and “  .c” file (Header and Source) and a sample training program under “GebraBit STM32F303”, “GebraBit ATMEGA32A” or “Arduino” development boards.

All the defined functions and structures in the library are commented in full detail and all the received parameters in the arguments of the functions and their return values, are briefly explained. Since the libraries are hardware independed, the user can easily add the library in any of their favorite compilers and develop it by desired microcontroller and development board.

GebraBit_HTU2XD.h header file

In this file, based on the datasheet of the sensor or IC, all address registers, the values of each register are defined in the form of “Enumeration”. Also, the casing of the HTU2XD sensor and the configurations related to each of the HTU2XD sensor internal blocks are defined in the form of a “STRUCT” with the name GebraBit_HTU2XD.     

USER REGISTER MAP

The registry map or sensor commands are defined in this section:

				
					#define HTU2XD_ADDRESS                                       0x40
#define HTU2XD_WRITE_USER_REGISTER_CMD                       0xE6
#define HTU2XD_READ_USER_REGISTER_CMD                        0xE7
#define HTU2XD_RESET_CMD                                     0xFE
#define HTU2XD_TRIGGER_TEMPERATURE_MEASUREMENT_CMD           0xE3  /// Hold Master
#define HTU2XD_TRIGGER_HUMIDITY_MEASUREMENT_CMD              0xE5  /// Hold Master
#define HTU2XD_USER_REGISTER_RESOLUTION_BIT_MASK             0x81

// Processing constants
#define HTU2XD_TEMPERATURE_COEFFICIENT                       (float)(-0.15)
#define HTU2XD_CONSTANT_A                                    (float)(8.1332)
#define HTU2XD_CONSTANT_B                                    (float)(1762.39)
#define HTU2XD_CONSTANT_C                                    (float)(235.66)

// Coefficients for temperature computation
#define TEMPERATURE_COEFF_MUL                                (175.72)
#define TEMPERATURE_COEFF_ADD                                (-46.85)

// Coefficients for relative humidity computation
#define HUMIDITY_COEFF_MUL                                   (125)
#define HUMIDITY_COEFF_ADD                                   (-6)

/*----------------------------------------------*
 *           USER REGISTER MAP End              *
 *----------------------------------------------*/

				
			

HTU2XD_Ability Enum

The ability to activate or deactivate different parts of the sensor is defined in this enum:

				
					typedef enum Ability
{
	Disable = 0 ,
	Enable
}HTU2XD_Ability;
				
			

HTU2XD_Battery_Status Enum

The values of this enum are used to determine the state of the sensor voltage level:

				
					1. typedef enum Battery_Status
2. {
3.  HTU2XD_BATTERY_VDD_OK,
4.  HTU2XD_BATTERY_VDD_LOW
5. }HTU2XD_Battery_Status;
6.
				
			

HTU2XD_OTP Enum

The values of this enum are used for sensor OTP settings:

				
					1. typedef enum OTP
2. {
3. 	OTP_DISABLE = 1 ,
4. 	OTP_ENABLE  = 0
5. }HTU2XD_OTP;
6.
				
			

HTU2XD_Measurement_Resolution Enum

The values of this enum are used to select the sensor resolution:

				
					1. typedef enum Measurement_Resolution
2. {
3. 	HTU2XD_HUMIDITY_12BIT_TEMPERATURE_14BIT = 0x00 ,
4. 	HTU2XD_HUMIDITY_8BIT_TEMPERATURE_12BIT  = 0x01 ,
5. 	HTU2XD_HUMIDITY_10BIT_TEMPERATURE_13BIT = 0x80 ,
6. 	HTU2XD_HUMIDITY_11BIT_TEMPERATURE_11BIT = 0x81
7. }HTU2XD_Measurement_Resolution;
8.
				
			

HTU2XD_Humidity_Conversion_Time Enum

The values of this enum are used to select the conversion time of humidity values:

				
					typedef enum Humidity_Conversion_Time
{
	HUMIDITY_12BIT_MEASUREMENT_TIME = 16,
	HUMIDITY_11BIT_MEASUREMENT_TIME = 8 ,
	HUMIDITY_10BIT_MEASUREMENT_TIME = 5 ,
	HUMIDITY_8BIT_MEASUREMENT_TIME  = 3
}HTU2XD_Humidity_Conversion_Time;
				
			

HTU2XD_Temperature_Conversion_Time Enum

The values of this enum are used to select the conversion time of temperature values:

				
					1. typedef enum Temperature_Conversion_Time
2. {
3. 	TEMPERATURE_14BIT_MEASUREMENT_TIME = 50 ,
4. 	TEMPERATURE_13BIT_MEASUREMENT_TIME = 25 ,
5. 	TEMPERATURE_12BIT_MEASUREMENT_TIME = 13 ,
6. 	TEMPERATURE_11BIT_MEASUREMENT_TIME = 7
7. }HTU2XD_Temperature_Conversion_Time;
8.
				
			

HTU2XD_CRC_Status Enum

Using this enum, the status of the CRC check is specified:

				
					1. typedef enum CRC_Status
2. {
3. 	CRC_ERROR = 0,
4. 	CRC_OK
5. }HTU2XD_CRC_Status;
6.
				
			

HTU2XD_Reset_Status Enum

By using this enum, the reset status of the sensor is specified:

				
					1. typedef enum
2. {
3. 	FAILED = 0 ,
4. 	DONE
5. }HTU2XD_Reset_Status;
6.
				
			

HTU2XD struct

All sensor properties, calibration coefficients and sensor data are defined in this “struct” and All the information and configuration implemented on the sensor are stored in this “structure” and you can see the changes in each part of the sensor in the “Debug Session” environment.

				
					1. typedef	struct HTU2XD
 2. {
 3. 	  uint8_t                       	   Register_Cache;
 4. 	  HTU2XD_Reset_Status				   RESET;
 5. 	  HTU2XD_Battery_Status                BATTERY_VDD;
 6. 	  HTU2XD_OTP				           OTP;
 7. 	  HTU2XD_Ability					   ON_CHIP_HEATER;
 8. 	  HTU2XD_Measurement_Resolution        MEASUREMENT_RESOLUTION;
 9. 	  HTU2XD_Humidity_Conversion_Time      HUMIDITY_MEASUREMENT_TIME;
10. 	  HTU2XD_Temperature_Conversion_Time   TEMPERATURE_MEASUREMENT_TIME;
11. 	  uint8_t                              ADC_TEMPERATURE[REGISTER_DATA_BUFFER_SIZE];
12. 	  uint16_t                             ADC_TEMPERATURE_DATA;
13. 	  uint8_t                              ADC_HUMIDITY[REGISTER_DATA_BUFFER_SIZE];
14. 	  uint16_t							   ADC_HUMIDITY_DATA;
15. 	  uint8_t 							   HTU2XD_CRC;
16. 	  HTU2XD_CRC_Status 				   CRC_CHECK;
17.       float 							   TEMPERATURE;
18. 	  float 							   HUMIDITY;
19. 	  float 							   COMPANSATED_HUMIDITY;
20. //	  double							   PARTIAL_PRESSURE;
21. //	  double 							   DEW_POINT;
22. }GebraBit_HTU2XD;
23.
				
			

Declaration of functions

At the end of this file, all the functions for reading and writing in HTU2XD registers, sensor configuration and receiving data from the sensor are declared:

				
					1. /********************************************************
 2.  *  Declare Read&Write HTU2XD Register Values Functions *
 3.  ********************************************************/
 4. extern void GB_HTU2XD_Read_User_Register(uint8_t *data)		;
 5. extern void GB_HTU2XD_Burst_Read(uint8_t regAddr,  uint8_t *data, uint16_t byteQuantity);
 6. extern void GB_HTU2XD_Read_User_Register_Bits ( uint8_t start_bit, uint8_t len, uint8_t* data);
 7. extern void GB_HTU2XD_Write_User_Register(uint8_t data)	;
 8. extern void GB_HTU2XD_Burst_Write(uint8_t regAddr,  uint8_t *data, uint16_t byteQuantity)								;
 9. extern void GB_HTU2XD_Write_User_Register_Bits( uint8_t start_bit, uint8_t len, uint8_t data);
10. /********************************************************
11.  *       Declare MS5611 Configuration Functions         *
12.  ********************************************************/
13. extern void GB_HTU2XD_Soft_Reset ( GebraBit_HTU2XD * HTU2XD )  ;
14. extern void GB_HTU2XD_Check_Battery_Voltage_VDD ( GebraBit_HTU2XD * HTU2XD  ) ;
15. extern void GB_HTU2XD_On_Chip_Heater ( GebraBit_HTU2XD * HTU2XD , HTU2XD_Ability heater )  ;
16. extern void GB_HTU2XD_Read_On_Chip_Heater_Status ( GebraBit_HTU2XD * HTU2XD , HTU2XD_Ability heater )    ;
17. extern void GB_HTU2XD_OTP ( GebraBit_HTU2XD * HTU2XD , HTU2XD_OTP otp )  ;
18. extern void GB_HTU2XD_Read_OTP ( GebraBit_HTU2XD * HTU2XD , HTU2XD_OTP otp ) ;
19. extern void GB_HTU2XD_Measurement_Resolution ( GebraBit_HTU2XD * HTU2XD , HTU2XD_Measurement_Resolution res ) ;
20. extern void GB_HTU2XD_Read_Measurement_Resolution ( GebraBit_HTU2XD * HTU2XD   )   ;
21. extern void GB_HTU2XD_CRC_Check( GebraBit_HTU2XD * HTU2XD , uint16_t value, uint8_t crc)  ;
22. extern void GB_HTU2XD_ADC_Temperature_Raw_Data ( GebraBit_HTU2XD * HTU2XD )  ;
23. extern void GB_HTU2XD_ADC_Humidity_Raw_Data ( GebraBit_HTU2XD * HTU2XD )   ;
24. extern void GB_HTU2XD_initialize( GebraBit_HTU2XD * HTU2XD )  ;
25. extern void GB_HTU2XD_Configuration(GebraBit_HTU2XD * HTU2XD)  ;
26. extern void GB_HTU2XD_Get_Data(GebraBit_HTU2XD * HTU2XD);
27.
				
			

GebraBit_HTU2XD.c source file

In this file, which is written in C language, all the functions are commented in full detail, and all the parameters received in the arguments of the functions and their return values are clearly explained so we confine to these explanations and invite users to check this file directly for more information.

Sample program in Arduino

After connecting the module to Arduino and adding the library to the IDE, go to the following path: File > Examples > GebraBit_HTU2XD > Temp-Humid

Description of Sample file

Enums and functions required by GebraBit HTU2XD module have been added to the structures. In the next part, a variable named HTU2XD of the GebraBit_HTU2XD structure type (this structure is in the GebraBit_HTU2XD header and is explained in the GebraBit_HTU2XD library description section) is defined for the configuration of the GebraBit HTU2XD module:  

				
					GebraBit_HTU2XD HTU2XD;
				
			

In the next part of the written code, using the GB_HTU2XD_initialize (&HTU2XD) and GB_HTU2XD_Configuration (&HTU2XD) functions, we set the GebraBit HTU2XD module and finally, in the loop part of the program, the data is read from the sensor and the humidity and temperature values are continuously received:

				
					void setup() {
    Wire.begin();           // Initialize the I2C bus
    Serial.begin(9600);     // Initialize serial communication for debugging

    GB_HTU2XD_initialize(&HTU2XD); // Initialize the HTU2XD sensor
    GB_HTU2XD_Configuration(&HTU2XD); // Configure the HTU2XD sensor
}

void loop() {
    GB_HTU2XD_Get_Data(&HTU2XD); // Read data from the sensor

    Serial.print("Temperature: ");
    Serial.print(HTU2XD.TEMPERATURE);
    Serial.println(" °C");

    Serial.print("Humidity: ");
    Serial.print(HTU2XD.HUMIDITY);
    Serial.println(" %");

    delay(2000); // Delay between readings
}
				
			

The Sample file code text:

				
					#include "GebraBit_HTU2XD.h"

GebraBit_HTU2XD HTU2XD;

void setup() {
    Wire.begin();           // Initialize the I2C bus
    Serial.begin(9600);     // Initialize serial communication for debugging

    GB_HTU2XD_initialize(&HTU2XD); // Initialize the HTU2XD sensor
    GB_HTU2XD_Configuration(&HTU2XD); // Configure the HTU2XD sensor
}

void loop() {
    GB_HTU2XD_Get_Data(&HTU2XD); // Read data from the sensor

    Serial.print("Temperature: ");
    Serial.print(HTU2XD.TEMPERATURE);
    Serial.println(" °C");

    Serial.print("Humidity: ");
    Serial.print(HTU2XD.HUMIDITY);
    Serial.println(" %");

    delay(2000); // Delay between readings
}

				
			

Connect your arduino to computer and select your Arduino Board

Then Verify and Upload the Sample code

After uploading the code, open the serial monitor and you can see the sensor values.

In the following, you can download the GebraBit_HTU2XD Library, the schematic of the modules and the “HTU2XD datasheet”.

Program output video

The video of the module operation will be uploaded soon

این مقاله را با دوستانتان به اشتراک بگذارید!

Be the first to write a review

Please help the Gebra team to improve the quality by sending comments and ratings

Your email address will not be published. Required fields are marked *

Shopping cart
Start typing to see posts you are looking for.

Sign in

No account yet?