HIH6130 Humidity/Temperature Sensor library

Dependents:   BLE_HIH6130_tinkering mbed_rifletool

Yet another HIH6130 library!

Honeywell HIH6130 is a relative humidity and temperature sensor which can be addressed through an I2C interface.

This library is based on the Honeywell documentation (/media/uploads/spiridion/i2c_comms_humidicon_tn_009061-2-en_final_07jun12.pdf) and as been tested on LPC1768 and FRM-KL25Z plateforms using the HIH6130 Sparkfun breakout (https://www.sparkfun.com/products/11295).

Files at this revision

API Documentation at this revision

Comitter:
spiridion
Date:
Sun Mar 09 16:53:11 2014 +0000
Commit message:
first release

Changed in this revision

HIH6130.cpp Show annotated file Show diff for this revision Revisions of this file
HIH6130.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r ed5a906c8e44 HIH6130.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HIH6130.cpp	Sun Mar 09 16:53:11 2014 +0000
@@ -0,0 +1,84 @@
+/*
+  @file HIH6130.cpp
+  
+  @brief Humidity and Temperature Sensor HIH6130 Breakout I2C Library      
+
+  @Author spiridion (http://mailme.spiridion.net)
+
+  Tested on LPC1768 and FRDM-KL25Z
+  
+  Copyright (c) 2014 spiridion
+  Released under the MIT License (see http://mbed.org/license/mit)
+
+  Documentation regarding I2C communication with HIH6130 can be found here: 
+  http://mbed.org/media/uploads/spiridion/i2c_comms_humidicon_tn_009061-2-en_final_07jun12.pdf
+*/
+ 
+#include "HIH6130.h"
+#include "mbed.h"
+
+HIH6130::HIH6130(PinName sda, PinName scl, int address) : m_i2c(sda,scl), m_addr(address)
+{
+    m_temperature = UNSET_HI6130_TEMPERATURE_VALUE;
+    m_humidity = UNSET_HI6130_HUMIDITY_VALUE;
+}
+
+HIH6130::HIH6130(I2C& i2c, int address) : m_i2c(i2c), m_addr(address)
+{
+    m_temperature = UNSET_HI6130_TEMPERATURE_VALUE;
+    m_humidity = UNSET_HI6130_HUMIDITY_VALUE;
+}
+
+int HIH6130::ReadData(float* pTemperature, float* pHumidity)
+{
+    int rsl = Measurement();
+    
+    if (rsl)
+    { 
+        m_temperature = TrueTemperature();
+        m_humidity = TrueHumidity();
+    }
+    else
+    {
+        m_temperature = UNSET_HI6130_TEMPERATURE_VALUE;
+        m_humidity = UNSET_HI6130_HUMIDITY_VALUE;
+    }
+    
+    if (pTemperature)
+        *pTemperature = m_temperature;
+    if (pHumidity)
+        *pHumidity = m_humidity;
+
+    return rsl;
+}
+
+float HIH6130::TrueTemperature()
+{   
+    // T = T_output / (2^14-2)*165-40
+    return ( ( ((unsigned int)m_data[2] << 8) | (unsigned int)m_data[3] ) >> 2 ) * 0.010072F - 40;  
+}
+
+float HIH6130::TrueHumidity()
+{
+    // H = H_output /(2^14-2)*100
+    return ( (((unsigned int)m_data[0] & 0x3f) << 8) | ((unsigned int)m_data[1] & 0xff) ) * 0.006104F;
+}
+
+int HIH6130::Measurement()
+{
+    int errors;
+
+    // Humidity and temperature measurement request
+    errors = m_i2c.write(m_addr, m_data, 1);
+    
+    wait_ms(10);
+    
+    // Humidity and temperature data fetch
+    errors += m_i2c.read(m_addr, m_data, 4);
+
+    //  Check data validity
+    if ( errors || !(m_data[0] & 0xC0)) 
+        return 0;
+        
+    return 1;
+}
diff -r 000000000000 -r ed5a906c8e44 HIH6130.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HIH6130.h	Sun Mar 09 16:53:11 2014 +0000
@@ -0,0 +1,121 @@
+/*
+  @file HIH6130.h
+  
+  @brief Humidity and Temperature Sensor HIH6130 Breakout I2C Library      
+
+  @Author spiridion (http://mailme.spiridion.net)
+
+  Tested on LPC1768 and FRDM-KL25Z
+  
+  Copyright (c) 2014 spiridion
+  Released under the MIT License (see http://mbed.org/license/mit)
+
+  Documentation regarding I2C communication with HIH6130 can be found here: 
+  http://mbed.org/media/uploads/spiridion/i2c_comms_humidicon_tn_009061-2-en_final_07jun12.pdf
+*/
+
+#ifndef HIH6130_H
+#define HIH6130_H
+
+#include "mbed.h"
+
+///  default address is not 0X27, as stated in the documentation, but 0x4E (0x27<<1 ?) 
+#define HIH6130_I2C_ADDRESS 0x4E 
+
+#define UNSET_HI6130_HUMIDITY_VALUE -100.F
+#define UNSET_HI6130_TEMPERATURE_VALUE -273.15F // absolute zero
+
+/** HIH6130 class.
+ *  Read humidity and temperature from the HIH6130 Breakout I2C sensor
+ *
+ * Example:
+ * @code
+ * #include "mbed.h"
+ * #include "HIH6130.h"
+ * 
+ * #if defined(TARGET_LPC1768)
+ *     #define PIN_SDA p9
+ *     #define PIN_SCL p10
+ * #elif defined(TARGET_KL25Z) // watch out for the PTE0/PTE1 mixed up in the KL25Z doc 
+ *     #define PIN_SDA PTE0
+ *     #define PIN_SCL PTE1
+ * #endif
+ * 
+ * int main() 
+ * {    
+ *     HIH6130 hih6130(PIN_SDA, PIN_SCL);
+ *     float humidity, temperature;
+ *     
+ *     while(1) {        
+ *         if (hih6130.ReadData(&humidity, &temperature))
+ *             printf("Humidity(%%RH): %8.2f \t Temperature(C): %8.2f\n", humidity, temperature);  
+ *         wait(1);
+ *     }
+ * }
+ * @endcode
+ */
+class HIH6130 
+{
+
+public:
+
+    /** Create a HIH6130 instance
+     * @param sda pin 
+     * @param scl pin 
+     * @param address: I2C slave address 
+     */
+    HIH6130(PinName sda, PinName scl, int address = HIH6130_I2C_ADDRESS); 
+
+    /** Create a HIH6130 instance
+     * @param i2c object
+     * @param address: I2C slave address 
+     */
+    HIH6130(I2C& i2c, int address = HIH6130_I2C_ADDRESS); 
+
+    /** Read relative humidity and temperature from the HIH6130.
+     * @param humidity (%RH) 
+     * @param temperature (C) 
+     * @returns
+     *   1 on success,
+     *   0 on error
+     */    
+    int ReadData(float* pTemperature=NULL, float* pHumidity=NULL);
+
+    /** Get temperature from a previous measurement 
+     *  
+     * @returns
+     *   temperature (C)
+     */    
+    float GetTemperature() {return m_temperature;}
+
+    /** Get relative humidity from a previous measurement
+     *
+     * @returns
+     *   relative humidity (%RH)
+     */    
+    float GetHumidity() {return m_humidity;}; 
+
+protected:
+
+    /** Measurement request and data fetch
+     */    
+    int Measurement();
+
+    /** Calculation of the temperature from the digital output
+     */    
+    float TrueTemperature();
+    
+    /** Calculation of the humidity from the digital output
+     */    
+    float TrueHumidity();
+    
+    float m_temperature;  
+    float m_humidity;  
+
+    I2C m_i2c;   
+    int m_addr;  
+     
+    char m_data[4];    
+};
+
+#endif