Use a Tiny Humidity and Temperature Sensor to Effectively Collect Critical Environmental Data

By Stephen Evanczuk

Contributed By Digi-Key's North American Editors

Given the effect temperature and humidity have on the structural integrity of buildings and electronic systems, the ability to obtain accurate and reliable measurements of these parameters is fundamental to designs for a broad array of consumer, industrial, and medical applications. The effects of humidity and temperature on health are of particular concern, with studies showing variations in these parameters having effects ranging from discomfort to infection via aerosolized viruses.

This need to sense temperature and humidity across a variety of applications has become such that to meet demand, designers need a cost-effective, small-form-factor solution that’s easy to deploy. For long battery life in remote or otherwise hard-to-access locations, solutions may also need to consume very little power, all while maintaining the necessary accuracy and stability.

This article discusses the impact of environmental temperature and humidity on infrastructure, electronic systems, and human health. It then introduces and shows how to apply a miniature humidity and temperature sensor from TE Connectivity Measurement Specialties that designers can use to more easily meet the requirements for critical measurement across a diverse range of applications.

The importance of accurate humidity and temperature measurement

The ability to accurately monitor and adjust humidity and temperature levels plays a key role in a wide range of areas including heating, ventilation, and air conditioning (HVAC) systems, continuous positive airway pressure (CPAP) equipment for sleep apnea, and even basic human wellbeing.

The familiar term, relative humidity (RH), expresses the water content of air as a percentage of the maximum amount of water that air at a given temperature can hold. Like temperature, humidity levels that are too high or too low can be uncomfortable to humans and even damaging to structures, as well as mechanical and electronic equipment.

High humidity levels in buildings can lead to corrosion, mold and mildew growth, and breakdown of concrete and other materials. In electronic equipment, high humidity levels can result in short circuits due to condensation, particularly when equipment is brought into a humid environment from a cooler location.

Low humidity levels can cause shrinkage in materials, damage to paper products, and buildup of static electricity. With increased buildup, the resulting electrostatic discharge can cause damage to electronic devices and ignite fires in environments with heavy levels of airborne volatile organic compounds (VOCs). Consequently, RH sensors play an important role in providing a safe, healthy environment in buildings. Increasingly, similar concerns arise in fostering human health and wellness.

Humans typically notice uncomfortable temperatures rather than humidity levels, but excessively low or high humidity levels are known to impact health. High or low humidity can exacerbate symptoms for asthma and allergy sufferers, and result in lower sleep quality even for otherwise healthy individuals. At very low humidity levels, human tissue dries out causing irritation of the eye or nasal passages. CPAP manufacturers typically rely on humidity sensors to ensure that their equipment delivers suitably moist air to users.

The need for measurement and control of humidity levels plays a broader role in public health. Researchers have found that humidity levels do not need to reach extremes of dryness or moisture to play a role in human physiology. Normally, moisture in nasal passages helps expel aerosolized viruses (viruses suspended in microscopic droplets). When nasal passages are dry, aerosolized pathogens can penetrate more deeply into the respiratory system and more easily cause infection [1]. Due to these and other physiological factors, the infectivity of aerosolized influenza virus is significantly higher at humidity levels below 40% RH (Figure 1) [2]. More recent studies suggest that RH between 40% and 60% also plays a role in reducing COVID-19 infection, and even degrading the SARS-CoV-2 virus that causes COVID-19 [3].

Graph of relation between low RH levels and increased infectivity of aerosolized virusesFigure 1: Research showing a relation between low RH levels and increased infectivity of aerosolized viruses continues to drive growing demand for more accurate measurement solutions. (Image source: TE Connectivity Measurement Specialties)

Although accurate measurement of humidity and temperature is critical in so many different applications, the corresponding design requirements have limited developers’ ability to easily build effective solutions. Besides the need for high accuracy with very low long-term drift, many applications require sensors that offer rapid measurement and low-power operation in a minimal footprint, allowing easier sensor placement at the ideal point of measurement whether that might be in an HVAC humidistat, CPAP moisture control unit, or precision environmental monitoring system. TE Connectivity’s HTU31D humidity and temperature digital sensor meets the requirements for a growing list of applications that depend on accurate data.

A solution for critical measurement requirements

