Y SI / lib_SHT25

Dependents:   lib_SHT25_example

Committer:
YSI
Date:
Wed Sep 22 13:26:04 2021 +0000
Revision:
8:bb3dbc86a180
Parent:
7:b55223269e6b
Child:
9:382955a266cb
fix maximum I2C frequency to 400KHz; reduce I2C read request at one by data request

Who changed what in which revision?

UserRevisionLine numberNew contents of line
YSI 0:71da3fbfe141 1 /** SHT25 class
YSI 0:71da3fbfe141 2 *
YSI 0:71da3fbfe141 3 * @purpose library for SHT25 humidity and temperature sensor
YSI 0:71da3fbfe141 4 *
YSI 0:71da3fbfe141 5 * Use to get temperature and humidity
YSI 0:71da3fbfe141 6 *
YSI 0:71da3fbfe141 7 * https://www.sensirion.com/products/humidity-sensor/
YSI 0:71da3fbfe141 8 *
YSI 0:71da3fbfe141 9 * Example:
YSI 0:71da3fbfe141 10 * @code
YSI 0:71da3fbfe141 11 * #include "lib_SHT25.h"
YSI 0:71da3fbfe141 12 *
YSI 0:71da3fbfe141 13 * Serial pc(USBTX, USBRX);
YSI 3:81324c19d870 14 * SHT25 sensor(I2C_SDA, I2C_SCL);
YSI 0:71da3fbfe141 15 *
YSI 0:71da3fbfe141 16 * int main()
YSI 0:71da3fbfe141 17 * {
YSI 0:71da3fbfe141 18 * float temperature, humidity;
YSI 0:71da3fbfe141 19 * while(1)
YSI 0:71da3fbfe141 20 * {
YSI 3:81324c19d870 21 * temperature = sensor.getTemperature();
YSI 3:81324c19d870 22 * humidity = sensor.getTemperature();
YSI 3:81324c19d870 23 * pc.printf("\r\ntemperature = %6.2f%cC -|- humidity = %6.2f%%RH", temperature, 248, humidity);
YSI 3:81324c19d870 24 * sensor.waitSafeHeat();
YSI 0:71da3fbfe141 25 * sensor.getData(&temperature, &humidity);
YSI 3:81324c19d870 26 * pc.printf("\ntemperature = %6.2f%cC -|- humidity = %6.2f%%RH", temperature, 248, humidity);
YSI 3:81324c19d870 27 * sensor.waitSafeHeat();
YSI 0:71da3fbfe141 28 * }
YSI 0:71da3fbfe141 29 * }
YSI 0:71da3fbfe141 30 * @endcode
YSI 1:ebd95757fba4 31 * @file lib_SHT25.cpp
YSI 0:71da3fbfe141 32 * @date Jun 2018
YSI 0:71da3fbfe141 33 * @author Yannic Simon
YSI 0:71da3fbfe141 34 */
YSI 0:71da3fbfe141 35 #include "lib_SHT25.h"
YSI 0:71da3fbfe141 36
YSI 8:bb3dbc86a180 37 SHT25::SHT25(PinName sda, PinName scl, enum_sht_prec precision, int frequency) : _i2c(sda, scl)
YSI 0:71da3fbfe141 38 {
YSI 8:bb3dbc86a180 39 _i2c.frequency((frequency<=SHT_I2C_FREQUENCY)?frequency:SHT_I2C_FREQUENCY);
YSI 8:bb3dbc86a180 40 setPrecision(precision);
YSI 7:b55223269e6b 41 _temperature = _humidity = NAN;
YSI 7:b55223269e6b 42 _selfHeatTemperature = _selfHeatHumidity = false;
YSI 7:b55223269e6b 43 _t.attach(callback(this, &SHT25::keepSafeTemperature), SHT_SELF_HEATING);
YSI 7:b55223269e6b 44 _h.attach(callback(this, &SHT25::keepSafeHumidity), SHT_SELF_HEATING);
YSI 0:71da3fbfe141 45 }
YSI 0:71da3fbfe141 46
YSI 0:71da3fbfe141 47 void SHT25::getData(float *tempC, float *relHumidity)
YSI 0:71da3fbfe141 48 {
YSI 7:b55223269e6b 49 *tempC = _temperature;
YSI 7:b55223269e6b 50 *relHumidity = _humidity;
YSI 3:81324c19d870 51 if(_selfHeatTemperature && _selfHeatHumidity)
YSI 0:71da3fbfe141 52 readData(tempC, relHumidity);
YSI 0:71da3fbfe141 53 }
YSI 0:71da3fbfe141 54
YSI 0:71da3fbfe141 55 void SHT25::readData(float *tempC, float *relHumidity)
YSI 0:71da3fbfe141 56 {
YSI 2:34e14b4cd981 57 *tempC = _temperature = readTemperature();
YSI 2:34e14b4cd981 58 *relHumidity = _humidity = readHumidity();
YSI 0:71da3fbfe141 59 }
YSI 0:71da3fbfe141 60
YSI 7:b55223269e6b 61 float SHT25::getTemperature(void)
YSI 7:b55223269e6b 62 {
YSI 7:b55223269e6b 63 if(_selfHeatTemperature)
YSI 7:b55223269e6b 64 _temperature = readTemperature();
YSI 7:b55223269e6b 65 return _temperature;
YSI 7:b55223269e6b 66 }
YSI 7:b55223269e6b 67
YSI 8:bb3dbc86a180 68 float SHT25::readTemperature(void) // if I2C Freezing go down PullUp resistor to 1K or slow frequency
YSI 7:b55223269e6b 69 {
YSI 7:b55223269e6b 70 char cmd[1] = {SHT_TRIG_TEMP_NHOLD}, rx[3] = {0xFF, 0xFF, 0xFF};
YSI 7:b55223269e6b 71 if(!_i2c.write(SHT_I2C_ADDR_WRITE, cmd, 1, false))
YSI 7:b55223269e6b 72 {
YSI 8:bb3dbc86a180 73 wait_us(SHT_TEMP_MEASURE);
YSI 8:bb3dbc86a180 74 _i2c.read(SHT_I2C_ADDR_READ, rx, 3, false);
YSI 7:b55223269e6b 75 _selfHeatTemperature = false;
YSI 7:b55223269e6b 76 _t.attach(callback(this, &SHT25::keepSafeTemperature), SHT_SELF_HEATING);
YSI 7:b55223269e6b 77 return -46.85f + 175.72f * ((((rx[0] << 8) | rx[1]) & 0xFFFC) / 65536.0f);
YSI 7:b55223269e6b 78 }
YSI 7:b55223269e6b 79 return NAN;
YSI 7:b55223269e6b 80 }
YSI 7:b55223269e6b 81
YSI 7:b55223269e6b 82 float SHT25::getHumidity(void)
YSI 7:b55223269e6b 83 {
YSI 7:b55223269e6b 84 if(_selfHeatHumidity)
YSI 7:b55223269e6b 85 _humidity = readHumidity();
YSI 7:b55223269e6b 86 return _humidity;
YSI 7:b55223269e6b 87 }
YSI 7:b55223269e6b 88
YSI 8:bb3dbc86a180 89 float SHT25::readHumidity(void) // if I2C Freezing go down PullUp resistor to 1K or slow frequency
YSI 7:b55223269e6b 90 {
YSI 7:b55223269e6b 91 char cmd[1] = {SHT_TRIG_RH_NHOLD}, rx[3] = {0xFF, 0xFF, 0xFF};
YSI 7:b55223269e6b 92 if(!_i2c.write(SHT_I2C_ADDR_WRITE, cmd, 1, false))
YSI 7:b55223269e6b 93 {
YSI 8:bb3dbc86a180 94 wait_us(SHT_HUM_MEASURE);
YSI 8:bb3dbc86a180 95 _i2c.read(SHT_I2C_ADDR_READ, rx, 3, false);
YSI 7:b55223269e6b 96 _selfHeatHumidity = false;
YSI 7:b55223269e6b 97 _h.attach(callback(this, &SHT25::keepSafeHumidity), SHT_SELF_HEATING);
YSI 7:b55223269e6b 98 return -6.0f + 125.0f * ((((rx[0] << 8) | rx[1]) & 0xFFFC) / 65536.0f);
YSI 7:b55223269e6b 99 }
YSI 7:b55223269e6b 100 return NAN;
YSI 7:b55223269e6b 101 }
YSI 7:b55223269e6b 102
YSI 5:8e34f365eb45 103 bool SHT25::setPrecision(const enum_sht_prec precision)
YSI 0:71da3fbfe141 104 {
YSI 7:b55223269e6b 105 char cmd[2] = {SHT_WRITE_REG_USER, precision};
YSI 7:b55223269e6b 106 return !_i2c.write(SHT_I2C_ADDR_WRITE, cmd, 2, false);
YSI 0:71da3fbfe141 107 }
YSI 0:71da3fbfe141 108
YSI 0:71da3fbfe141 109 bool SHT25::softReset()
YSI 0:71da3fbfe141 110 {
YSI 7:b55223269e6b 111 char cmd[1] = {SHT_SOFT_RESET};
YSI 7:b55223269e6b 112 return !_i2c.write(SHT_I2C_ADDR_WRITE, cmd, 1, false);
YSI 0:71da3fbfe141 113 }
YSI 0:71da3fbfe141 114
YSI 3:81324c19d870 115 void SHT25::waitSafeHeat(void)
YSI 3:81324c19d870 116 {
YSI 7:b55223269e6b 117 while(!_selfHeatTemperature || !_selfHeatHumidity) __NOP();
YSI 3:81324c19d870 118 }
YSI 3:81324c19d870 119
YSI 3:81324c19d870 120 void SHT25::keepSafeTemperature(void)
YSI 0:71da3fbfe141 121 {
YSI 3:81324c19d870 122 _selfHeatTemperature = true;
YSI 3:81324c19d870 123 }
YSI 3:81324c19d870 124
YSI 3:81324c19d870 125 void SHT25::keepSafeHumidity(void)
YSI 3:81324c19d870 126 {
YSI 3:81324c19d870 127 _selfHeatHumidity = true;
YSI 0:71da3fbfe141 128 }