Library for the Measurement Specialties' HTU21D Humidity and Temperature sensor. Code includes device's heater on/off control, serial number access, dew point calculations and RTOS hooks. To date, code tested on GR-PEACH, K64F and KL25Z boards with and w/o RTOS, SDFlash and USB serial Rx interrupts.

Dependents:   HTU21D_testing BLE_soil_humidity

Embed: (wiki syntax)

« Back to documentation index

htu21d Class Reference

htu21d Class Reference

measurement specialties / Honeywell HTU21D digital humidity and temperature sensor. More...

#include <htu21d.h>

Data Structures

struct  HTU21snStruct
 Structure to access HTU21D's serial number

  • HTU21D_sna is the hi 16 bit word of the s/n, always is 0x4854
  • HTU21D_snb is the mid 32 bit word of the s/n, 0x00--------
  • HTU21D_snc is the low 16 bit word of the s/n, 0x32--
  • The complete 64 bit s/n value is: 0x48 54 00 -- -- -- 32 --
  • The numbers shown are fixed fields
  • The '-' numbers are variable
  • For reference, the CRC values for the s/n are included.
More...

Public Member Functions

 htu21d (PinName sda, PinName scl)
 Constructor

  • Fixed at I2C address 0x80
  • I2C speed set to 400000.

 htu21d (PinName sda, PinName scl, int i2cFrequency)
 Constructor

  • Fixed at I2C address 0x80
  • I2C speed set by user.

 ~htu21d ()
 Destructor.
int softReset ()
 Reset the HTU21D chip

  • Waits 15mS before exiting, allowing the chip reset to finish
  • Executes getSNReg() which loads up HTU21D serial number structure.

uint8_t getUserReg ()
 Get HTU21D user register.
int heaterOn ()
 Turn ON the heater on the HTU21D.
int heaterOff ()
 Turn OFF the heater on the HTU21D.
uint8_t getHeater ()
 Get heater on/off status of the HTU21D.
float getTemp ()
 Get HTU21D Temperature.
float getHum ()
 Get HTU21D Humidity.
float getDewPt ()
 Calculate the Dew Point.
float getDewPtFast ()
 Calculate the Dew Point fast



Detailed Description

measurement specialties / Honeywell HTU21D digital humidity and temperature sensor.

  • Web site: http://www.meas-spec.com
  • Part Number: HPP828E031
  • HTU21D = +-3% rh error at 55%
  • HTU20D = +-5% rh error at 55%
  • Main code generated from datasheet dated October 2013
  • Serial number code generated from App Note "HTU2X Serial Number Reading", dated Februrary 2014
  • No checksum checking is performed in this code
 //Tested on FRDM-K64F

 #include "mbed.h"
 #include "htu21d.h"

 #define SDA     PTE25 
 #define SCL     PTE24

 Serial pc(USBTX, USBRX);                  //local terminal
 htu21d htu(SDA, SCL);                     //Temp Hum || sda, scl

 float H21Temp = 0.0;                      //Temperture from HTU21D
 float H21Hum = 0.0;                       //Humidity from HTU21D
 float H21Dew = 0.0;                       //Dew Point from HTU21D

 //Note: If RTOS is used, Mutex for I2C must be initialized
 #ifdef RTOS_H
 Mutex MutexI2cWait;
 #endif

 int main() {
     pc.baud(230400);                        //local terminal baud
     pc.printf("\r\n\r\nK64F_HTU21D basic operation\r\n"); 
 
     //initialize the HTU21D
     int htu21 = htu.softReset();
     if(htu21 == 0) {
         pc.printf(" - HTU21D broken...\r\n");
     } else {
         uint8_t HTU21DuserReg = htu.getUserReg();
         pc.printf("HTU21D UserReg: 0x%02x   SN: 0x%04x %08x %04x\r\n", 
                   HTU21DuserReg, htu.HTU21sn.HTU21D_sna, htu.HTU21sn.HTU21D_snb, htu.HTU21sn.HTU21D_snc);
     }
 
     while(true) {
         //get humidity, temperature and dew point from HTU21D
         if(htu21 == 1) {    //if HTU21D didn't initialize, don't access HTU21D anymore
             H21Hum = htu.getHum();
             if((double)H21Hum == 255.0) pc.printf("\r\n*** HTU21D Hum error!!\r\n");
             H21Temp = htu.getTemp();
             if((double)H21Temp == 255.0) pc.printf("\r\n*** HTU21D Temp error!!\r\n");
             H21Dew = htu.getDewPtFast();
         }
         pc.printf("Temp: %7.2f C %7.2f F   Hum: %4.1f %%   DewPt: %7.2f C\r\n", H21Temp, H21Hum, H21Dew);
         wait(1.0);
     }
 }

Definition at line 97 of file htu21d.h.


Constructor & Destructor Documentation

htu21d ( PinName  sda,
PinName  scl 
)

Constructor

  • Fixed at I2C address 0x80
  • I2C speed set to 400000.

Parameters:
PinNamesda and scl, mbed I2C interface pins

Definition at line 19 of file htu21d.cpp.

htu21d ( PinName  sda,
PinName  scl,
int  i2cFrequency 
)

Constructor

  • Fixed at I2C address 0x80
  • I2C speed set by user.

Parameters:
PinNamesda and scl, mbed I2C interface pins
intI2C frequency

Definition at line 23 of file htu21d.cpp.

~htu21d (  )

Destructor.

Parameters:
--none--

Definition at line 30 of file htu21d.cpp.


Member Function Documentation

float getDewPt (  )

Calculate the Dew Point.

Parameters:
--none--NOTE: You MUST run getTemp() and getHum() first!!
Returns:
float of Dew Point

Definition at line 215 of file htu21d.cpp.

float getDewPtFast (  )

Calculate the Dew Point fast

Parameters:
--none--NOTE: You MUST run getTemp() and getHum() first!!
Returns:
float of Dew Point

Definition at line 229 of file htu21d.cpp.

uint8_t getHeater (  )

Get heater on/off status of the HTU21D.

Parameters:
--none--
Returns:
0x04 = on, 0 = off

Definition at line 135 of file htu21d.cpp.

float getHum (  )

Get HTU21D Humidity.

Parameters:
--none--
Returns:
float of Humidity in percentage. 255.0 if error

Definition at line 199 of file htu21d.cpp.

float getTemp (  )

Get HTU21D Temperature.

Parameters:
--none--
Returns:
float of Temperature in degrees C. 255.0 if error

Definition at line 188 of file htu21d.cpp.

uint8_t getUserReg (  )

Get HTU21D user register.

Parameters:
--none--
Returns:
8 bit user register value

Definition at line 60 of file htu21d.cpp.

int heaterOff (  )

Turn OFF the heater on the HTU21D.

Parameters:
--none--
Returns:
success / failure of HTU21D i2c access. 1 = ok, 0 = error

Definition at line 111 of file htu21d.cpp.

int heaterOn (  )

Turn ON the heater on the HTU21D.

Parameters:
--none--
Returns:
success / failure of HTU21D i2c access. 1 = ok, 0 = error

Definition at line 88 of file htu21d.cpp.

int softReset (  )

Reset the HTU21D chip

  • Waits 15mS before exiting, allowing the chip reset to finish
  • Executes getSNReg() which loads up HTU21D serial number structure.

Parameters:
--none--NOTE: run softReset() once at initialization time
Returns:
success / failure of HTU21D i2c access. 1 = ok, 0 = error

Definition at line 36 of file htu21d.cpp.