BASIC LIBRARY TO INTERFACE WITH HTU21D TEMPERATURE AND HUMIDITY SENSOR

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

Files at this revision

API Documentation at this revision

Comitter:
hwing91
Date:
Fri Mar 28 14:55:03 2014 +0000
Commit message:
INITIAL COMMIT

Changed in this revision

HTU21D/HTU21D.cpp Show annotated file Show diff for this revision Revisions of this file
HTU21D/HTU21D.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r db86ad1b4459 HTU21D/HTU21D.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HTU21D/HTU21D.cpp	Fri Mar 28 14:55:03 2014 +0000
@@ -0,0 +1,108 @@
+/**
+ * @author Alan Lai & Nelson Diaz
+ * The Georgia Institute of Technology 
+ * ECE 4180 Embeded Systems
+ * Professor Hamblen
+ * 03/28/2014
+ * 
+ * @section LICENSE
+ * ----------------------------------------------------------------------------
+ * "THE BEER-WARE LICENSE" (Revision 42):
+ * <alanhlai91@gmail.com> wrote this file. As long as you retain this notice you
+ * can do whatever you want with this stuff. If we meet some day, and you think
+ * this stuff is worth it, you can buy me a beer in return.
+ * ----------------------------------------------------------------------------
+ *
+ *
+ * @section DESCRIPTION
+ *
+ * HTU21D Humidity and Temperature sensor.
+ *
+ * Datasheet, specs, and information:
+ *
+ * https://www.sparkfun.com/products/12064
+ */
+
+/**
+ * Includes
+ */
+#include "HTU21D.h"
+
+HTU21D::HTU21D(PinName sda, PinName scl) {
+
+    i2c_ = new I2C(sda, scl);
+    //400KHz, as specified by the datasheet.
+    i2c_->frequency(400000);
+
+
+
+}
+
+int HTU21D::sample_ctemp(void) {
+
+    char tx[1];
+    char rx[2];
+
+    tx[0] = TRIGGER_TEMP_MEASURE; // Triggers a temperature measure by feeding correct opcode.
+    i2c_->write((HTU21D_I2C_ADDRESS << 1) & 0xFE, tx, 1);
+    wait_ms(1);
+    
+    // Reads triggered measure
+    i2c_->read((HTU21D_I2C_ADDRESS << 1) | 0x01, rx, 2);
+    wait_ms(1);
+    
+    // Algorithm from datasheet to compute temperature.
+    unsigned int rawTemperature = ((unsigned int) rx[0] << 8) | (unsigned int) rx[1];
+    rawTemperature &= 0xFFFC;
+
+    float tempTemperature = rawTemperature / (float)65536; //2^16 = 65536
+    float realTemperature = -46.85 + (175.72 * tempTemperature); //From page 14
+
+    return (int)realTemperature;
+
+}
+
+int HTU21D::sample_ftemp(void){
+    int temptemp = sample_ctemp();
+    int ftemp = temptemp * 1.8 + 32;
+    
+    return ftemp;
+}
+
+int HTU21D::sample_ktemp(void){
+    int temptemp = sample_ctemp();
+    int ktemp = temptemp + 274;
+    
+    return ktemp;
+    
+    
+}
+
+int HTU21D::sample_humid(void) {
+
+    char tx[1];
+    char rx[2];
+
+
+    tx[0] = TRIGGER_HUMD_MEASURE; // Triggers a humidity measure by feeding correct opcode.
+    i2c_->write((HTU21D_I2C_ADDRESS << 1) & 0xFE, tx, 1);
+    wait_ms(1);
+    
+    // Reads triggered measure
+    i2c_->read((HTU21D_I2C_ADDRESS << 1) | 0x01, rx, 2);
+    wait_ms(1);
+    
+    //Algorithm from datasheet.
+    unsigned int rawHumidity = ((unsigned int) rx[0] << 8) | (unsigned int) rx[1];
+
+    rawHumidity &= 0xFFFC; //Zero out the status bits but keep them in place
+    
+    //Given the raw humidity data, calculate the actual relative humidity
+    float tempRH = rawHumidity / (float)65536; //2^16 = 65536
+    float rh = -6 + (125 * tempRH); //From page 14
+
+
+    return (int)rh;
+
+}
+
diff -r 000000000000 -r db86ad1b4459 HTU21D/HTU21D.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HTU21D/HTU21D.h	Fri Mar 28 14:55:03 2014 +0000
@@ -0,0 +1,103 @@
+/**
+ * @author Alan Lai & Nelson Diaz
+ * The Georgia Institute of Technology 
+ * ECE 4180 Embeded Systems
+ * Professor Hamblen
+ * 03/28/2014
+ * 
+ * @section LICENSE
+ * ----------------------------------------------------------------------------
+ * "THE BEER-WARE LICENSE" (Revision 42):
+ * <alanhlai91@gmail.com> wrote this file. As long as you retain this notice you
+ * can do whatever you want with this stuff. If we meet some day, and you think
+ * this stuff is worth it, you can buy me a beer in return.
+ * ----------------------------------------------------------------------------
+ *
+ *
+ * @section DESCRIPTION
+ *
+ * Honeywell HTU21D Humidity and Temperature sensor.
+ *
+ * Datasheet, specs, and information:
+ *
+ * https://www.sparkfun.com/products/12064
+ */
+
+#ifndef HTU21D_H
+#define HTU21D_H
+
+/**
+ * Includes
+ */
+#include "mbed.h"
+
+/**
+ * Defines
+ */
+// Acquired from Datasheet.
+
+#define HTU21D_I2C_ADDRESS  0x40 
+#define TRIGGER_TEMP_MEASURE  0xE3
+#define TRIGGER_HUMD_MEASURE  0xE5
+
+
+//Commands.
+#define HTU21D_EEPROM_WRITE 0x80
+#define HTU21D_EEPROM_READ  0x81
+
+
+/**
+ * Honeywell HTU21D digital humidity and temperature sensor.
+ */
+class HTU21D {
+
+public:
+
+    /**
+     * Constructor.
+     *
+     * @param sda mbed pin to use for SDA line of I2C interface.
+     * @param scl mbed pin to use for SCL line of I2C interface.
+     */
+    HTU21D(PinName sda, PinName scl);
+
+
+    //Samples the temperature, input void, outputs an int in celcius.
+    int sample_ctemp(void);
+    
+       //Samples the temperature, input void, outputs an int in fahrenheit.
+    int sample_ftemp(void);
+    
+       //Samples the temperature, input void, outputs an int in kelvin.
+    int sample_ktemp(void);
+    
+    //Samples the humidity, input void, outputs and int.
+    int sample_humid(void);
+
+   
+
+private:
+
+    I2C* i2c_;
+
+    /**
+     * Write to EEPROM or RAM on the device.
+     *
+     * @param EepromOrRam 0x80 -> Writing to EEPROM
+     * @param address Address to write to.
+     * @param data Data to write.
+     */
+    void write(int EepromOrRam, int address, int data);
+
+    /**
+     * Read EEPROM or RAM on the device.
+     *
+     * @param EepromOrRam 0x81 -> Reading from EEPROM
+     * @param address Address to read from.
+     * @return The contents of the memory address.
+     */
+    int read(int EepromOrRam, int address);
+
+};
+
+#endif /* HTU21D_H */