SHT2x - Temperature and humidity

Description

Library for interfacing Sensirion SHT2x family (SHT20, SHT21, SHT25) humidity and temperature sensors

The SHT2x is a fully calibrated temperature and humidity sensor. -) I2C interface with communication speed up to 400 kHz -) Temperature range: -40 to 125°C ±0.3°C (SHT25: ±0.2°C) -) Relative humidity range: 0..100%rH (SHT20:±3%rH, SHT21:±2%rH, SHT25: ±1.8%rH)

OVERVIEW

SHT2x_Abstract:
This library can be used to handle Sensirion SHT2x (SHT20, SHT21, SHT25) humidity and temperature sensors.

Interfaces: 1x I2C

The SHT2x is a fully calibrated temperature and humidity sensor.
  • I2C interface with communication speed up to 400 kHz
  • Temperature range: -40 to 125°C ±0.3°C (SHT25: ±0.2°C)
  • Relative humidity range: 0..100%rH (SHT20:±3%rH, SHT21:±2%rH, SHT25: ±1.8%rH)
  • SHT2x_How_to_use:
    /* Hardware setup */ const { PORT_I2C = 0, // The first I2C interface should be used. CHIP_ADDR = SHT2x_I2C_ADR1, // I2C Address of the chip } /* Software setup */ const { CONFIG = SHT2x_USER_RES_12_14BIT,// Configuration to be written to the user register (use SHT2x_USER_RES_x) } /* Handles for available sensors */ new hSHT2x[TSHT2x_Handle]; // Handle to manage the SHT2x /* 1 sec. timer is used for the general program sequence */ #callback MainTimer() { /* Temporary memory for the return value of a function */ new iResult; // Init with "open loop" value to identify missing measurements // (temp/rH simply stay unchanged in case of an error) static temp = S32_OL; // Temperature read from the device handle static rh = S32_OL; // Humidity read from the device handle // Reads temperature from the device handle (updated by the SHT2x_HandleTimer() function) iResult = SHT2x_GetTemperature(hSHT2x, temp); // If no valid temperature is stored in the device handle -> Sets temperature value to "open loop" if(iResult < OK) temp = S32_OL; // Reads humidity from the device handle (updated by the SHT2x_HandleTimer() function) iResult = SHT2x_GetHumidity(hSHT2x, rh); // If no valid humidity is stored in the device handle -> Sets humidity value to "open loop" if(iResult < OK) rh = S32_OL; // Issues temperature and humidity via the watch panel #watch( "temp_rh=%s°C %s%%", s32ToNamurStr( temp,2), s32ToNamurStr( rh,1)); } /* 100ms timer is used for the SHT21 measurement process */ #callback MeasurementTimer() { SHT2x_HandleTimer(hSHT2x); // Handles SHT21 measurement process } /* Called when the timer for waiting until the sensor reaches idle state has expired */ #callback WaitSensorBoot() { // Init I2C interface catch(rM2M_I2cInit(PORT_I2C, SHT2x_CLOCK, 0)); // Initializes i2c communication with the SHT2x catch(SHT2x_Init(hSHT2x, PORT_I2C, CHIP_ADDR, CONFIG)); // Initialisation of a cyclic 100ms timer setInterval(MeasurementTimer, 100); // Initialisation of a cyclic sec. timer setInterval(MainTimer, 1000); } main() { // Starts the timer to wait until the sensor reaches idle state after power-up. setTimeout(WaitSensorBoot, 15); }

    BASIC

    SHT2x_CLOCK -

    typical I2C clock speed to be used (400kHz, I2C Fast-Mode)

    SHT2x_I2C_ADR1 -

    SHT2x I2C address

    SHT2x_USER_x:

    User Register configuration bits

    SHT2x_USER_RES_x - bit 7, 0: Measurement resolution
    SHT2x_USER_EOB_ON - bit6: End of battery
    This status bit is updated after each measurement
    0 - VDD > 2.25V (default)
    1 - VDD < 2.25V
    SHT2x_USER_RESERVED_MASK - bit 3-5: Reserved (must not be changed !)
    SHT2x_USER_HEATER_x - bit2: On-chip heater
    0 (SHT2x_USER_HEATER_OFF) - Disabeld (default)
    1 (SHT2x_USER_HEATER_ON) - Enabled
    SHT2x_USER_OTP_R_DISABLE - bit1: Disable OTP Reload (is not recommended for use !)
    0 - Enabled
    1 - Disabled (default)
    SHT2x_USER_RES_x:

    Measurement resolution (User register, bit7 and bit0)

    SHT2x_USER_RES_12_14BIT - 0x00: RH=12bit, T=14bit (default)
    SHT2x_USER_RES_8_12BIT - 0x01: RH= 8bit, T=12bit
    SHT2x_USER_RES_10_13BIT - 0x80: RH=10bit, T=13bit
    SHT2x_USER_RES_11_11BIT - 0x81: RH=11bit, T=11bit
    SHT2x_USER_RES_MASK - 0x81: Measurement resolution selection bit mask
    SHT2x_Init(handle[TSHT2x_Handle], i2c, addr, iConfig = SHT2x_USER_RES_12_14BIT)

    Initializes i2c communication with the SHT2x and prepares the SHT2x for use

    After power-up, the sensor needs at most 15ms, for reaching idle state, i.e. to be ready accepting commands. Do not call the SH2x_Init function until this time has elapsed.

    First copies the I2C port, I2C address and the transferred configuation to the transferred empty device handle. Afterwards the configuration is written into the user register. This also checks whether the SHT2x is present.

    handle : TSHT2x_Handle - Empty device handle for a SHT21
    i2c : s32 - I2C interface where SHT21 is connected to
    The I2C interface must be initialisation before using rM2M_I2cInit().
    addr : s32 - I2C Address of the chip
    Use only SHT2x_I2C_ADR1
    iConfig : s32 - Configuration for the SHT2x (use SHT2x_USER_x )
    returns : s32
    OK - if successful
    ERROR - Configuration could not be written into the user register
    SHT2x_HandleTimer(handle[TSHT2x_Handle])

    SHT21 measurement process state handling

    This function must be called cyclically. The interval should not be shorter than 90ms (It depends on the measurement duration, which in turn depends on the selected resolution). If a shorter interval is required, the times for the measurement duration in the data sheet must be taken into account.

    The SHT21 must be initialised using SHT2x_Init(() before calling this function for the first time. Each time the function is called, it checks the current state of the SHT21 measurement process and switches to the respective communication step. The first time it is called, a soft reset is performed. On the second call, the configuration stored in the device handle is transferred to the SHT21. From the third call the following sequence is executed: Trigger temperature measurement -> Read temperature -> Trigger humidity measurement -> Read humidity -> Trigger temperature measurement -> ...

    Due to the sequence described above, the SHT2x_HandleTimer() must be called 4 times until both a new temperature reading and a new humidity reading are available. Use SHT2x_GetTemperature() and SHT2x_GetHumidity() to read out the measurement data.
    handle : TSHT2x_Handle - Device handle of a specific SHT21 (Initialized by SHT2x_Init() )
    SHT2x_GetTemperature(handle[TSHT2x_Handle], &iValue)

    Reads temperature value stored in the device handle

    Requires that the SHT2x_HandleTimer () function is called cyclically.

    The function checks if any valid temperature value is available in the device handle and returns it if so.

    handle : TSHT2x_Handle - Device handle of a specific SHT21 (Initialized by SHT2x_Init() )
    iValue : s32 - Variable for storing the temperature [0.01°C], unchanged if result < OK
    returns : s32
    OK - Data available
    ERROR - No data available
    SHT2x_GetHumidity(handle[TSHT2x_Handle], &iValue)

    Reads humidity value stored in the device handle

    Requires that the SHT2x_HandleTimer () function is called cyclically.

    The function checks if any valid humidity value is available in the device handle and returns it if so.

    handle : TSHT2x_Handle - Device handle of a specific SHT21 (Initialized by SHT2x_Init() )
    iValue : s32 - Variable for storing the humidity [0.1%], unchanged if result < OK
    returns : s32
    OK - Data available
    ERROR - No data available

    EXPERT

    SHT2x_CLOCK_MAX -

    absolute maximum I2C clock speed

    SHT2x_CMD_x:

    Available Commands for SHT2x access

    (Command | R/W | Default |Description)
    SHT2x_CMD_TRIG_T_MEAS_POLL - 0xF3 | W | ---
    Trigger temperature measurement using "no hold master mode" (use SHT2x_SendCmd()). To read the measurement results use SHT2x_ReadMeasurement()
    SHT2x_CMD_TRIG_RH_MEAS_POLL - 0xF5 | W | ---
    Trigger humidity measurement using "no hold master mode" (use SHT2x_SendCmd()). To read the measurement results use SHT2x_ReadMeasurement()
    SHT2x_CMD_USER_REG_W - 0xE6 | W | 0x3A
    Write user register (use SHT2x_WriteConfig())
    SHT2x_CMD_USER_REG_R - 0xE7 | R | 0x3A
    Read user register (use SHT2x_ReadConfig())
    SHT2x_CMD_SOFT_RESET - 0xFE | W | ---
    Trigger soft reset (use SHT2x_SendCmd())
    SHT2x_SendCmd(handle[TSHT2x_Handle], iCommand)

    Sends a command to the STH21


    The function sends one byte of data containing the command to the sensor.

    handle : TSHT2x_Handle - Device handle of a specific SHT21 (Initialized by SHT2x_Init() )
    iCommand : s32 - Command that should be sent to the SHT2x (use SHT2x_CMD_TRIG_x or SHT2x_CMD_SOFT_RESET)
    returns : s32
    OK - if successful
    ERROR - Command contained in iCommand is invalid
    < OK - if another error occurs
    SHT2x_ReadConfig(handle[TSHT2x_Handle], &iValue)

    Reads configuration data from the SHT21


    The function sends one byte of data containing the Read user register command to the sensor and reads one byte of configuration data.

    handle : TSHT2x_Handle - Device handle of a specific SHT21 (Initialized by SHT2x_Init() )
    iValue : s32 - Variable for storing the configuration read from the STH21
    returns : s32
    OK - if successful
    < OK - if another error occurs
    SHT2x_WriteConfig(handle[TSHT2x_Handle], iConfig=-1)

    Writes configuration data to the SHT21


    Since the reserved bits in the user register must not be changed, the current content of the user register is first read out. After that, the value to be written to the user register is composed of the read value and the desired configuration. Finally two bytes of data containing the write user register command and the composed configuration data are sent to the sensor.

    handle : TSHT2x_Handle - Device handle of a specific SHT21 (Initialized by SHT2x_Init() )
    iConfig : s32 - Configuration for the SHT2x (optional, use SHT2x_USER_x )
    -1 - Configuration stored in device handle is written to the user register
    >= 0 - Value to be written to the user register
    returns : s32
    OK - if successful
    ERROR - Current contents of the user register could not be read
    < OK - if another error occurs
    SHT2x_ReadMeasurement(handle[TSHT2x_Handle], &iValue)

    Reads measurement value from the SHT21


    The function reads two bytes of measurement data from the SHT21. Depending on the status information contained in the measurement data, the raw value read is then converted into the temperature [0.01°C] or the humidity [0.1%].

    handle : TSHT2x_Handle - Device handle of a specific SHT21 (Initialized by SHT2x_Init() )
    iValue : s32 - Variable for storing the temperature [0.01°C] or humidity [0.1%] (Depending on the status information contained in the measurement data)
    returns : s32
    OK+1 - iValue contains a humidity value
    OK+2 - iValue contains a temperature value
    ERROR - CRC is invalid
    < OK - if another error occurs
    SHT21_GetSerialNumber(handle[TSHT2x_Handle], serial[8])

    Gets SHT21 serial number


    The function reads data from memory location 1 & 2 and returns the serial number for the given SHT21 sensor.

    handle : TSHT2x_Handle - Device handle of a specific SHT21 (Initialized by SHT2x_Init() )
    serial[8] - Cell array for storing the serial number
    returns : s32
    OK - if successful
    ERROR - Contents of memory location 1 & 2 could not be read

    DEBUG

    SHT2x_Debug_Config:
    To configure debug mode add the following macro to the main.dde file.
    /** * Use this macro to configure the debug mode: * * 0: No debugging * 1: In the event of an error, the return values of the functions for accessing * the SHT2x used within the SHT2x_HandleTimer function are issued via the console. */ #define SHT2x_DEBUG 0

    On this page

    SHT2x - Temperature and humidity