|
| 1 | +/* |
| 2 | + SHT2x - A Humidity Library for Arduino. |
| 3 | +
|
| 4 | + Supported Sensor modules: |
| 5 | + SHT21-Breakout Module - http://www.moderndevice.com/products/sht21-humidity-sensor |
| 6 | + SHT2x-Breakout Module - http://www.misenso.com/products/001 |
| 7 | +
|
| 8 | + Created by Christopher Ladden at Modern Device on December 2009. |
| 9 | + Modified by Paul Badger March 2010 |
| 10 | + |
| 11 | + Modified by www.misenso.com on October 2011: |
| 12 | + - code optimisation |
| 13 | + - compatibility with Arduino 1.0 |
| 14 | +
|
| 15 | + * This file is part of Sodaq_SHT2x. |
| 16 | + * |
| 17 | + * Sodaq_SHT2x is free software: you can redistribute it and/or |
| 18 | + * modify it under the terms of the GNU Lesser General Public License |
| 19 | + * as published by the Free Software Foundation, either version 3 of |
| 20 | + * the License, or(at your option) any later version. |
| 21 | + * |
| 22 | + * Sodaq_SHT2x is distributed in the hope that it will be useful, |
| 23 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 25 | + * GNU Lesser General Public License for more details. |
| 26 | + * |
| 27 | + * You should have received a copy of the GNU Lesser General Public |
| 28 | + * License along with Sodaq_SHT2x. If not, see |
| 29 | + * <http://www.gnu.org/licenses/>. |
| 30 | + */ |
| 31 | + |
| 32 | +#include <stdint.h> |
| 33 | +#include <math.h> |
| 34 | +#include <Arduino.h> |
| 35 | +#include <Wire.h> |
| 36 | +#include "SHT2x.h" |
| 37 | + |
| 38 | +// Specify the constants for water vapor and barometric pressure. |
| 39 | +#define WATER_VAPOR 17.62f |
| 40 | +#define BAROMETRIC_PRESSURE 243.5f |
| 41 | + |
| 42 | +typedef enum { |
| 43 | + eSHT2xAddress = 0x40, |
| 44 | +} HUM_SENSOR_T; |
| 45 | + |
| 46 | +typedef enum { |
| 47 | + eTempHoldCmd = 0xE3, |
| 48 | + eRHumidityHoldCmd = 0xE5, |
| 49 | + eTempNoHoldCmd = 0xF3, |
| 50 | + eRHumidityNoHoldCmd = 0xF5, |
| 51 | +} HUM_MEASUREMENT_CMD_T; |
| 52 | + |
| 53 | + |
| 54 | +/****************************************************************************** |
| 55 | + * Global Functions |
| 56 | + ******************************************************************************/ |
| 57 | + |
| 58 | +/********************************************************** |
| 59 | + * GetHumidity |
| 60 | + * Gets the current humidity from the sensor. |
| 61 | + * |
| 62 | + * @return float - The relative humidity in %RH |
| 63 | + **********************************************************/ |
| 64 | +float SHT2xClass::GetHumidity(void) |
| 65 | +{ |
| 66 | + float value = readSensor(eRHumidityHoldCmd); |
| 67 | + if (value == 0) { |
| 68 | + return 0; // Some unrealistic value |
| 69 | + } |
| 70 | + return -6.0 + 125.0 / 65536.0 * value; |
| 71 | +} |
| 72 | + |
| 73 | +/********************************************************** |
| 74 | + * GetTemperature |
| 75 | + * Gets the current temperature from the sensor. |
| 76 | + * |
| 77 | + * @return float - The temperature in Deg C |
| 78 | + **********************************************************/ |
| 79 | +float SHT2xClass::GetTemperature(void) |
| 80 | +{ |
| 81 | + float value = readSensor(eTempHoldCmd); |
| 82 | + if (value == 0) { |
| 83 | + return -273; // Roughly Zero Kelvin indicates an error |
| 84 | + } |
| 85 | + return -46.85 + 175.72 / 65536.0 * value; |
| 86 | +} |
| 87 | + |
| 88 | +/********************************************************** |
| 89 | + * GetDewPoint |
| 90 | + * Gets the current dew point based on the current humidity and temperature |
| 91 | + * |
| 92 | + * @return float - The dew point in Deg C |
| 93 | + **********************************************************/ |
| 94 | +float SHT2xClass::GetDewPoint(void) |
| 95 | +{ |
| 96 | + float humidity = GetHumidity(); |
| 97 | + float temperature = GetTemperature(); |
| 98 | + |
| 99 | + // Calculate the intermediate value 'gamma' |
| 100 | + float gamma = log(humidity / 100) + WATER_VAPOR * temperature / (BAROMETRIC_PRESSURE + temperature); |
| 101 | + // Calculate dew point in Celsius |
| 102 | + float dewPoint = BAROMETRIC_PRESSURE * gamma / (WATER_VAPOR - gamma); |
| 103 | + |
| 104 | + return dewPoint; |
| 105 | +} |
| 106 | + |
| 107 | +/****************************************************************************** |
| 108 | + * Private Functions |
| 109 | + ******************************************************************************/ |
| 110 | + |
| 111 | +uint16_t SHT2xClass::readSensor(uint8_t command) |
| 112 | +{ |
| 113 | + uint16_t result; |
| 114 | + |
| 115 | + Wire.beginTransmission(eSHT2xAddress); |
| 116 | + Wire.write(command); |
| 117 | + delay(100); |
| 118 | + Wire.endTransmission(); |
| 119 | + |
| 120 | + Wire.requestFrom(eSHT2xAddress, 3); |
| 121 | + uint32_t timeout = millis() + 300; // Don't hang here for more than 300ms |
| 122 | + while (Wire.available() < 3) { |
| 123 | + if ((millis() - timeout) > 0) { |
| 124 | + return 0; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + //Store the result |
| 129 | + result = Wire.read() << 8; |
| 130 | + result += Wire.read(); |
| 131 | + result &= ~0x0003; // clear two low bits (status bits) |
| 132 | + |
| 133 | + //Clear the final byte from the buffer |
| 134 | + Wire.read(); |
| 135 | + |
| 136 | + return result; |
| 137 | +} |
| 138 | + |
| 139 | +SHT2xClass SHT2x; |
0 commit comments