forked from practicalarduino/SHT1x
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8f39326
Showing
5 changed files
with
338 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
Copyright 2009 Jonathan Oxer <[email protected]> | ||
|
||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
|
||
http://www.gnu.org/licenses/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
SHT1x | ||
===== | ||
|
||
Copyright 2009 Jonathan Oxer <[email protected]> | ||
www.practicalarduino.com | ||
|
||
Provides a simple interface to the SHT1x series (SHT10, SHT11, SHT15) | ||
temperature / humidity sensors from Sensirion (www.sensirion.com). | ||
These sensors use a "2-wire" communications buss that is similar to I2C | ||
and can co-exist on the same physical wire as I2C devices, but is not | ||
directly interoperable with it. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,238 @@ | ||
/** | ||
* SHT1x Library | ||
* | ||
* Copyright 2009 Jonathan Oxer <[email protected]> / <www.practicalarduino.com> | ||
* Based on previous work by: | ||
* Maurice Ribble: <www.glacialwanderer.com/hobbyrobotics/?p=5> | ||
* Wayne ?: <ragingreality.blogspot.com/2008/01/ardunio-and-sht15.html> | ||
* | ||
* Manages communication with SHT1x series (SHT10, SHT11, SHT15) | ||
* temperature / humidity sensors from Sensirion (www.sensirion.com). | ||
*/ | ||
#include "WProgram.h" | ||
#include "SHT1x.h" | ||
|
||
SHT1x::SHT1x(int dataPin, int clockPin) | ||
{ | ||
_dataPin = dataPin; | ||
_clockPin = clockPin; | ||
} | ||
|
||
|
||
/* ================ Public methods ================ */ | ||
|
||
/** | ||
* Reads the current temperature in degrees Celsius | ||
*/ | ||
float SHT1x::readTemperatureC() | ||
{ | ||
int _val; // Raw value returned from sensor | ||
float _temperature; // Temperature derived from raw value | ||
|
||
// Conversion coefficients from SHT15 datasheet | ||
const float D1 = -40.0; // for 14 Bit @ 5V | ||
const float D2 = 0.01; // for 14 Bit DEGC | ||
|
||
// Fetch raw value | ||
_val = readTemperatureRaw(); | ||
|
||
// Convert raw value to degrees Celsius | ||
_temperature = (_val * D2) + D1; | ||
|
||
return (_temperature); | ||
} | ||
|
||
/** | ||
* Reads the current temperature in degrees Fahrenheit | ||
*/ | ||
float SHT1x::readTemperatureF() | ||
{ | ||
int _val; // Raw value returned from sensor | ||
float _temperature; // Temperature derived from raw value | ||
|
||
// Conversion coefficients from SHT15 datasheet | ||
const float D1 = -40.0; // for 14 Bit @ 5V | ||
const float D2 = 0.018; // for 14 Bit DEGC | ||
|
||
// Fetch raw value | ||
_val = readTemperatureRaw(); | ||
|
||
// Convert raw value to degrees Celsius | ||
_temperature = (_val * D2) + D1; | ||
|
||
return (_temperature); | ||
} | ||
|
||
/** | ||
* 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); | ||
} | ||
|
||
|
||
/* ================ Private methods ================ */ | ||
|
||
/** | ||
* Reads the current raw temperature value | ||
*/ | ||
float SHT1x::readTemperatureRaw() | ||
{ | ||
int _val; | ||
|
||
// Command to send to the SHT1x to request Temperature | ||
int _gTempCmd = 0b00000011; | ||
|
||
sendCommandSHT(_gTempCmd, _dataPin, _clockPin); | ||
waitForResultSHT(_dataPin); | ||
_val = getData16SHT(_dataPin, _clockPin); | ||
skipCrcSHT(_dataPin, _clockPin); | ||
|
||
return (_val); | ||
} | ||
|
||
/** | ||
*/ | ||
int SHT1x::shiftIn(int _dataPin, int _clockPin, int _numBits) | ||
{ | ||
int ret = 0; | ||
int i; | ||
|
||
for (i=0; i<_numBits; ++i) | ||
{ | ||
digitalWrite(_clockPin, HIGH); | ||
delay(10); // I don't know why I need this, but without it I don't get my 8 lsb of temp | ||
ret = ret*2 + digitalRead(_dataPin); | ||
digitalWrite(_clockPin, LOW); | ||
} | ||
|
||
return(ret); | ||
} | ||
|
||
/** | ||
*/ | ||
void SHT1x::sendCommandSHT(int _command, int _dataPin, int _clockPin) | ||
{ | ||
int ack; | ||
|
||
// Transmission Start | ||
pinMode(_dataPin, OUTPUT); | ||
pinMode(_clockPin, OUTPUT); | ||
digitalWrite(_dataPin, HIGH); | ||
digitalWrite(_clockPin, HIGH); | ||
digitalWrite(_dataPin, LOW); | ||
digitalWrite(_clockPin, LOW); | ||
digitalWrite(_clockPin, HIGH); | ||
digitalWrite(_dataPin, HIGH); | ||
digitalWrite(_clockPin, LOW); | ||
|
||
// The command (3 msb are address and must be 000, and last 5 bits are command) | ||
shiftOut(_dataPin, _clockPin, MSBFIRST, _command); | ||
|
||
// Verify we get the correct ack | ||
digitalWrite(_clockPin, HIGH); | ||
pinMode(_dataPin, INPUT); | ||
ack = digitalRead(_dataPin); | ||
if (ack != LOW) { | ||
//Serial.println("Ack Error 0"); | ||
} | ||
digitalWrite(_clockPin, LOW); | ||
ack = digitalRead(_dataPin); | ||
if (ack != HIGH) { | ||
//Serial.println("Ack Error 1"); | ||
} | ||
} | ||
|
||
/** | ||
*/ | ||
void SHT1x::waitForResultSHT(int _dataPin) | ||
{ | ||
int i; | ||
int ack; | ||
|
||
pinMode(_dataPin, INPUT); | ||
|
||
for(i= 0; i < 100; ++i) | ||
{ | ||
delay(10); | ||
ack = digitalRead(_dataPin); | ||
|
||
if (ack == LOW) { | ||
break; | ||
} | ||
} | ||
|
||
if (ack == HIGH) { | ||
//Serial.println("Ack Error 2"); // Can't do serial stuff here, need another way of reporting errors | ||
} | ||
} | ||
|
||
/** | ||
*/ | ||
int SHT1x::getData16SHT(int _dataPin, int _clockPin) | ||
{ | ||
int val; | ||
|
||
// Get the most significant bits | ||
pinMode(_dataPin, INPUT); | ||
pinMode(_clockPin, OUTPUT); | ||
val = shiftIn(_dataPin, _clockPin, 8); | ||
val *= 256; | ||
|
||
// Send the required ack | ||
pinMode(_dataPin, OUTPUT); | ||
digitalWrite(_dataPin, HIGH); | ||
digitalWrite(_dataPin, LOW); | ||
digitalWrite(_clockPin, HIGH); | ||
digitalWrite(_clockPin, LOW); | ||
|
||
// Get the least significant bits | ||
pinMode(_dataPin, INPUT); | ||
val |= shiftIn(_dataPin, _clockPin, 8); | ||
|
||
return val; | ||
} | ||
|
||
/** | ||
*/ | ||
void SHT1x::skipCrcSHT(int _dataPin, int _clockPin) | ||
{ | ||
// Skip acknowledge to end trans (no CRC) | ||
pinMode(_dataPin, OUTPUT); | ||
pinMode(_clockPin, OUTPUT); | ||
|
||
digitalWrite(_dataPin, HIGH); | ||
digitalWrite(_clockPin, HIGH); | ||
digitalWrite(_clockPin, LOW); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* SHT1x Library | ||
* | ||
* Copyright 2009 Jonathan Oxer <[email protected]> / <www.practicalarduino.com> | ||
* Based on previous work by: | ||
* Maurice Ribble: <www.glacialwanderer.com/hobbyrobotics/?p=5> | ||
* Wayne ?: <ragingreality.blogspot.com/2008/01/ardunio-and-sht15.html> | ||
* | ||
* Manages communication with SHT1x series (SHT10, SHT11, SHT15) | ||
* temperature / humidity sensors from Sensirion (www.sensirion.com). | ||
*/ | ||
#ifndef SHT1x_h | ||
#define SHT1x_h | ||
|
||
#include "WProgram.h" | ||
|
||
class SHT1x | ||
{ | ||
public: | ||
SHT1x(int dataPin, int clockPin); | ||
float readHumidity(); | ||
float readTemperatureC(); | ||
float readTemperatureF(); | ||
private: | ||
int _dataPin; | ||
int _clockPin; | ||
int _numBits; | ||
float readTemperatureRaw(); | ||
int shiftIn(int _dataPin, int _clockPin, int _numBits); | ||
void sendCommandSHT(int _command, int _dataPin, int _clockPin); | ||
void waitForResultSHT(int _dataPin); | ||
int getData16SHT(int _dataPin, int _clockPin); | ||
void skipCrcSHT(int _dataPin, int _clockPin); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* ReadSHT1xValues | ||
* | ||
* Read temperature and humidity values from an SHT1x-series (SHT10, | ||
* SHT11, SHT15) sensor. | ||
* | ||
* Copyright 2009 Jonathan Oxer <jon@oxer.com.au> | ||
* www.practicalarduino.com | ||
*/ | ||
|
||
#include <SHT1x.h> | ||
|
||
// Specify data and clock connections and instantiate SHT1x object | ||
#define dataPin 10 | ||
#define clockPin 11 | ||
SHT1x sht1x(dataPin, clockPin); | ||
|
||
void setup() | ||
{ | ||
Serial.begin(38400); // Open serial connection to report values to host | ||
Serial.println("Starting up"); | ||
} | ||
|
||
void loop() | ||
{ | ||
float temp_c; | ||
float temp_f; | ||
float humidity; | ||
|
||
// Read values from the sensor | ||
temp_c = sht1x.readTemperatureC(); | ||
temp_f = sht1x.readTemperatureF(); | ||
humidity = sht1x.readHumidity(); | ||
|
||
// Print the values to the serial port | ||
Serial.print("Temperature: "); | ||
Serial.print(temp_c, DEC); | ||
Serial.print("C / "); | ||
Serial.print(temp_f, DEC); | ||
Serial.print("F. Humidity: "); | ||
Serial.print(humidity); | ||
Serial.println("%"); | ||
|
||
delay(2000); | ||
} |