Example #1
0
/**
 * Reads current temperature-corrected relative humidity
 */
float SHT1x::readHumidity()
{
  int _val;                    // Raw humidity value returned from sensor
  float _linearHumidity;       // Humidity with linear correction applied
  float _correctedHumidity;    // Temperature-corrected humidity
  float _temperature;          // Raw temperature value

  // Conversion coefficients from SHT15 datasheet
  const float C1 = -4.0;       // for 12 Bit
  const float C2 =  0.0405;    // for 12 Bit
  const float C3 = -0.0000028; // for 12 Bit
  const float T1 =  0.01;      // for 14 Bit @ 5V
  const float T2 =  0.00008;   // for 14 Bit @ 5V

  // Command to send to the SHT1x to request humidity
  int _gHumidCmd = 0b00000101;

  // Fetch the value from the sensor
  sendCommandSHT(_gHumidCmd, _dataPin, _clockPin);
  waitForResultSHT(_dataPin);
  _val = getData16SHT(_dataPin, _clockPin);
  skipCrcSHT(_dataPin, _clockPin);

  // Apply linear conversion to raw value
  _linearHumidity = C1 + C2 * _val + C3 * _val * _val;

  // Get current temperature for humidity correction
  _temperature = readTemperatureC();

  // Correct humidity value for current temperature
  _correctedHumidity = _temperature * (T1 + T2 * _val) + _linearHumidity;

  return (_correctedHumidity);
}
Example #2
0
/**
 * Reads current temperature-corrected relative humidity
 */
float sensirion::readHumidity()
{
  uint16_t _val;                    // Raw humidity value returned from sensor
  float _linearHumidity;       // Humidity with linear correction applied
  float _correctedHumidity;    // Temperature-corrected humidity
  float _temperature;          // Raw temperature value

  // Conversion coefficients from SHT15 datasheet
  const float C1 = -4.0;       // for 12 Bit
  const float C2 =  0.0405;    // for 12 Bit
  const float C3 = -0.0000028; // for 12 Bit
  const float T1 =  0.01;      // for 14 Bit @ 5V
  const float T2 =  0.00008;   // for 14 Bit @ 5V

  // Command to send to the SHT1x to request humidity
  const uint8_t _gHumidCmd = 0b00000101;

  // check cache
  if ( millis() - _lastHumMillis < SHT_CACHE_MILLIS){
	  return _lastHumVal;

  } else{
	  // Fetch the value from the sensor
	  if ( sendCommandSHT(_gHumidCmd, _dataPin, _clockPin) == SHT_SUCCESS ){
		  waitForResultSHT(_dataPin);
		  _val = getData16SHT(_dataPin, _clockPin);

		  uint8_t rxcrc = rev8bits( readCRC(_dataPin, _clockPin ) );
		  uint8_t mycrc = crc8add( 0, _gHumidCmd );
		  mycrc = crc8add( mycrc, _val );
		  if (mycrc != rxcrc){
			Serial.println("SHT: crc error");
			Serial.print(" Got:      0x"); Serial.println(rxcrc, HEX);
			Serial.print(" Expected: 0x"); Serial.println(mycrc, HEX);
			return NAN;
		  }

		  // Apply linear conversion to raw value
		  _linearHumidity = C1 + C2 * _val + C3 * _val * _val;

			// Get current temperature for humidity correction
		  _temperature = readTemperatureC();

		  // Correct humidity value for current temperature
		  _correctedHumidity = (_temperature - 25.0 ) * (T1 + T2 * _val) + _linearHumidity;

		  // cache
		  _lastHumVal = _correctedHumidity;
		  _lastHumMillis = millis();

		  return (_correctedHumidity);
	  }
  }
  //error
  return NAN;
}