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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers htu21d.h Source File

htu21d.h

00001 /**
00002 HTU21D / HPP828E031 driver for mbed.
00003 - Includes RTOS hooks if RTOS is detected during compile.
00004 Author: Kevin Braun
00005  **/
00006 
00007 #ifndef HTU21D_H
00008 #define HTU21D_H
00009 
00010 #include "mbed.h"
00011 
00012 #if(defined(TARGET_KL25Z) )//|| defined(TARGET_K64F))
00013 
00014     #define HTU21Di2cLOWLEVEL   1           //if the use of low-level I2C routines is needed
00015     #warning "HTU21D using low level I2C routines"
00016     
00017 #endif
00018 
00019 //Defines for HTU21D
00020 #define HTU21Di2cWRITE      0x80
00021 #define HTU21Di2cREAD       0x81
00022 
00023 #define HTU21DWRITEUSER     0xE6
00024 #define HTU21DREADUSER      0xE7
00025 #define HTU21DtempNOHOLD    0xF3
00026 #define HTU21DhumNOHOLD     0xF5
00027 #define HTU21DRESET         0xFE
00028 
00029 #define HTU21SNAC1          0xFC
00030 #define HTU21SNAC2          0xC9
00031 #define HTU21SNB1           0xFA
00032 #define HTU21SNB2           0x0F
00033 
00034 #define HTU21DHEATER        0x04
00035 
00036 
00037     /**
00038     * measurement specialties / Honeywell HTU21D digital humidity and temperature sensor.
00039     * - Web site: http://www.meas-spec.com  
00040     * - Part Number: HPP828E031
00041     * - HTU21D = +-3% rh error at 55%
00042     * - HTU20D = +-5% rh error at 55%
00043     * - Main code generated from datasheet dated October 2013
00044     * - Serial number code generated from App Note "HTU2X Serial Number Reading", dated Februrary 2014
00045     * - No checksum checking is performed in this code
00046     *
00047     * @code
00048     * //Tested on FRDM-K64F
00049     *
00050     * #include "mbed.h"
00051     * #include "htu21d.h"
00052     *
00053     * #define SDA     PTE25 
00054     * #define SCL     PTE24
00055     *
00056     * Serial pc(USBTX, USBRX);                  //local terminal
00057     * htu21d htu(SDA, SCL);                     //Temp Hum || sda, scl
00058     *
00059     * float H21Temp = 0.0;                      //Temperture from HTU21D
00060     * float H21Hum = 0.0;                       //Humidity from HTU21D
00061     * float H21Dew = 0.0;                       //Dew Point from HTU21D
00062     *
00063     * //Note: If RTOS is used, Mutex for I2C must be initialized
00064     * #ifdef RTOS_H
00065     * Mutex MutexI2cWait;
00066     * #endif
00067     *
00068     * int main() {
00069     *     pc.baud(230400);                        //local terminal baud
00070     *     pc.printf("\r\n\r\nK64F_HTU21D basic operation\r\n"); 
00071     * 
00072     *     //initialize the HTU21D
00073     *     int htu21 = htu.softReset();
00074     *     if(htu21 == 0) {
00075     *         pc.printf(" - HTU21D broken...\r\n");
00076     *     } else {
00077     *         uint8_t HTU21DuserReg = htu.getUserReg();
00078     *         pc.printf("HTU21D UserReg: 0x%02x   SN: 0x%04x %08x %04x\r\n", 
00079     *                   HTU21DuserReg, htu.HTU21sn.HTU21D_sna, htu.HTU21sn.HTU21D_snb, htu.HTU21sn.HTU21D_snc);
00080     *     }
00081     * 
00082     *     while(true) {
00083     *         //get humidity, temperature and dew point from HTU21D
00084     *         if(htu21 == 1) {    //if HTU21D didn't initialize, don't access HTU21D anymore
00085     *             H21Hum = htu.getHum();
00086     *             if((double)H21Hum == 255.0) pc.printf("\r\n*** HTU21D Hum error!!\r\n");
00087     *             H21Temp = htu.getTemp();
00088     *             if((double)H21Temp == 255.0) pc.printf("\r\n*** HTU21D Temp error!!\r\n");
00089     *             H21Dew = htu.getDewPtFast();
00090     *         }
00091     *         pc.printf("Temp: %7.2f C %7.2f F   Hum: %4.1f %%   DewPt: %7.2f C\r\n", H21Temp, H21Hum, H21Dew);
00092     *         wait(1.0);
00093     *     }
00094     * }
00095     * @endcode
00096     **/
00097 class htu21d {
00098 
00099 public:
00100     /**
00101      * Constructor
00102      * - Fixed at I2C address 0x80
00103      * - I2C speed set to 400000
00104      *
00105      * @param PinName sda and scl, mbed I2C interface pins
00106      */
00107     htu21d(PinName sda, PinName scl);
00108     /**
00109      * Constructor
00110      * - Fixed at I2C address 0x80
00111      * - I2C speed set by user
00112      *
00113      * @param PinName sda and scl, mbed I2C interface pins 
00114      * @param int I2C frequency
00115      */
00116     htu21d(PinName sda, PinName scl, int i2cFrequency);
00117     /**
00118      * Destructor
00119      *
00120      * @param --none--
00121      */
00122     ~htu21d();
00123     /**
00124      * Reset the HTU21D chip
00125      * - Waits 15mS before exiting, allowing the chip reset to finish
00126      * - Executes getSNReg() which loads up HTU21D serial number structure
00127      * 
00128      * @param --none-- NOTE: run softReset() once at initialization time
00129      *
00130      * @return success / failure of HTU21D i2c access. 1 = ok, 0 = error
00131     */
00132     int softReset();
00133     /**
00134      * Get HTU21D user register
00135      * 
00136      * @param --none--
00137      *
00138      * @return 8 bit user register value
00139     */
00140     uint8_t getUserReg();
00141     /**
00142      * Turn ON the heater on the HTU21D
00143      * 
00144      * @param --none--
00145      *
00146      * @return success / failure of HTU21D i2c access. 1 = ok, 0 = error
00147     */
00148     int heaterOn();
00149     /**
00150      * Turn OFF the heater on the HTU21D
00151      * 
00152      * @param --none--
00153      *
00154      * @return success / failure of HTU21D i2c access. 1 = ok, 0 = error
00155     */
00156     int heaterOff();
00157     /**
00158      * Get heater on/off status of the HTU21D
00159      * 
00160      * @param --none--
00161      *
00162      * @return 0x04 = on, 0 = off
00163     */
00164     uint8_t getHeater();
00165     /**
00166      * Get HTU21D Temperature
00167      * 
00168      * @param --none--
00169      *
00170      * @return float of Temperature in degrees C.  255.0 if error
00171     */
00172     float getTemp();
00173     /**
00174      * Get HTU21D Humidity
00175      * 
00176      * @param --none--
00177      *
00178      * @return float of Humidity in percentage.  255.0 if error
00179     */
00180     float getHum();
00181     /**
00182      * Calculate the Dew Point
00183      * 
00184      * @param --none-- NOTE: You MUST run getTemp() and getHum() first!!
00185      *
00186      * @return float of Dew Point
00187     */
00188     float getDewPt();
00189     /**
00190      * Calculate the Dew Point fast
00191      * - 5x faster than getDewPt()
00192      * - slightly less accurate than getDewPt()
00193      * 
00194      * @param --none-- NOTE: You MUST run getTemp() and getHum() first!!
00195      *
00196      * @return float of Dew Point
00197     */
00198     float getDewPtFast();
00199     /**
00200      * Structure to access HTU21D's serial number 
00201      * - HTU21D_sna is the hi  16 bit word of the s/n, always is 0x4854
00202      * - HTU21D_snb is the mid 32 bit word of the s/n, 0x00--------
00203      * - HTU21D_snc is the low 16 bit word of the s/n, 0x32--
00204      * - The complete 64 bit s/n value is: 0x48 54 00 -- -- -- 32 --
00205      * - The numbers shown are fixed fields
00206      * - The '-' numbers are variable
00207      * - For reference, the CRC values for the s/n are included
00208     */    
00209     struct HTU21snStruct {
00210         uint16_t HTU21D_sna;            /**< Highest order 16 bit word of SN
00211                                             - Value always = 0x4854
00212                                             */
00213         uint32_t HTU21D_snb;            /**< Middle order 32 bit word of SN
00214                                             - Value = 0x00--------
00215                                             - Highest byte always = 0x00
00216                                             - Lower 3 bytes are variable
00217                                             */
00218         uint16_t HTU21D_snc;            /**< Lowest order 16 bit word of SN
00219                                             - Value = 0x32--
00220                                             - Highest byte always = 0x32
00221                                             - Lowest byte is variable
00222                                             */
00223         uint8_t HTU21D_crca;            /**< Single byte checksum from HTU21D_sna
00224                                             */  
00225         uint32_t HTU21D_crcb;           /**< Four byte checksum from HTU21D_snb
00226                                             */
00227         uint8_t HTU21D_crcc;            /**< Single byte checksum from HTU21D_snc
00228                                             */
00229         HTU21snStruct() {
00230             HTU21D_sna = 0;
00231             HTU21D_snb = 0;
00232             HTU21D_snc = 0;
00233             HTU21D_crca = 0;
00234             HTU21D_crcb = 0;
00235             HTU21D_crcc = 0;
00236         }
00237     } HTU21sn;
00238 
00239 private:
00240     I2C _i2c;
00241     /**
00242      * I2C access for getting raw Temperature and Humidity data
00243      * 
00244      * @param 8 bit HTU21D register to get data from. Must use non-blocking regs
00245      *
00246      * @return 16 bit raw i2c data, ANDed to 14 bits 0xFFFC. 0000 if error
00247     */
00248     uint16_t getData(uint8_t reg);
00249     /**
00250      * Get the HTU21D's serial number. 
00251      * - Number returned is 0x4854 00-- ---- 32--
00252      * - The numbers shown are fixed fields
00253      * - The '-' numbers are variable
00254      * 
00255      * @param --none--
00256      *
00257      * @return --none--
00258     */
00259 #if not defined HTU21Di2cLOWLEVEL
00260 char htuBuffer[8];
00261 #endif
00262     void getSNReg();
00263     double theTempIs;
00264     double theHumIs;
00265 };
00266 
00267 #endif