├── CHANGES.txt ├── DISTRIBUTION ├── README.markdown ├── SHT1x.cpp ├── SHT1x.h ├── examples └── ReadSHT1xValues │ └── ReadSHT1xValues.pde └── keywords.txt /CHANGES.txt: -------------------------------------------------------------------------------- 1 | 2011-09-20 2 | * Conditionally include Arduino.h for compatibility with Arduino 1.0 3 | 4 | 2010-07-23 5 | * Added SHT7x to list of supported sensors. 6 | * Fixed temperature offset in humidity calculation. 7 | -------------------------------------------------------------------------------- /DISTRIBUTION: -------------------------------------------------------------------------------- 1 | Copyright 2009 Jonathan Oxer / 2 | Copyright 2008 Maurice Ribble / 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | http://www.gnu.org/licenses/ 10 | -------------------------------------------------------------------------------- /README.markdown: -------------------------------------------------------------------------------- 1 | SHT1x Temperature / Humidity Sensor Library for Arduino 2 | ======================================================= 3 | Copyright 2009 Jonathan Oxer jon@oxer.com.au / http://www.practicalarduino.com 4 | Copyright 2008 Maurice Ribble ribblem@yahoo.com / http://www.glacialwanderer.com 5 | 6 | Provides a simple interface to the SHT1x series (SHT10, SHT11, SHT15) 7 | and SHT7x series (SHT71, SHT75) temperature / humidity sensors from 8 | Sensirion, http://www.sensirion.com. These sensors use a "2-wire" 9 | communications buss that is similar to I2C and can co-exist on the same 10 | physical wire as I2C devices. 11 | 12 | Installation 13 | ------------ 14 | Download the directory "SHT1x" and move it into the "libraries" 15 | directory inside your sketchbook directory, then restart the Arduino 16 | IDE. You will then see it listed under File->Examples->SHT1x. 17 | 18 | Usage 19 | ----- 20 | The library is instantiated as an object with methods provided to read 21 | relative humidity and temperature. Include it in your sketch and then 22 | create an object, specifying the pins to use for communication with the 23 | sensor: 24 | 25 | #include 26 | #define dataPin 10 27 | #define clockPin 11 28 | SHT1x sht1x(dataPin, clockPin); 29 | 30 | You can then call methods on that object within your program. In this 31 | example we created an object called "sht1x", but it could have been 32 | called whatever you like. A complete example program is included with 33 | the library and can be accessed from the File->Examples->SHT1x menu. 34 | 35 | ### readTemperatureC() ### 36 | 37 | Returns a float within the valid range of the sensor of -40 to +123.8C. 38 | A value of -40 is returned in the event of a communication error with 39 | the sensor. 40 | 41 | Example: 42 | 43 | float tempC = sht1x.readTemperatureC(); 44 | 45 | ### readTemperatureF() ### 46 | 47 | Returns a float within the valid range of the sensor of -40 to +254.9F. 48 | A value of -40 is returned in the event of a communication error with 49 | the sensor. 50 | 51 | Example: 52 | 53 | float tempF = sht1x.readTemperatureF(); 54 | 55 | ### readHumidity() ### 56 | 57 | Returns a float within the valid range of the sensor of 0 to 100%. 58 | A negative value is returned in the event of a communication error with 59 | the sensor. 60 | 61 | Example: 62 | 63 | float humidity = sht1x.readHumidity(); 64 | -------------------------------------------------------------------------------- /SHT1x.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * SHT1x Library 3 | * 4 | * Copyright 2009 Jonathan Oxer / 5 | * Based on previous work by: 6 | * Maurice Ribble: 7 | * Wayne ?: 8 | * 9 | * Manages communication with SHT1x series (SHT10, SHT11, SHT15) 10 | * temperature / humidity sensors from Sensirion (www.sensirion.com). 11 | */ 12 | #if (ARDUINO >= 100) 13 | #include 14 | #else 15 | #include 16 | #endif 17 | 18 | #include "SHT1x.h" 19 | 20 | SHT1x::SHT1x(int dataPin, int clockPin) 21 | { 22 | _dataPin = dataPin; 23 | _clockPin = clockPin; 24 | } 25 | 26 | 27 | /* ================ Public methods ================ */ 28 | 29 | /** 30 | * Reads the current temperature in degrees Celsius 31 | */ 32 | float SHT1x::readTemperatureC() 33 | { 34 | int _val; // Raw value returned from sensor 35 | float _temperature; // Temperature derived from raw value 36 | 37 | // Conversion coefficients from SHT15 datasheet 38 | const float D1 = -40.0; // for 14 Bit @ 5V 39 | const float D2 = 0.01; // for 14 Bit DEGC 40 | 41 | // Fetch raw value 42 | _val = readTemperatureRaw(); 43 | 44 | // Convert raw value to degrees Celsius 45 | _temperature = (_val * D2) + D1; 46 | 47 | return (_temperature); 48 | } 49 | 50 | /** 51 | * Reads the current temperature in degrees Fahrenheit 52 | */ 53 | float SHT1x::readTemperatureF() 54 | { 55 | int _val; // Raw value returned from sensor 56 | float _temperature; // Temperature derived from raw value 57 | 58 | // Conversion coefficients from SHT15 datasheet 59 | const float D1 = -40.0; // for 14 Bit @ 5V 60 | const float D2 = 0.018; // for 14 Bit DEGF 61 | 62 | // Fetch raw value 63 | _val = readTemperatureRaw(); 64 | 65 | // Convert raw value to degrees Fahrenheit 66 | _temperature = (_val * D2) + D1; 67 | 68 | return (_temperature); 69 | } 70 | 71 | /** 72 | * Reads current temperature-corrected relative humidity 73 | */ 74 | float SHT1x::readHumidity() 75 | { 76 | int _val; // Raw humidity value returned from sensor 77 | float _linearHumidity; // Humidity with linear correction applied 78 | float _correctedHumidity; // Temperature-corrected humidity 79 | float _temperature; // Raw temperature value 80 | 81 | // Conversion coefficients from SHT15 datasheet 82 | const float C1 = -4.0; // for 12 Bit 83 | const float C2 = 0.0405; // for 12 Bit 84 | const float C3 = -0.0000028; // for 12 Bit 85 | const float T1 = 0.01; // for 14 Bit @ 5V 86 | const float T2 = 0.00008; // for 14 Bit @ 5V 87 | 88 | // Command to send to the SHT1x to request humidity 89 | int _gHumidCmd = 0b00000101; 90 | 91 | // Fetch the value from the sensor 92 | sendCommandSHT(_gHumidCmd, _dataPin, _clockPin); 93 | waitForResultSHT(_dataPin); 94 | _val = getData16SHT(_dataPin, _clockPin); 95 | skipCrcSHT(_dataPin, _clockPin); 96 | 97 | // Apply linear conversion to raw value 98 | _linearHumidity = C1 + C2 * _val + C3 * _val * _val; 99 | 100 | // Get current temperature for humidity correction 101 | _temperature = readTemperatureC(); 102 | 103 | // Correct humidity value for current temperature 104 | _correctedHumidity = (_temperature - 25.0 ) * (T1 + T2 * _val) + _linearHumidity; 105 | 106 | return (_correctedHumidity); 107 | } 108 | 109 | 110 | /* ================ Private methods ================ */ 111 | 112 | /** 113 | * Reads the current raw temperature value 114 | */ 115 | float SHT1x::readTemperatureRaw() 116 | { 117 | int _val; 118 | 119 | // Command to send to the SHT1x to request Temperature 120 | int _gTempCmd = 0b00000011; 121 | 122 | sendCommandSHT(_gTempCmd, _dataPin, _clockPin); 123 | waitForResultSHT(_dataPin); 124 | _val = getData16SHT(_dataPin, _clockPin); 125 | skipCrcSHT(_dataPin, _clockPin); 126 | 127 | return (_val); 128 | } 129 | 130 | /** 131 | */ 132 | int SHT1x::shiftIn(int _dataPin, int _clockPin, int _numBits) 133 | { 134 | int ret = 0; 135 | int i; 136 | 137 | for (i=0; i<_numBits; ++i) 138 | { 139 | digitalWrite(_clockPin, HIGH); 140 | delay(10); // I don't know why I need this, but without it I don't get my 8 lsb of temp 141 | ret = ret*2 + digitalRead(_dataPin); 142 | digitalWrite(_clockPin, LOW); 143 | } 144 | 145 | return(ret); 146 | } 147 | 148 | /** 149 | */ 150 | void SHT1x::sendCommandSHT(int _command, int _dataPin, int _clockPin) 151 | { 152 | int ack; 153 | 154 | // Transmission Start 155 | pinMode(_dataPin, OUTPUT); 156 | pinMode(_clockPin, OUTPUT); 157 | digitalWrite(_dataPin, HIGH); 158 | digitalWrite(_clockPin, HIGH); 159 | digitalWrite(_dataPin, LOW); 160 | digitalWrite(_clockPin, LOW); 161 | digitalWrite(_clockPin, HIGH); 162 | digitalWrite(_dataPin, HIGH); 163 | digitalWrite(_clockPin, LOW); 164 | 165 | // The command (3 msb are address and must be 000, and last 5 bits are command) 166 | shiftOut(_dataPin, _clockPin, MSBFIRST, _command); 167 | 168 | // Verify we get the correct ack 169 | digitalWrite(_clockPin, HIGH); 170 | pinMode(_dataPin, INPUT); 171 | ack = digitalRead(_dataPin); 172 | if (ack != LOW) { 173 | //Serial.println("Ack Error 0"); 174 | } 175 | digitalWrite(_clockPin, LOW); 176 | ack = digitalRead(_dataPin); 177 | if (ack != HIGH) { 178 | //Serial.println("Ack Error 1"); 179 | } 180 | } 181 | 182 | /** 183 | */ 184 | void SHT1x::waitForResultSHT(int _dataPin) 185 | { 186 | int i; 187 | int ack; 188 | 189 | pinMode(_dataPin, INPUT); 190 | 191 | for(i= 0; i < 100; ++i) 192 | { 193 | delay(10); 194 | ack = digitalRead(_dataPin); 195 | 196 | if (ack == LOW) { 197 | break; 198 | } 199 | } 200 | 201 | if (ack == HIGH) { 202 | //Serial.println("Ack Error 2"); // Can't do serial stuff here, need another way of reporting errors 203 | } 204 | } 205 | 206 | /** 207 | */ 208 | int SHT1x::getData16SHT(int _dataPin, int _clockPin) 209 | { 210 | int val; 211 | 212 | // Get the most significant bits 213 | pinMode(_dataPin, INPUT); 214 | pinMode(_clockPin, OUTPUT); 215 | val = shiftIn(_dataPin, _clockPin, 8); 216 | val *= 256; 217 | 218 | // Send the required ack 219 | pinMode(_dataPin, OUTPUT); 220 | digitalWrite(_dataPin, HIGH); 221 | digitalWrite(_dataPin, LOW); 222 | digitalWrite(_clockPin, HIGH); 223 | digitalWrite(_clockPin, LOW); 224 | 225 | // Get the least significant bits 226 | pinMode(_dataPin, INPUT); 227 | val |= shiftIn(_dataPin, _clockPin, 8); 228 | 229 | return val; 230 | } 231 | 232 | /** 233 | */ 234 | void SHT1x::skipCrcSHT(int _dataPin, int _clockPin) 235 | { 236 | // Skip acknowledge to end trans (no CRC) 237 | pinMode(_dataPin, OUTPUT); 238 | pinMode(_clockPin, OUTPUT); 239 | 240 | digitalWrite(_dataPin, HIGH); 241 | digitalWrite(_clockPin, HIGH); 242 | digitalWrite(_clockPin, LOW); 243 | } 244 | -------------------------------------------------------------------------------- /SHT1x.h: -------------------------------------------------------------------------------- 1 | /** 2 | * SHT1x Library 3 | * 4 | * Copyright 2009 Jonathan Oxer / 5 | * Based on previous work by: 6 | * Maurice Ribble: 7 | * Wayne ?: 8 | * 9 | * Manages communication with SHT1x series (SHT10, SHT11, SHT15) 10 | * temperature / humidity sensors from Sensirion (www.sensirion.com). 11 | */ 12 | #ifndef SHT1x_h 13 | #define SHT1x_h 14 | 15 | #if (ARDUINO >= 100) 16 | #include 17 | #else 18 | #include 19 | #endif 20 | 21 | class SHT1x 22 | { 23 | public: 24 | SHT1x(int dataPin, int clockPin); 25 | float readHumidity(); 26 | float readTemperatureC(); 27 | float readTemperatureF(); 28 | private: 29 | int _dataPin; 30 | int _clockPin; 31 | int _numBits; 32 | float readTemperatureRaw(); 33 | int shiftIn(int _dataPin, int _clockPin, int _numBits); 34 | void sendCommandSHT(int _command, int _dataPin, int _clockPin); 35 | void waitForResultSHT(int _dataPin); 36 | int getData16SHT(int _dataPin, int _clockPin); 37 | void skipCrcSHT(int _dataPin, int _clockPin); 38 | }; 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /examples/ReadSHT1xValues/ReadSHT1xValues.pde: -------------------------------------------------------------------------------- 1 | /** 2 | * ReadSHT1xValues 3 | * 4 | * Read temperature and humidity values from an SHT1x-series (SHT10, 5 | * SHT11, SHT15) sensor. 6 | * 7 | * Copyright 2009 Jonathan Oxer 8 | * www.practicalarduino.com 9 | */ 10 | 11 | #include 12 | 13 | // Specify data and clock connections and instantiate SHT1x object 14 | #define dataPin 10 15 | #define clockPin 11 16 | SHT1x sht1x(dataPin, clockPin); 17 | 18 | void setup() 19 | { 20 | Serial.begin(38400); // Open serial connection to report values to host 21 | Serial.println("Starting up"); 22 | } 23 | 24 | void loop() 25 | { 26 | float temp_c; 27 | float temp_f; 28 | float humidity; 29 | 30 | // Read values from the sensor 31 | temp_c = sht1x.readTemperatureC(); 32 | temp_f = sht1x.readTemperatureF(); 33 | humidity = sht1x.readHumidity(); 34 | 35 | // Print the values to the serial port 36 | Serial.print("Temperature: "); 37 | Serial.print(temp_c, DEC); 38 | Serial.print("C / "); 39 | Serial.print(temp_f, DEC); 40 | Serial.print("F. Humidity: "); 41 | Serial.print(humidity); 42 | Serial.println("%"); 43 | 44 | delay(2000); 45 | } 46 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | SHT1x KEYWORD1 2 | readHumidity KEYWORD2 3 | readTemperatureC KEYWORD2 4 | readTemperatureF KEYWORD2 5 | --------------------------------------------------------------------------------