Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes readings error #6

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
2013-06-11
* -decoupled requests, read in, and parsing
* -adjusted temperature for voltage
* -stores temperature for humidity conversion rather than re-requesting it

2011-09-20
* Conditionally include Arduino.h for compatibility with Arduino 1.0

Expand Down
3 changes: 3 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
SHT1x Temperature / Humidity Sensor Library for Arduino
=======================================================
Additional work done by Louis Thiery ([email protected]) - hoping to see GNU or CC copyright emerge

Copyright 2009 Jonathan Oxer [email protected] / http://www.practicalarduino.com

Copyright 2008 Maurice Ribble [email protected] / http://www.glacialwanderer.com

Provides a simple interface to the SHT1x series (SHT10, SHT11, SHT15)
Expand Down
64 changes: 64 additions & 0 deletions README.markdown~
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
SHT1x Temperature / Humidity Sensor Library for Arduino
=======================================================
Additional work done by Louis Thiery ([email protected]) - hoping to see GNU or CC copyright emerge
Copyright 2009 Jonathan Oxer [email protected] / http://www.practicalarduino.com
Copyright 2008 Maurice Ribble [email protected] / http://www.glacialwanderer.com

Provides a simple interface to the SHT1x series (SHT10, SHT11, SHT15)
and SHT7x series (SHT71, SHT75) temperature / humidity sensors from
Sensirion, http://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.

Installation
------------
Download the directory "SHT1x" and move it into the "libraries"
directory inside your sketchbook directory, then restart the Arduino
IDE. You will then see it listed under File->Examples->SHT1x.

Usage
-----
The library is instantiated as an object with methods provided to read
relative humidity and temperature. Include it in your sketch and then
create an object, specifying the pins to use for communication with the
sensor:

#include <SHT1x.h>
#define dataPin 10
#define clockPin 11
SHT1x sht1x(dataPin, clockPin);

You can then call methods on that object within your program. In this
example we created an object called "sht1x", but it could have been
called whatever you like. A complete example program is included with
the library and can be accessed from the File->Examples->SHT1x menu.

### readTemperatureC() ###

Returns a float within the valid range of the sensor of -40 to +123.8C.
A value of -40 is returned in the event of a communication error with
the sensor.

Example:

float tempC = sht1x.readTemperatureC();

### readTemperatureF() ###

Returns a float within the valid range of the sensor of -40 to +254.9F.
A value of -40 is returned in the event of a communication error with
the sensor.

Example:

float tempF = sht1x.readTemperatureF();

### readHumidity() ###

Returns a float within the valid range of the sensor of 0 to 100%.
A negative value is returned in the event of a communication error with
the sensor.

Example:

float humidity = sht1x.readHumidity();
195 changes: 107 additions & 88 deletions SHT1x.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/**
* SHT1x Library
*
* Recent elaborations by Louis Thiery ([email protected]) <hoping to see a GNU or CC license>
*
* Copyright 2009 Jonathan Oxer <[email protected]> / <www.practicalarduino.com>
* Based on previous work by:
* Maurice Ribble: <www.glacialwanderer.com/hobbyrobotics/?p=5>
Expand All @@ -17,131 +19,165 @@

#include "SHT1x.h"

#define DEFAULT_VOLTAGE 5

SHT1x::SHT1x(int dataPin, int clockPin)
{
_dataPin = dataPin;
_clockPin = clockPin;
_setConversionCoeffs(DEFAULT_VOLTAGE);
}

SHT1x::SHT1x(int dataPin, int clockPin, float voltage)
{
_dataPin = dataPin;
_clockPin = clockPin;
_setConversionCoeffs(voltage);
}

#define VALS_IN_TABLE 5

// dynamically determines conversion coefficients based on voltage
// tables generated by datasheet
void SHT1x::_setConversionCoeffs(float voltage){
_D2C = 0.01; // for 14 Bit DEGC
_D2F = 0.018; // for 14 Bit DEGC

const float coeffsC[] = {40.1,39.8,39.7,39.6,39.4};
const float coeffsF[] = {40.2,39.6,39.5,39.3,38.9};
const float vals[]={5,4,3.5,3,2.5};

for (int i=1;i<VALS_IN_TABLE;i++){
if(voltage>vals[i]){
_D1C=-_linearInterpolation(coeffsC[i-1],coeffsC[i],vals[i-1],voltage);
_D1F=-_linearInterpolation(coeffsF[i-1],coeffsF[i],vals[i-1],voltage);
break;
}
}
}

float SHT1x::_linearInterpolation(float coeffA, float coeffB, float valA, float input){
Serial.println((coeffA-coeffB)/valA*input+coeffB);
return (coeffA-coeffB)/valA*input+coeffB;
}


/* ================ Public methods ================ */

