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 HTU31D 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 HTU31D (HTU21D/HTU31D) temperature and humidity sensor to an Arduino and establish I2C communication.
Utilize an existing library for HTU31D 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 HTU31D module to the Arduino UNO as follows:
Then download and add the GebraBit HTU31D library to your Arduino IDE.
Required Library
|
---|
If you don’t know how to add GebraBit libraries to Arduino IDE, refer to the tutorial link below.
HTU31D 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_HTU31D.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 HTU31D sensor and the configurations related to each of the HTU31D sensor internal blocks are defined in the form of a “STRUCT” with the name GebraBit_ HTU31D. Finally, in the Debug Session environment, all the configurations related to each block can be seen in real time.
USER REGISTER MAP
The registry map or sensor commands are defined in this section:
/************************************************
* USER REGISTER MAP *
************************************************/
#define HTU31D_ADDRESS 0x40
#define HTU31D_CONVERSION 0x40
#define HTU31D_READ_TEMPERATURE_HUMIDITY 0x00
#define HTU31D_READ_HUMIDITY 0x10
#define HTU31D_RESET 0x1E
#define HTU31D_HEATER_ON 0x04
#define HTU31D_HEATER_OFF 0x02
#define HTU31D_READ_SERIAL_NUMBER 0x0A
#define HTU31D_READ_DIAGNOSTIC 0x08
// Processing constants
#define HTU31D_TEMPERATURE_COEFFICIENT (float)(-0.15)
#define HTU31D_CONSTANT_A (float)(8.1332)
#define HTU31D_CONSTANT_B (float)(1762.39)
#define HTU31D_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 *
*----------------------------------------------*/
HTU31D_Ability Enum
The ability to activate or deactivate different parts of the sensor is defined in this enum:
typedef enum Ability
{
Disable = 0 ,
Enable
}HTU31D_Ability;
HTU31D_Action Enum
The values of this enum are used to activate and deactivate different parts of the sensor:
1. typedef enum Action
2. {
3. DEACTIVE = 0,
4. ACTIVE = 1
5. }HTU31D_Action;
HTU31D_ Humidity_OSR Enum
The values of this enum are used for the humidity sensor OSR settings:
1. typedef enum Humidity_OSR
2. {
3. HUMIDITY_OSR_0 = 0x00 ,
4. HUMIDITY_OSR_1 = 0x08 ,
5. HUMIDITY_OSR_2 = 0x10 ,
6. HUMIDITY_OSR_3 = 0x18
7. }HTU31D_Humidity_OSR;
8.
HTU31D_ Temperature_OSR Enum
The values of this enum are used for the temperature sensor OSR settings:
1. typedef enum Temperature_OSR
2. {
3. TEMPERATURE_OSR_0 = 0x00 ,
4. TEMPERATURE_OSR_1 = 0x02 ,
5. TEMPERATURE_OSR_2 = 0x04 ,
6. TEMPERATURE_OSR_3 = 0x06
7. }HTU31D_Temperature_OSR;
8.
HTU31D_Humidity_Conversion_Time Enum
The values of this enum are used to select the humidity values conversion time:
1. typedef enum Humidity_Conversion_Time
2. {
3. HUMIDITY_OSR_0_MEASUREMENT_TIME = 1 ,
4. HUMIDITY_OSR_1_MEASUREMENT_TIME = 2 ,
5. HUMIDITY_OSR_2_MEASUREMENT_TIME = 4 ,
6. HUMIDITY_OSR_3_MEASUREMENT_TIME = 8
7. }HTU31D_Humidity_Conversion_Time;
8.
HTU31D_Temperature_Conversion_Time Enum
The values of this enum are used to select the temperature values conversion time:
1. typedef enum Temperature_Conversion_Time
2. {
3. TEMPERATURE_OSR_0_MEASUREMENT_TIME = 2 ,
4. TEMPERATURE_OSR_1_MEASUREMENT_TIME = 4 ,
5. TEMPERATURE_OSR_2_MEASUREMENT_TIME = 7 ,
6. TEMPERATURE_OSR_3_MEASUREMENT_TIME = 13
7. }HTU31D_Temperature_Conversion_Time;
8.
HTU31D_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. }HTU31D_CRC_Status;
6.
HTU31D_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. }HTU31D_Reset_Status;
6.
HTU31D 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. /*************************************************
2. * Defining HTU31D Register & Data As Struct *
3. **************************************************/
4. typedef struct HTU31D
5. {
6. uint8_t Register_Cache;
7. HTU31D_Reset_Status RESET;
8. uint32_t SERIAL_NUMBER;
9. uint8_t DIAGNOSTIC;
10. HTU31D_Action NVM_CRC_ERROR;
11. HTU31D_Action HUMIDITY_UNDER_OVER_RUN;
12. HTU31D_Action HUMIDITY_ABOVE_120RH_ERROR;
13. HTU31D_Action HUMIDITY_BELOW_10RH_ERROR ;//-10
14. HTU31D_Action TEMPERATURE_UNDER_OVER_RUN;
15. HTU31D_Action TEMPERATURE_ABOVE_150_ERROR;
16. HTU31D_Action TEMPERATURE_BELOW_50_ERROR ;//-50
17. HTU31D_Heater ON_CHIP_HEATER;
18. HTU31D_Humidity_OSR HUMIDITY_OSR;
19. HTU31D_Temperature_OSR TEMPERATURE_OSR;
20. HTU31D_Humidity_Conversion_Time HUMIDITY_CONVERSION_TIME;
21. HTU31D_Temperature_Conversion_Time TEMPERATURE_CONVERSION_TIME;
22. uint8_t ADC_RAW_DATA[ADC_RAW_DATA_BUFFER_SIZE];
23. uint16_t RAW_TEMPERATURE;
24. uint16_t RAW_HUMIDITY;
25. uint8_t HTU31D_CRC;
26. HTU31D_CRC_Status CRC_CHECK;
27. float TEMPERATURE;
28. float HUMIDITY;
29. // double PARTIAL_PRESSURE;
30. // double DEW_POINT;
31. }GebraBit_HTU31D;
32.
Declaration of functions
At the end of this file, all the functions for reading and writing in HTU31D registers, sensor configuration and receiving data from the sensor are declared:
1. /********************************************************
2. * Declare Read&Write HTU31D Register Values Functions *
3. ********************************************************/
4. extern void GB_HTU31D_Write_Command( uint8_t cmd);
5. /********************************************************
6. * Declare HTU31D Configuration Functions *
7. ********************************************************/
8. extern void GB_HTU31D_Soft_Reset ( GebraBit_HTU31D * HTU31D ) ;
9. extern void GB_HTU31D_CRC_Check( GebraBit_HTU31D * HTU31D , uint16_t value, uint8_t crc) ;
10. extern void GB_HTU31D_On_Chip_Heater ( GebraBit_HTU31D * HTU31D , HTU31D_Heater heater ) ;
11. extern void GB_HTU31D_Read_Serial_Number ( GebraBit_HTU31D * HTU31D ) ;
12. extern void GB_HTU31D_Read_Diagnostic ( GebraBit_HTU31D * HTU31D ) ;
13. extern void GB_HTU31D_Humidity_OSR ( GebraBit_HTU31D * HTU31D , HTU31D_Humidity_OSR osr ) ;
14. extern void GB_HTU31D_Temperature_OSR ( GebraBit_HTU31D * HTU31D , HTU31D_Temperature_OSR osr ) ;
15. extern void GB_HTU31D_Configuration(GebraBit_HTU31D * HTU31D) ;
16. extern void GB_HTU31D_Start_Conversion ( GebraBit_HTU31D * HTU31D ) ;
17. extern void GB_HTU31D_Read_Raw_Temperature_Humidity( GebraBit_HTU31D * HTU31D ) ;
18. extern void GB_HTU31D_Temperature ( GebraBit_HTU31D * HTU31D ) ;
19. extern void GB_HTU31D_Humidity ( GebraBit_HTU31D * HTU31D ) ;
20. extern void GB_HTU31D_Dew_Point( GebraBit_HTU31D * HTU31D ) ;
21. extern void GB_HTU31D_initialize( GebraBit_HTU31D * HTU31D ) ;
22. extern void GB_HTU31D_Get_Data(GebraBit_HTU31D * HTU31D);
23.
GebraBit_HTU31D.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_HTU31D > Temp-Humid
Description of Sample file
Enums and functions required by GebraBit HTU31D module have been added to the structures. In the next part, a variable named HTU31D_Module of the GebraBit_HTU31D structure type (this structure is in the GebraBit_ HTU31D header and is explained in the GebraBit_HTU31D library description section) is defined for the configuration of the GebraBit HTU31D module:
GebraBit_HTU31D HTU31D;
In the next part of the written code, using the GB_HTU31D_initialize (&HTU31D) and GB_HTU31D_Configuration (&HTU31D) functions, we set the GebraBit HTU31D module and finally, in the while 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_HTU31D_initialize(&HTU31D); // Initialize the HTU31D sensor
GB_HTU31D_Configuration(&HTU31D); // Configure the HTU31D sensor
}
void loop() {
GB_HTU31D_Get_Data(&HTU31D); // Read data from the sensor
Serial.print("Temperature: ");
Serial.print(HTU31D.TEMPERATURE);
Serial.println(" °C");
Serial.print("Humidity: ");
Serial.print(HTU31D.HUMIDITY);
Serial.println(" %");
delay(2000); // Delay between readings
}
The Sample file code text:
#include "GebraBit_HTU31D.h"
GebraBit_HTU31D HTU31D;
void setup() {
Wire.begin(); // Initialize the I2C bus
Serial.begin(9600); // Initialize serial communication for debugging
GB_HTU31D_initialize(&HTU31D); // Initialize the HTU31D sensor
GB_HTU31D_Configuration(&HTU31D); // Configure the HTU31D sensor
}
void loop() {
GB_HTU31D_Get_Data(&HTU31D); // Read data from the sensor
Serial.print("Temperature: ");
Serial.print(HTU31D.TEMPERATURE);
Serial.println(" °C");
Serial.print("Humidity: ");
Serial.print(HTU31D.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_HTU31D Library, the schematic of the modules and the “HTU31D datasheet”.
Program output video
The video of the module operation will be uploaded soon