Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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
| |
Public Member Functions | |
| htu21d (PinName sda, PinName scl) | |
Constructor
| |
| htu21d (PinName sda, PinName scl, int i2cFrequency) | |
Constructor
| |
| ~htu21d () | |
| Destructor. | |
| int | softReset () |
Reset the HTU21D chip
| |
| 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:
-
PinName sda 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:
-
PinName sda and scl, mbed I2C interface pins int I2C frequency
Definition at line 23 of file htu21d.cpp.
| ~htu21d | ( | ) |
Member Function Documentation
| float getDewPt | ( | ) |
| float getDewPtFast | ( | ) |
Calculate the Dew Point fast
- 5x faster than getDewPt()
- slightly less accurate than getDewPt()
- 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.
Generated on Tue Jul 12 2022 18:12:19 by
1.7.2