/**
* Reads the current temperature in degrees Celsius
* Requests, reads, and parses temperatures in C
*/
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;
//just putting together functions
requestTemperature();
return parseTemperatureC(readInTemperature());
}

return (_temperature);
float SHT1x::parseTemperatureC(int raw){
return (raw * _D2C) + _D1C;
}


/**
* Reads the current temperature in degrees Fahrenheit
* Requests, reads, and parses temperatures in F
*/
float SHT1x::readTemperatureF()
{
int _val; // Raw value returned from sensor
float _temperature; // Temperature derived from raw value
requestTemperature();
return parseTemperatureF(readInTemperature());
}

// Conversion coefficients from SHT15 datasheet
const float D1 = -40.0; // for 14 Bit @ 5V
const float D2 = 0.018; // for 14 Bit DEGF
float SHT1x::parseTemperatureF(int raw){
// Convert raw value to degrees Fahrenheit
return (raw * _D2F) + _D1F;
}

// Fetch raw value
_val = readTemperatureRaw();
/**
* Temperature Request
*/
void SHT1x::requestTemperature()
{
int _val;
// Command to send to the SHT1x to request Temperature
int _gTempCmd = 0b00000011;
sendCommandSHT(_gTempCmd, _dataPin, _clockPin);
}

// Convert raw value to degrees Fahrenheit
_temperature = (_val * D2) + D1;
/**
* Read-in Temperature
*/
int SHT1x::readInTemperature()
{
waitForResultSHT(_dataPin);

return (_temperature);
_temperatureRaw= getData16SHT(_dataPin, _clockPin); //store in variable for humidity
skipCrcSHT(_dataPin, _clockPin);
return _temperatureRaw;
}

/**
* 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
requestHumidity();
return parseHumidity(readInHumidity());
}

// 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
void SHT1x::requestHumidity(){

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

// Fetch the value from the sensor
sendCommandSHT(_gHumidCmd, _dataPin, _clockPin);
}

float SHT1x::readInHumidity(){
int val; // Raw humidity value returned from sensor
waitForResultSHT(_dataPin);
_val = getData16SHT(_dataPin, _clockPin);
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 - 25.0 ) * (T1 + T2 * _val) + _linearHumidity;

return (_correctedHumidity);
return val;
}

float SHT1x::parseHumidity(int raw){
float linearHumidity; // Humidity with linear correction applied
float correctedHumidity; // Temperature-corrected humidity
float temperature; // Raw temperature value

/* ================ Private methods ================ */
// 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
const float T2 = 0.00008; // for 14 Bit

/**
* Reads the current raw temperature value
*/
float SHT1x::readTemperatureRaw()
{
int _val;
// Apply linear conversion to raw value
linearHumidity = C1 + C2 * raw + C3 * raw * raw;

// Command to send to the SHT1x to request Temperature
int _gTempCmd = 0b00000011;
//temperature request should be accomplished right before this!
temperature = parseTemperatureC(_temperatureRaw);

sendCommandSHT(_gTempCmd, _dataPin, _clockPin);
waitForResultSHT(_dataPin);
_val = getData16SHT(_dataPin, _clockPin);
skipCrcSHT(_dataPin, _clockPin);
// Correct humidity value for current temperature
correctedHumidity = (temperature - 25.0 ) * (T1 + T2 * raw) + linearHumidity;

return (_val);
return correctedHumidity;
}

/**
*/
int SHT1x::shiftIn(int _dataPin, int _clockPin, int _numBits)
uint8_t 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);
}

Expand Down Expand Up @@ -169,38 +205,21 @@ void SHT1x::sendCommandSHT(int _command, int _dataPin, int _clockPin)
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
}

unsigned long int start = millis();

//wait for SHT to show that its ready or move along it timeout is reached
while( digitalRead(_dataPin) && (millis()-start)<TIMEOUT_MILLIS );
}

/**
Expand Down
Loading