Sunday, July 6, 2025

SHT4x library for AVR

SHT4x is a digital sensor for measuring relative humidity and temperature over I2C interface. It has a very good accuracy and a low power consumption. The device also includes an electric heater with 3 selectable power levels.

The chip comes in a small package of 1.5x1.5mm which makes it difficult fond hand-soldering and also not recommended by the manufacturer, however I did managed to hand solder it. That being said I recommend using a PCB stencil otherwise you risk getting flux into the sensors membrane. It is not recommended using any cleaning solution that might contaminate the membrane unless you have the sensor version that includes a protective cover. Before soldering, consider reading the Handling Instructions by Sensirion.

The datasheet.

 

SHT4x highlights


Contents

 

SHT4x pinout

 

SHT4x pinout
1 - SDA: serial data, bidirectional.

2 - SCL: serial clock, unidirectional input.

3 - VDD: supply voltage (1.08V - 3.6V).

4 - VSS: ground.

Typical application circuit

SHT4 typical application circuit

API

Configuration

#define SHT4_SERIAL_ADDR	    0x44 // SHT4x device serial address
#define SHT4_TWI_MODULE		    &twi0 // &twi0 or &twi1
#define SHT4_TWI_SPEED		    TWI_100KHZ // TWI_100KHZ or TWI_400KHZ

 The header file contains some configuration that defines the device serial address, the TWI (I2C) module to use and serial speed.

Initialization

void SHT4_Init(void)

Initialize TWI and add a 1ms delay power-up time for the sensor.

Read sensor

int8_t SHT4_Read(uint8_t mode)

Read temperature and relative humidity.

mode

Read mode: high, medium or low precision.

Available constants:

SHT4_T_RH_HR - measure T & RH with high precision (high repeatability)

SHT4_T_RH_MR - measure T & RH with medium precision (medium repeatability)

SHT4_T_RH_LR - measure T & RH with low precision (low repeatability)

Return

0 on success, -1 on temperature CRC mismatch, -2 on humidity CRC mismatch,  1 if timeout and no sensor response.

Get temperature in Celsius

float SHT4_TCelsius(void)

Read temperature in degree Celsius. Must be used after issuing a read command using SHT4_Read().

Get temperature in Fahrenheit

float SHT4_TFahrenheit(void)

Read temperature in degree Fahrenheit. Must be used after issuing a read command using SHT4_Read().

Get relative humidity

float SHT4_RH(void)

Read relative humidity. Must be used after issuing a read command using SHT4_Read()

Activate heater

int8_t SHT4_Heater(uint8_t mode)

Activate heater. All modes include a high precision measurement just before deactivation, and can be read using SHT4_TCelsius() and SHT4_RH().

mode

Heater mode.

Available constants:

SHT4_HEAT_200mW_1S - activate heater with 200mW for 1s 

SHT4_HEAT_200mW_0S1 - activate heater with 200mW for 0.1s

SHT4_HEAT_110mW_1S - activate heater with 110mW for 1s

SHT4_HEAT_110mW_0S1 - activate heater with 110mW for 0.1s

SHT4_HEAT_20mW_1S - activate heater with 20mW for 1s

SHT4_HEAT_20mW_0S1 - activate heater with 20mW for 0.1s

Return

0 on success, -1 on temperature CRC mismatch, -2 on humidity CRC mismatch,  1 if timeout and no sensor response.

Read serial number

int8_t SHT4_ReadSN(void)

Read serial number as two 16-bit words.

Return

0 on success, -1 on CRC mismatch, 1 if timeout and no sensor response.

Get serial number

uint16_t* SHT4_GetSN(void)

Get serial number. Must be used after issuing a read command using SHT4_ReadSN().    

Return

A pointer to an 16-bit array with size of 2.

Soft reset

void SHT4_Reset(void)

Issue a soft reset command.

Code example

int main(void){
    float tempC = 0;
    float RH = 0;
    uint8_t ret_code = 0;
    uint16_t sn0 = 0;
    uint16_t sn1 = 0;
    uint16_t *sn = 0;

    /* SHT4 */
    SHT4_Init();

    // Read S/N
    ret_code = SHT4_ReadSN();
    if(ret_code == 0){
        sn = SHT4_GetSN();
        sn0 = sn[0];
        sn1 = sn[1];
    }

    while(1){
        // Read Temp and RH
        ret_code = SHT4_Read(SHT4_T_RH_HR);
        
        if(ret_code == 0){
	    tempC = SHT4_TCelsius();
	    RH = SHT4_RH();
	}else{
            // Something went wrong.
	}

        _delay_ms(1000);
    }

    return 1;
}

Links

v1.0 SHT4x library
Changelog
v1.0 (2025-07-06)
Public release under GNU GPL v3 license.

No comments:

Post a Comment