Featuring a small footprint and high accuracy, the HTU31D is optimized for applications ranging from consumer products to medical and professional monitoring systems. It comes in a 6-pin package measuring 2.5 x 2.5 x 0.9 millimeters (mm), is fully calibrated, and requires no additional field calibration. Thanks to its small footprint, developers can place the sensor in locations too small for earlier sensing solutions, and connect a remotely placed HTU31D through its I²C serial interface to its host controller using readily available I²C buffers or level shifters.

The HTU31D measures RH from 0 to 100% at a typical accuracy of ±2%, ±0.7% RH hysteresis, and typical long-term drift less than 0.25% RH/year. The device’s temperature measurement range is -40 to 125°C with a typical accuracy of ±0.2°C and typical long-term drift of 0.04°C/year. To help maintain its reliability, the sensor integrates a heating element for eliminating condensation at high humidity levels, as well as internal diagnostics to detect measurement errors, heating element errors, and internal memory errors.

In its baseline mode for humidity and temperature measurement, the sensor features resolution of 0.020% RH and 0.040°C, with conversion times of 1 millisecond (ms) and 1.6 ms, respectively. For more demanding requirements, the device provides operating modes that let developers increase resolution at the cost of increased conversion time. At the maximum resolution mode for each sensor, the HTU31D can provide 0.007% RH with a conversion time of 7.8 ms and 0.012°C with a conversion time of 12.1 ms.

For some applications such as battery-powered products, the device’s low current consumption is an equally important characteristic. Operating in its baseline resolution mode and performing one RH and temperature measurement per second, the device typically requires only 1.04 microamperes (μA). During non-active periods, the device can be placed in sleep mode where it typically consumes 0.13 μA. Of course, brief use of the internal heater to remove condensation or test temperature sensor operation results in an equally brief but significant increase in current.

Simple hardware and software interfaces

The HTU31D humidity and temperature digital sensor provides simple interfaces for hardware and software integration in developers’ designs. Along with the 3 to 5.5 volt supply voltage (VDD) and ground (GND) pins, the device’s hardware interface includes pins for I²C standard Serial Data (SDA) and Serial Clock (SCL) lines. The remaining two pins include a reset (RST) pin and an address (IC_ADD) pin. When IC_ADD is tied to GND or VDD, the device responds to I²C address 0x40 or 0x41, respectively, allowing two HTU31D devices to share the same I²C bus without conflict.

A host processor sends commands and reads results using basic I²C serial transactions. Commands use a two-byte sequence consisting of the I²C address, followed by a command byte with individual bits set to specify supported functions including combined temperature and humidity measurement, humidity only measurement, reset, heater on or off, device serial number, and diagnostics.

To perform a combined temperature and RH (T & RH) measurement, for example, the host would send the address byte and a byte containing the conversion command bit and bits specifying the desired resolution for the temperature and RH measurements. The device supports a simple polling method, so after transmitting the two-byte conversion command sequence, the host processor would wait for the resolution-dependent duration specified in the datasheet before issuing a two-byte sequence with the address byte (0x40 or 0x41), followed by the T & RH read command byte (0x0) (Figure 2, top row). The HTU31D would respond by transmitting the upper and lower bytes of raw values for each requested temperature and humidity measurement (Figure 2, bottom two rows). The raw values are converted to the corresponding physical temperature and humidity values using a pair of equations provided in the HTU31D datasheet.

Diagram of TE Connectivity HTU31D humidity and temperature digital sensor (click to enlarge)Figure 2: The HTU31D humidity and temperature digital sensor provides a straightforward interface for rapidly obtaining temperature and RH measurements. (Image source: TE Connectivity Measurement Specialties)

As shown in Figure 2, the HTU31D follows each 16-bit data sequence with a byte containing the data’s cyclic redundancy check (CRC) value generated by the device. This CRC-8 checksum allows detection of individual bit errors or double-bit errors anywhere in the data transmission, or clusters of bit errors within an 8-bit window. By comparing this transmitted CRC value with the CRC value it computes from the received data, the host processor can quickly identify a failed transmission and take appropriate action such as repeating the measurement command, briefly turning on the HTU31D integrated heating element, issuing a reset, or alerting the user to a possible failure in the measurement system.

Another feature of the transmission sequence allows the host to stop the respond sequence before its normal completion when some overriding need occurs. In a normal transaction, the HTU31D expects an acknowledge (ack) after the first data byte with a final not acknowledge (nack) and stop sequence at the very end of the data sequence (see Figure 2 again). Developers can use this feature to stop further transmission when CRC data or humidity data are not required, or when a new command such as device reset or heater activation is urgently required. Here, rather than sending the expected ack following a data or CRC byte, the host can issue the final nack/stop sequence to immediately terminate data transmission from the sensor.

