SHT21 temperature and humidity sensor

The SHT21 is an I2C temperature and humidity sensor from Sensirion. The SHT21 digital humidity and temperature sensor is fully calibrated and offers high precision and excellent long-term stability.

Hello World

Import programsht21_test

Basic test program for the SHT2x library

Library

Import programsht21_test

Basic test program for the SHT2x library

Notes

SHT2x
Misenso SHT2x Breakout Board

The Misenso SHT2x breakout board already includes the two I2C pullups (seen above) needed.

Wiring

mbedSHT21 sensor
gndgnd
Vout(3.3V)VCC
P9(sda)SDA
p10(scl)SCL

Code Example


#include "mbed.h"
#include "SHT2x.h"

SHT2x sht21(p9, p10);

DigitalOut myled(LED2);
Serial pc(USBTX, USBRX);

int rh, temp;
int error;
float relHumidity, temperature;
int userRegister;

int main() {
    sht21.SHT2x_SoftReset();
    
    error |= sht21.SHT2x_ReadUserRegister(&userRegister);  //get actual user reg
    userRegister = (userRegister & ~SHT2x_RES_MASK) | SHT2x_RES_12_14BIT;
    error |= sht21.SHT2x_WriteUserRegister(&userRegister); //write changed user reg    
    
    while (1) {
        myled = 1;
        
        pc.printf("program start\r\n");
        error |= sht21.SHT2x_MeasureHM(HUMIDITY, &rh);
        error |= sht21.SHT2x_MeasureHM(TEMP, &temp);
        
        if (error > 0)
            pc.printf("error code %d \r\n", error);

        relHumidity = sht21.SHT2x_CalcRH(rh);
        temperature = sht21.SHT2x_CalcTemperatureC(temp);
        
        pc.printf("RH value -> %f \r\n", relHumidity);
        pc.printf("Temp in C -> %f \r\n", temperature);
        pc.printf("Dew point %f \r\n", sht21.SHT2x_GetDewpoint(relHumidity, temperature));

        myled = 0;
        wait(1);
    }
}





You need to log in to post a discussion

Questions