2025-08-22 9:00 AM
In HTS221 sensor I have issue that H0_T0_OUT and H1_T0_OUT, H0_RH and H1_RH value is same like below
T0_degC float 20.5
T1_degC float 52.5
T0_OUT int16_t 1028
T1_OUT int16_t 0
H0_RH float 29.5
H1_RH float 29.5
H0_T0_OUT int16_t 0xf7f7 (hex)
H1_T0_OUT int16_t 0xf7f7 (hex)
This is my code
Init function
uint8_t HTS221_Init(HTS221 *hts221, I2C_HandleTypeDef *hi2c) {
hts221->hi2c = hi2c;
uint8_t data[2];
if (HTS221_ReadRegister(hts221, HTS221_WHO_AM_I, data, 1) != HAL_OK) {
return 1; // Error reading WHO_AM_I register
}
if (data[0] != HTS221_WHO_AM_I_VALUE) {
return 2; // WHO_AM_I value does not match expected value
}
// Read calibration data
if (HTS221_ReadRegister(hts221, CALIB_0, data, 2) != HAL_OK) {
return 3; // Error reading calibration data
}
hts221->H0_RH = (float) (data[0]) / 2.0f; // H0_rH_x2
hts221->H1_RH = (float) (data[1]) / 2.0f; // H1_rH_x2
if (HTS221_ReadRegister(hts221, CALIB_2, data, 2) != HAL_OK) {
return 4; // Error reading temperature calibration data
}
uint16_t T0_degC_x8 = (data[0]);
uint16_t T1_degC_x8 = (data[1]);
if (HTS221_ReadRegister(hts221, CALIB_5, data, 1) != HAL_OK) {
return 5; // Error reading T1/T0 msb
}
hts221->T0_degC = (float) (T0_degC_x8 | ((data[0] & 0b0011) << 8)) / 8.0f; // T0_degC_x8
hts221->T1_degC = (float) (T1_degC_x8 | (((data[0] & 0b1100) >> 2) << 8))
/ 8.0f; // T1_degC_x8
if (HTS221_ReadRegister(hts221, CALIB_6, data, 2) != HAL_OK) {
return 6; // Error reading H0_T0_OUT
}
hts221->H0_T0_OUT = (int16_t) ((data[1] << 8) | data[0]); // H0_T0_OUT_H and H0_T0_OUT_L
if (HTS221_ReadRegister(hts221, CALIB_A, data, 2) != HAL_OK) {
return 7; // Error reading H1_T0_OUT
}
hts221->H1_T0_OUT = (int16_t) ((data[1] << 8) | data[0]); // H1_T0_OUT_H and H1_T0_OUT_L
if (HTS221_ReadRegister(hts221, CALIB_C, data, 2) != HAL_OK) {
return 8; // Error reading T0_OUT
}
hts221->T0_OUT = (int16_t) ((data[1] << 8) | data[0]); // T0_OUT_H and T0_OUT_L
if (HTS221_ReadRegister(hts221, CALIB_E, data, 2) != HAL_OK) {
return 9; // Error reading T1_OUT
}
hts221->T1_OUT = (int16_t) ((data[1] << 8) | data[0]); // T1_OUT_H and T1_OUT_L
return 0; // Initialization successful
}
Header file
/*
* HTS221.h
*
* Created by CureSaba on 2025/08/21.
* This header file defines the interface for the HTS221 humidity and temperature sensor.
* It includes necessary definitions, register addresses, and a structure for sensor data.
*/
#ifndef HTS221_H
#define HTS221_H
#include "stm32l0xx_hal.h" // Include the HAL library for STM32L0xx
// Define the I2C address for the HTS221 sensor(P.20 of HTS221 datasheet)
#define HTS221_I2C_SAD (0b1011111<<1) // 7-bit address for HTS221
#define HTS221_WHO_AM_I 0x0F // WHO_AM_I register address
#define HTS221_WHO_AM_I_VALUE 0xBC // Expected value for WHO_AM_I register
#define AV_CONF 0x10 // Average configuration register address
#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define STATUS_REG 0x27
#define HUMIDITY_OUT_L 0x28
#define HUMIDITY_OUT_H 0x29
#define TEMP_OUT_L 0x2A
#define TEMP_OUT_H 0x2B
// Calibration registers(P.26 of HTS221 datasheet)
#define CALIB_0 0x30 // H0_rH_x2
#define CALIB_1 0x31 // H1_rH_x2
#define CALIB_2 0x32 // T0_degC_x8
#define CALIB_3 0x33 // T1_degC_x8
#define CALIB_5 0x35 // T1/T0 msb
#define CALIB_6 0x36 // H0_T0_OUT_L
#define CALIB_7 0x37 // H0_T0_OUT_H
#define CALIB_A 0x3A // H1_T0_OUT_L
#define CALIB_B 0x3B // H1_T0_OUT_H
#define CALIB_C 0x3C // T0_OUT_L
#define CALIB_D 0x3D // T0_OUT_H
#define CALIB_E 0x3E // T1_OUT_L
#define CALIB_F 0x3F // T1_OUT_H
typedef struct {
I2C_HandleTypeDef *hi2c; // Pointer to the I2C handle
// Temperature calibration data
float T0_degC;
float T1_degC;
int16_t T0_OUT;
int16_t T1_OUT;
// Humidity calibration data
float H0_RH;
float H1_RH;
int16_t H0_T0_OUT;
int16_t H1_T0_OUT;
float humidity;
float temperature;
} HTS221;
// Initialization function for HTS221
uint8_t HTS221_Init(HTS221 *hts221, I2C_HandleTypeDef *hi2c);
// Low-level read and write functions for HTS221
HAL_StatusTypeDef HTS221_ReadRegister(HTS221 *hts221, uint8_t reg, uint8_t *data, uint16_t size);
HAL_StatusTypeDef HTS221_WriteRegister(HTS221 *hts221, uint8_t reg, uint8_t *data, uint16_t size);
// Function to read humidity and temperature data from HTS221
HAL_StatusTypeDef HTS221_ReadTemperature(HTS221 *hts221);
HAL_StatusTypeDef HTS221_ReadHumidity(HTS221 *hts221);
#endif // HTS221_H