HTS221_library Files for All Microcontrollers

Introduction: HTS221_library Files for All Microcontrollers

About: hi keep learning keep supporting.

Hello everyone Recently I made a library for HTS221 which is a Capacitive digital sensor for measuring relative humidity and temperature. I have also mentioned for all MCU making it compatible with other controllers is not a big deal if you see a liquid crystal display with the help of library files available online you can use such LCD with almost all type of microcontrollers.

Similarly my library files contains sets of functions with can be used on almost all the microcontrollers, For controllers like SMT32 which widely uses HAL library or LL library this requires another set of library files which act as a mediator to access the hardware peripheral for HTS221 library file.

To drive a liquid crystal display we use library files such as "LiquidCrystal.h" and "wire.h" library to communicate with I2C enabled devices and sensors.


STM32 development board( any with I2c)

HTS221 sensor

CubeIDE latest version(works fine with some old versions)

Step 1: What Is a Library File

what is a function?

Functions are set of code that accomplish a specific task. Functions usually "take in" data, process it, and "return" a result. Once a function is written, it can be used over and over and over again. Functions can be "called" from the inside of other functions.


Libraries in programming languages are collections of prewritten code that users can use to organize simplify there task. usually library contains functions and variable.

Step 2: About HTS221 Sensor

HTS221 is a Temperature and Humidity sensor by STmicroelectronics

The HTS221 is an ultra-compact sensor for relative humidity and temperature. It includes a sensing element and a mixed signal ASIC to provide the measurement information through digital serial interfaces. The sensing element consists of a polymer dielectric planar capacitor structure capable of detecting relative humidity variations and is manufactured using a dedicated ST process.

Key Features:

 16-bit humidity and temperature output data

 SPI and I²C interfaces

 Factory calibrated

For more information Download the Datasheet "CLICK HERE"

Step 3: About HTS221-Library File

The library was created in C language. The entire library can be downloaded from Github along with that the project also contains the project file for STM32l151RE/152RE.




These files allows us to Read/Write registers and performs post calibration on output data.




These files make possible for HTS221-library to access resources STmicroelectronics HAL Library. Note: This files can only be used to Acess I2C Resources. If you wish to use SPI you should use "HAL_SPI_anklabs" library files.

HTS221_anklabs library consists of 52 variable declarations for HTS221 Registers and 10 functions. HAL_I2C_anklabs version 1 consists of 3 functions and a struct variable.

Step 4: How to Use HTS221-Lib File

How to use HTS221-Lib file

  • Open project workspace in your prefered toolchain.
  • Include HTS221_anklabs.h along with the rest of the .h files in your tool chain, you can create separate folder to place this files but the linker should be able to locate your files.
  • The same process applies for HTS221_anklabs.c
  • Include "#include

How to use HTS221-API files for HAL_libarary based STM32 controllers

  • CubeIDE and IAR embedded are the prefered toolcains but it will work fine with keil and other IDE too.
  • The same instructions follows as explained earlier and include "#include".
  • A small modification for the header file is essential when ever your are using different STM32 controllers.
  • Open HAL_I2C_anklabs.h file and modify the header file.


  • if your are using STM32l1xx - #include "stm32l1xx_hal.h"
  • if your are using STM32G4xx - #include "stm32g4xx_hal.h"
  • if your are using STM32F7xx - #include "stm32f7xx_hal.h"
  • if your are using STM32H7xx - #include "stm32h7xx_hal.h"
  • if your are using STM32l4xx - #include "stm32l4xx_hal.h"


Step 5: HTS221 Supporting Functions


  • Initializes the sensor and allows the user to change the output data rate.
  • Sets ODR to 1Hz(default).

Input parameter - no input Arguments(updating....)

Output parameter - void


  • Returns the Device ID of HTS221 Sensor.

Input parameter - no input Arguments.

Output parameter - Returns Device ID(uint8_t)


  • Returns 1 when HTS221 is detected.
  • Returns 0 when HTS221 is not detected.

Input parameter - no input Arguments.

Output parameter - Returns 0 or 1(uint8_t)


  • Performs Average for Temperature and Humidity values internally.
  • Makes 16 Temperature Average and 32 Humidity average.

Input parameter - No input Arguments

Output parameter - void


  • In oneshot mode the conversion has to be started manually.
  • After every ADC conversion Bit0 of Control register 2 in HTS221 sets to 0.
  • This function sets the required bits and starts the conversion.

Input parameter - No input parameter.

Output parameter - void


  • This functions check the availability of Temperature data.
  • When the users reads the data, the output will be zero when the ADC conversion has not completed.
  • After a successful read the data stored in the register sets to zero.
  • This function will make sure that the user reads the valid data.

Input parameter - No input parameter.

Output parameter - Returns 1 or 0 (uint8_t)


  • Same as before but this time it will check the availability of Humidity data.

Input parameter - No input parameter.

Output parameter - Returns 1 or 0 (uint8_t)


  • This function will check the availability of Temperature and Humidity data.

Input parameter - No input parameter.

Output parameter - Returns 1 or 0 (uint8_t)


  • Reads the 16-bit Temperature data from HTS221.
  • Performs linear interpolation.

Input parameter - No input parameter.

Output parameter - Returns calibrated Temperature data(uint32_t)


  • Reads the 16-bit Humidity data from HTS221.
  • Performs linear interpolation.

Input parameter - No input parameter.

Output parameter - Returns calibrated Humidity data(uint32_t)


Download HTS221-Datasheet "CLICK HERE"

Downlaod HTS221-Library Files "CLICK HERE"

Be the First to Share


    • Fix It Speed Challenge

      Fix It Speed Challenge
    • Photography Challenge

      Photography Challenge
    • Jewelry Challenge

      Jewelry Challenge