TE’s HTU31D offers a straightforward electrical and functional interface, but employing any highly sensitive sensor requires careful physical design to avoid measurement artifacts arising from electrical or thermal interaction with other devices on board. Similarly, errors in implementing the command sequence protocol or value conversion equations can delay evaluation and prototyping of a humidity and temperature sensing feature in an evolving product. An add-on board and associated software from MikroElektronika allow developers to bypass potential implementation issues and begin immediate design and development.

Rapid prototyping and accelerated development

The MikroElektronika MIKROE-4306 HTU31D-based Temp & Hum 14 Click add-on board provides a complete implementation of the sensor’s electrical interface (Figure 3, left), mounted on a board measuring 28.6 x 25.4 mm (Figure 3, right).

Image of MikroElektronika Temp & Hum 14 Click board and design schematic (click to enlarge)Figure 3: Besides serving as a reference design schematic (left) for custom development, the MikroElektronika Temp & Hum 14 Click board (right) offers a platform for immediate evaluation and rapid prototyping of measurement solutions based on the HTU31D sensor. (Image source: MikroElektronika)

As with other mikroBUS Click add-on boards from MikroElektronika and other providers, the MikroElektronika Temp & Hum 14 Click board is designed to be plugged into a host processor board such as a MikroElektronika Fusion development board, and be used with MikroElektronika’s open-source mikroSDK software development framework.

MikroElektronika supplements the mikroSDK environment with software packages that provide drivers and board support for specific Click boards and development boards. For the Temp & Hum 14 Click board, MikroElektronika provides bindings of its Temp-Hum 14 Click software package for its Fusion and other MikroElektronika board families.

The Temp-Hum 14 Click software package supports development using an HTU31D-specific function library accessible through an application programming interface (API). An accompanying sample application demonstrates operation of the HTU31D sensor using a simple set of API functions including:

  • temphum14_set_conversion, which performs the conversion sequence mentioned earlier
  • temphum14_get_temp_and_hum, which executes the sensor’s T & RH data sequence
  • temphum14_get_diagnostic, which reads error status from the HTU31D’s on-chip diagnostic register

The sample application code demonstrates system initialization, application initialization, and execution of an application task. Listing 1 shows a snippet from the software package designed to run on the MikroElektronika Fusion for KINETIS v8 MIKROE-3515 development board, which is based on NXP's MK64FN1M0VDC12 Arm® Cortex®-M4 Kinetis K60 microcontroller.

#include "Click_TempHum14_types.h"
#include "Click_TempHum14_config.h"
#include "Click_TempHum14_other_peripherals.h"
temphum14_diagn_t status_data;
uint32_t ser_numb;
float temperature;
float humidity;
char log_text[ 50 ];
[code deleted]
void system_init ( )
    mikrobus_gpioInit( _MIKROBUS1, _MIKROBUS_RST_PIN, _GPIO_OUTPUT );
    mikrobus_i2cInit( _MIKROBUS1, &TEMPHUM14_I2C_CFG[0] );
    mikrobus_logInit( _LOG_USBUART, 9600 );
    Delay_ms( 100 );
    mikrobus_logWrite( "-----------------------------", _LOG_LINE );
    mikrobus_logWrite( "       Temp-Hum 14 click     ", _LOG_LINE );
    mikrobus_logWrite( "-----------------------------", _LOG_LINE );
    Delay_ms( 100 );
void application_init ( )
    temphum14_i2c_driver_init( (temphum14_obj_t)&_MIKROBUS1_GPIO, 
                               TEMPHUM14_I2C_SLAVE_ADDR_GND );
    Delay_ms( 100 );
    mikrobus_logWrite( "        Hardware Reset       ", _LOG_LINE );
    temphum14_hw_reset( );
    Delay_ms( 100 );
    ser_numb = temphum14_get_serial_number( );
    LongWordToStr( ser_numb, log_text );
    Ltrim( log_text );
    mikrobus_logWrite( "-----------------------------", _LOG_LINE );
    mikrobus_logWrite( "  Serial Number : ", _LOG_TEXT );
    mikrobus_logWrite( log_text, _LOG_LINE );
    mikrobus_logWrite( "-----------------------------", _LOG_LINE );
    mikrobus_logWrite( "        Software Reset       ", _LOG_LINE );
    temphum14_soft_reset( );
    Delay_ms( 100 );
    temphum14_get_diagnostic( &status_data );
    Delay_ms( 10 );
    display_diagnostic( );
    Delay_ms( 100 );
