Y SI / lib_SHT25

Dependents:   lib_SHT25_example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lib_SHT25.h Source File

lib_SHT25.h

Go to the documentation of this file.
00001 /** SHT25 class
00002 *
00003 * @purpose       library for SHT25 humidity and temperature sensor
00004 *
00005 * Use to get temperature and humidity
00006 *
00007 * https://www.sensirion.com/products/humidity-sensor/
00008 *
00009 * Example:
00010 * @code
00011 * #include "lib_SHT25.h"
00012 * 
00013 * SHT25  sensor(I2C_SDA, I2C_SCL);
00014 * 
00015 * int main()
00016 * {
00017 *     while(1)
00018 *     {
00019 *         sensor.waitSafeHeat();
00020 *         float temperature = sensor.getTemperature(), humidity = sensor.getHumidity();
00021 *         printf("\r\ntemperature = %6.2f%cC -|- humidity = %6.2f%%RH", temperature, 248, humidity);
00022 *         sensor.waitSafeHeat();
00023 *         sensor.getData(&temperature, &humidity);
00024 *         printf("\r\ntemperature = %6.2f%cC -|- humidity = %6.2f%%RH", temperature, 248, humidity);
00025 *     }
00026 * }
00027 * @endcode
00028 * @file          lib_SHT25.h 
00029 * @date          Jun 2018
00030 * @author        Yannic Simon
00031 */
00032 #ifndef SHT25_H
00033 #define SHT25_H
00034 
00035 #include "mbed.h"
00036 
00037 #define SHT_I2C_FREQUENCY   100e3   //Sensor I2C Frequency max 400KHz
00038 #define SHT_I2C_ADDR        0x80    //Sensor I2C address
00039 #define SHT_TRIG_TEMP_HOLD  0xE3    //Trigger Temp  with hold master
00040 #define SHT_TRIG_RH_HOLD    0xE5    //Trigger RH    with hold master
00041 #define SHT_TRIG_TEMP_NHOLD 0xF3    //Trigger Temp  with no hold master
00042 #define SHT_TRIG_RH_NHOLD   0xF5    //Trigger RH    with no hold master
00043 #define SHT_WRITE_REG_USER  0xE6    //Write to user register
00044 #define SHT_READ_REG_USER   0xE7    //Read from user register
00045 #define SHT_SOFT_RESET      0xFE    //Soft reset the sensor
00046 #if MBED_MAJOR_VERSION > 5
00047 #define SHT_SELF_HEATING    2s      //Keep self heating
00048 #define SHT_WAIT(ms)        (thread_sleep_for(ms))
00049 #else
00050 #define SHT_SELF_HEATING    0x01    //Keep self heating
00051 #define SHT_WAIT(ms)        (wait_us(1000*(ms)))
00052 #endif
00053 
00054 
00055 /** SHT25 class
00056  */
00057 class SHT25
00058 {
00059     public:
00060         /** enumerator of the different precision of the sensor
00061         */
00062         typedef enum { SHT_PREC_RH12T14 = 0x00, SHT_PREC_RH08T12 = 0x01, SHT_PREC_RH10T13 = 0x80, SHT_PREC_RH11T11 = 0x81 }
00063             enum_sht_prec;
00064         /** make new SHT25 instance
00065         * connected to sda, scl I2C pins
00066         *
00067         * @param sda I2C pin
00068         * @param scl I2C pin
00069         * @param precision SHT25 precision for humidity(default 12 bits) and temperature(default 14 bits)
00070         * @param frequency I2C frequency, default 100KHz and maximum 400KHz
00071         */
00072         SHT25(PinName sda, PinName scl, enum_sht_prec precision = SHT_PREC_RH12T14, int frequency = SHT_I2C_FREQUENCY);
00073         
00074         /** return Temperature(°C) and Humidity
00075         *
00076         * @param tempC address to return Temperature
00077         * @param relHumidity address to return Humidity
00078         * @returns none
00079         */ 
00080         void getData(float *tempC, float *relHumidity);
00081         
00082         /** return Temperature(°C)
00083         *
00084         * @param none
00085         * @returns Temperature(°C)
00086         */  
00087         float getTemperature(void);
00088         
00089         /** return Humidity
00090         *
00091         * @param none
00092         * @returns Humidity
00093         */  
00094         float getHumidity(void);
00095         
00096         /** set data precision 
00097         *
00098         * @param precision { SHT_PREC_RH12T14 = 0x00, SHT_PREC_RH08T12 = 0x01, SHT_PREC_RH10T13 = 0x80, SHT_PREC_RH11T11 = 0x81 }
00099         * @returns true on I2C acknoledge
00100         */  
00101         bool setPrecision(const enum_sht_prec precision);
00102         
00103         /** soft reset the sensor
00104         *
00105         * @param none
00106         * @returns true on I2C acknoledge
00107         */
00108         bool softReset(void);
00109         
00110         /** wait safe heat for sensor
00111         *
00112         * @param none
00113         * @returns none
00114         */
00115         void waitSafeHeat(void);
00116     protected:
00117         I2C     _i2c;
00118         Timeout _t, _h;
00119     private:
00120         void  readData(void);
00121         float readTemperature(void);
00122         float readHumidity(void);
00123         void  keepSafeTemperature(void);
00124         void  keepSafeHumidity(void);
00125         float _temperature, _humidity;
00126         bool  _selfHeatTemperature, _selfHeatHumidity;
00127 };
00128 
00129 #endif