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:   BLE_soil_humidity

Library for the Measurement Specialties / Honeywell 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 K64F and KL25Z boards with and without RTOS, SDFileSystem and USB serial Rx interrupts.

The HTU21D's serial number is an odd cookie. There are two 16 bit registers and a 32 bit register are combined to generate the serial number. Some of the serial number bit fields are fixed for all devices and some change from part to part.

htu21d.h

Committer:
loopsva
Date:
2014-05-14
Revision:
0:2dab43acb3a4
Child:
1:d3ed713f8354

File content as of revision 0:2dab43acb3a4:

/**
 */

#ifndef HTU21D_H
#define HTU21D_H

#include "mbed.h"

//Defines for HTU21D
#define HTU21Di2cWRITE      0x80
#define HTU21Di2cREAD       0x81

#define HTU21DWRITEUSER     0xE6
#define HTU21DREADUSER      0xE7
#define HTU21DtempNOHOLD    0xF3
#define HTU21DhumNOHOLD     0xF5
#define HTU21DRESET         0xFE

#define HTU21DHEATER        0x04


/**
 * Honeywell HTU21D digital humidity and temperature sensor.
 */
class htu21d {

public:
    /**
     * Constructor.
     *
     * @param sda and scl, mbed I2C interface pins.
     */
    htu21d(PinName sda, PinName scl);
    /**
     * De-constructor.
     *
     * @param --none--.
     */
    ~htu21d();
    /**
     * Get HTU21D Temperature.
     * 
     * @param --none--.
     *
     * @return success / failure of HTU21D i2c access. 1 = ok, 0 = error.
    */
    int softReset();
    /**
     * Get HTU21D user register.
     * 
     * @param --none--.
     *
     * @return success / failure of HTU21D i2c access. 1 = ok, 0 = error.
    */
    uint8_t getUserReg();
    /**
     * Turn ON the heater in the HTU21D.
     * 
     * @param --none--.
     *
     * @return success / failure of HTU21D i2c access. 1 = ok, 0 = error.
    */
    int heaterOn();
    /**
     * Turn OFF the heater in the HTU21D.
     * 
     * @param --none--.
     *
     * @return --none--.
    */
    int heaterOff();
    /**
     * Get heater on/off status in the HTU21D.
     * 
     * @param --none--.
     *
     * @return 4 = on, 0 = 0ff.
    */
    uint8_t getHeater();
    /**
     * Do a reset on the HTU21D.
     * 
     * @param --none--.
     *
     * @return float of Temperature in degrees C.  255.0 if error.
    */
    float getTemp();
    /**
     * Get HTU21D Humidity.
     * 
     * @param --none--.
     *
     * @return float of Humidity in percentage.  255.0 if error.
    */
    float getHum();
    /**
     * Claculate the Dew Point.
     * 
     * @param MUST run getTemp and getHum first!!
     *
     * @return float of Dew Point.
    */
    float getDewPt();
    /**
     * Claculate the Dew Point. 5x faster than getDewPt().
     * 
     * @param MUST run getTemp and getHum first!!
     *
     * @return float of Dew Point.
    */
    float getDewPtFast();

 

private:
    I2C _i2c;
    /**
     * I2C access for getting raw Temperature and Humidity data.
     * 
     * @param 8 bit HTU21D register to get data from. Must use non-blocking regs.
     *
     * @return 16 bit raw i2c data, ANDed to 14 bits 0xFFFC. 0000 if error.
    */
    uint16_t getData(uint8_t reg);
    double theTempIs;
    double theHumIs;
    float getTrash();

};

#endif