void application_task ( )
    temphum14_set_conversion( TEMPHUM14_CONVERSION_HUM_OSR_0_020,
                              TEMPHUM14_CONVERSION_TEMP_0_040 );
    Delay_ms( 10 );
    temphum14_get_temp_and_hum( &temperature, &humidity );
    Delay_ms( 10 );
    FloatToStr( temperature, log_text );
    mikrobus_logWrite( "  Temperature : ", _LOG_TEXT );
    mikrobus_logWrite( log_text, _LOG_TEXT );
    mikrobus_logWrite( " C", _LOG_LINE );
    FloatToStr( humidity, log_text );
    mikrobus_logWrite( "  Humidity    : ", _LOG_TEXT );
    mikrobus_logWrite( log_text, _LOG_TEXT );
    mikrobus_logWrite( " %", _LOG_LINE );
    mikrobus_logWrite( "-----------------------------", _LOG_LINE );
    Delay_ms( 3000 );
void main ( )
    system_init( );
    application_init( );
    for ( ; ; )
            application_task( );

Listing 1: This snippet from the MikroElektronika sample application demonstrates the basic design patterns for initializing and executing a simple task to obtain temperature and RH measurements from the HTU31D sensor. (Code source: MikroElektronika)

The sample application included in the software package demonstrates the basic design patterns for implementing a software application using the TE HTU31D sensor. As illustrated in Listing 1, the main routine starts by calling a system initialization function (system_init()) to set up low-level drivers including the HTU31D sensor and calling a function (application_init()) to initialize application resources. In this case, application_init() initializes the system’s I²C driver with an instance of a sensor object before performing a sensor reset and a function call (temphum14_get_diagnostic()) to retrieve the sensor’s diagnostics and display the diagnostic information (display_diagnostic()).

After the short initialization phase, the sample application enters an endless loop that calls an application task every three seconds. In the sample code shown in Listing 1, the application task requests a conversion at 0.020% RH resolution and 0.040°C, the HTU31D’s baseline operating mode as noted earlier. In this baseline mode, the HTU31D needs only 1 ms to measure RH and 1.6 ms to measure temperature. The sample application pads the wait duration, using a 10 ms delay (delay_ms(10)) before calling the API function temphum14_get_temp_and_hum() to retrieve the temperature and humidity values. Because the library performs the transformation needed to convert raw values from the HTU31D to physical temperature and humidity measurement values, the resulting measurement values can be used directly—simply logging the results in this case.

Using this hardware platform and associated software environment, developers can quickly evaluate and prototype HTU31D sensor applications to obtain accurate RH and temperature measurements at a variety of resolutions. For custom hardware development, the MikroElektronika Temp & Hum 14 Click board serves as a complete reference design including full schematics and physical design. For custom software development, the Temp-Hum 14 click software package provides a basic template for building more comprehensive applications.


Humidity and temperature play a critical role in the integrity of structures and equipment, as well as human health and wellness. However, suitable management of humidity and temperature depends on a combination of measurement accuracy and pervasive measurement that has been difficult to achieve easily due to limitations in conventional sensor solutions.

A humidity and temperature sensor from TE Connectivity Measurement Specialties offers the unique combination of accuracy, stability, size, and ease of use needed to meet emerging measurement requirements in consumer, industrial, and medical applications.


  1. Low ambient humidity impairs barrier function and innate resistance against influenza infection
  2. High Humidity Leads to Loss of Infectious Influenza Virus from Simulated Coughs
  3. The effect of temperature and humidity on the stability of SARS-CoV-2 and other enveloped viruses

Disclaimer: The opinions, beliefs, and viewpoints expressed by the various authors and/or forum participants on this website do not necessarily reflect the opinions, beliefs, and viewpoints of Digi-Key Electronics or official policies of Digi-Key Electronics.

About this author

Stephen Evanczuk

Stephen Evanczuk has more than 20 years of experience writing for and about the electronics industry on a wide range of topics including hardware, software, systems, and applications including the IoT. He received his Ph.D. in neuroscience on neuronal networks and worked in the aerospace industry on massively distributed secure systems and algorithm acceleration methods. Currently, when he's not writing articles on technology and engineering, he's working on applications of deep learning to recognition and recommendation systems.

About this publisher

Digi-Key's North American Editors