BASIC LIBRARY TO INTERFACE WITH HTU21D TEMPERATURE AND HUMIDITY SENSOR

Dependents:   Natural_Calamities_Monitoring_System Nucleo_HTU21D-F Nucleo_motors lpc1768_blinky ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HTU21D.cpp Source File

HTU21D.cpp

00001 /**
00002  * @author Alan Lai & Nelson Diaz
00003  * The Georgia Institute of Technology 
00004  * ECE 4180 Embeded Systems
00005  * Professor Hamblen
00006  * 03/28/2014
00007  * 
00008  * @section LICENSE
00009  * ----------------------------------------------------------------------------
00010  * "THE BEER-WARE LICENSE" (Revision 42):
00011  * <alanhlai91@gmail.com> wrote this file. As long as you retain this notice you
00012  * can do whatever you want with this stuff. If we meet some day, and you think
00013  * this stuff is worth it, you can buy me a beer in return.
00014  * ----------------------------------------------------------------------------
00015  *
00016  *
00017  * @section DESCRIPTION
00018  *
00019  * HTU21D Humidity and Temperature sensor.
00020  *
00021  * Datasheet, specs, and information:
00022  *
00023  * https://www.sparkfun.com/products/12064
00024  */
00025 
00026 /**
00027  * Includes
00028  */
00029 #include "HTU21D.h"
00030 
00031 HTU21D::HTU21D(PinName sda, PinName scl) {
00032 
00033     i2c_ = new I2C(sda, scl);
00034     //400KHz, as specified by the datasheet.
00035     i2c_->frequency(400000);
00036 
00037 
00038 
00039 }
00040 
00041 int HTU21D::sample_ctemp(void) {
00042 
00043     char tx[1];
00044     char rx[2];
00045 
00046     tx[0] = TRIGGER_TEMP_MEASURE; // Triggers a temperature measure by feeding correct opcode.
00047     i2c_->write((HTU21D_I2C_ADDRESS << 1) & 0xFE, tx, 1);
00048     wait_ms(1);
00049     
00050     // Reads triggered measure
00051     i2c_->read((HTU21D_I2C_ADDRESS << 1) | 0x01, rx, 2);
00052     wait_ms(1);
00053     
00054     // Algorithm from datasheet to compute temperature.
00055     unsigned int rawTemperature = ((unsigned int) rx[0] << 8) | (unsigned int) rx[1];
00056     rawTemperature &= 0xFFFC;
00057 
00058     float tempTemperature = rawTemperature / (float)65536; //2^16 = 65536
00059     float realTemperature = -46.85 + (175.72 * tempTemperature); //From page 14
00060 
00061     return (int)realTemperature;
00062 
00063 }
00064 
00065 int HTU21D::sample_ftemp(void){
00066     int temptemp = sample_ctemp();
00067     int ftemp = temptemp * 1.8 + 32;
00068     
00069     return ftemp;
00070 }
00071 
00072 int HTU21D::sample_ktemp(void){
00073     int temptemp = sample_ctemp();
00074     int ktemp = temptemp + 274;
00075     
00076     return ktemp;
00077     
00078     
00079 }
00080 
00081 int HTU21D::sample_humid(void) {
00082 
00083     char tx[1];
00084     char rx[2];
00085 
00086 
00087     tx[0] = TRIGGER_HUMD_MEASURE; // Triggers a humidity measure by feeding correct opcode.
00088     i2c_->write((HTU21D_I2C_ADDRESS << 1) & 0xFE, tx, 1);
00089     wait_ms(1);
00090     
00091     // Reads triggered measure
00092     i2c_->read((HTU21D_I2C_ADDRESS << 1) | 0x01, rx, 2);
00093     wait_ms(1);
00094     
00095     //Algorithm from datasheet.
00096     unsigned int rawHumidity = ((unsigned int) rx[0] << 8) | (unsigned int) rx[1];
00097 
00098     rawHumidity &= 0xFFFC; //Zero out the status bits but keep them in place
00099     
00100     //Given the raw humidity data, calculate the actual relative humidity
00101     float tempRH = rawHumidity / (float)65536; //2^16 = 65536
00102     float rh = -6 + (125 * tempRH); //From page 14
00103 
00104 
00105     return (int)rh;
00106 
00107 }
00108