Skip to content

Commit

Permalink
Cleanup for 1.1.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Townsend committed Oct 1, 2018
1 parent 98e6872 commit 0042e4e
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 98 deletions.
182 changes: 113 additions & 69 deletions Adafruit_HTU21DF.cpp
Original file line number Diff line number Diff line change
@@ -1,100 +1,144 @@
/***************************************************
/***************************************************
This is a library for the HTU21DF Humidity & Temp Sensor
Designed specifically to work with the HTU21DF sensor from Adafruit
----> https://www.adafruit.com/products/1899
These displays use I2C to communicate, 2 pins are required to
These displays use I2C to communicate, 2 pins are required to
interface
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/

#include "Adafruit_HTU21DF.h"

#if defined(__AVR__)
#include <util/delay.h>
#endif

Adafruit_HTU21DF::Adafruit_HTU21DF() {
/**
* Constructor for the HTU21DF driver.
*/
Adafruit_HTU21DF::Adafruit_HTU21DF()
{
/* Assign default values to internal tracking variables. */
_last_humidity = 0.0f;
_last_temp = 0.0f;
}

/**
* Initialises the I2C transport, and configures the IC for normal operation.
*
* @return true (1) if the device was successfully initialised, otherwise
* false (0).
*/
boolean Adafruit_HTU21DF::begin(void)
{
Wire.begin();

reset();

Wire.beginTransmission(HTU21DF_I2CADDR);
Wire.write(HTU21DF_READREG);
Wire.endTransmission();
Wire.requestFrom(HTU21DF_I2CADDR, 1);
return (Wire.read() == 0x2); // after reset should be 0x2
}

boolean Adafruit_HTU21DF::begin(void) {
Wire.begin();

reset();

Wire.beginTransmission(HTU21DF_I2CADDR);
Wire.write(HTU21DF_READREG);
Wire.endTransmission();
Wire.requestFrom(HTU21DF_I2CADDR, 1);
return (Wire.read() == 0x2); // after reset should be 0x2
/**
* Sends a 'reset' request to the HTU21DF, followed by a 15ms delay.
*/
void Adafruit_HTU21DF::reset(void)
{
Wire.beginTransmission(HTU21DF_I2CADDR);
Wire.write(HTU21DF_RESET);
Wire.endTransmission();
delay(15);
}

void Adafruit_HTU21DF::reset(void) {
Wire.beginTransmission(HTU21DF_I2CADDR);
Wire.write(HTU21DF_RESET);
Wire.endTransmission();
delay(15);
/**
* Performs a single temperature conversion in degrees Celsius.
*
* @return a single-precision (32-bit) float value indicating the measured
* temperature in degrees Celsius.
*/
float Adafruit_HTU21DF::readTemperature(void)
{
// OK lets ready!
Wire.beginTransmission(HTU21DF_I2CADDR);
Wire.write(HTU21DF_READTEMP);
Wire.endTransmission();

delay(50); // add delay between request and actual read!

uint8_t count = Wire.requestFrom(HTU21DF_I2CADDR, 3);

/* Make sure we got 3 bytes back. */
if (count != 3) {
return 0.0f;
}

/* Read 16 bits of data, dropping the last two status bits. */
uint16_t t = Wire.read();
t <<= 8;
t |= Wire.read() & 0b11111100;

uint8_t crc = Wire.read();
(void)crc;

float temp = t;
temp *= 175.72f;
temp /= 65536.0f;
temp -= 46.85f;

/* Track the value internally in case we need to access it later. */
_last_temp = temp;

return temp;
}

/**
* Performs a single relative humidity conversion.
*
* @return A single-precision (32-bit) float value indicating the relative
* humidity in percent (0..100.0%).
*/
float Adafruit_HTU21DF::readHumidity(void) {
/* Prepare the I2C request. */
Wire.beginTransmission(HTU21DF_I2CADDR);
Wire.write(HTU21DF_READHUM);
Wire.endTransmission();

float Adafruit_HTU21DF::readTemperature(void) {

// OK lets ready!
Wire.beginTransmission(HTU21DF_I2CADDR);
Wire.write(HTU21DF_READTEMP);
Wire.endTransmission();

delay(50); // add delay between request and actual read!

Wire.requestFrom(HTU21DF_I2CADDR, 3);
while (!Wire.available()) {}
/* Wait a bit for the conversion to complete. */
delay(50);

uint16_t t = Wire.read();
t <<= 8;
t |= Wire.read();
/* Read the conversion results. */
uint8_t count = Wire.requestFrom(HTU21DF_I2CADDR, 3);

uint8_t crc = Wire.read();
/* Make sure we got 3 bytes back. */
if (count != 3) {
return 0.0f;
}

float temp = t;
temp *= 175.72;
temp /= 65536;
temp -= 46.85;
/* Read 16 bits of data, dropping the last two status bits. */
uint16_t h = Wire.read();
h <<= 8;
h |= Wire.read() & 0b11111100;

return temp;
}

uint8_t crc = Wire.read();
(void)crc;

float Adafruit_HTU21DF::readHumidity(void) {
// OK lets ready!
Wire.beginTransmission(HTU21DF_I2CADDR);
Wire.write(HTU21DF_READHUM);
Wire.endTransmission();

delay(50); // add delay between request and actual read!

Wire.requestFrom(HTU21DF_I2CADDR, 3);
while (!Wire.available()) {}

uint16_t h = Wire.read();
h <<= 8;
h |= Wire.read();

uint8_t crc = Wire.read();

float hum = h;
hum *= 125;
hum /= 65536;
hum -= 6;

return hum;
}
float hum = h;
hum *= 125.0f;
hum /= 65536.0f;
hum -= 6.0f;

/* Track the value internally in case we need to access it later. */
_last_humidity = hum;


/*********************************************************************/
return hum;
}
56 changes: 35 additions & 21 deletions Adafruit_HTU21DF.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/***************************************************
/***************************************************
This is a library for the HTU21D-F Humidity & Temp Sensor
Designed specifically to work with the HTU21D-F sensor from Adafruit
----> https://www.adafruit.com/products/1899
These displays use I2C to communicate, 2 pins are required to
These displays use I2C to communicate, 2 pins are required to
interface
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/

Expand All @@ -24,25 +24,39 @@
#endif
#include "Wire.h"

#define HTU21DF_I2CADDR 0x40
#define HTU21DF_READTEMP 0xE3
#define HTU21DF_READHUM 0xE5
#define HTU21DF_WRITEREG 0xE6
#define HTU21DF_READREG 0xE7
#define HTU21DF_RESET 0xFE
/** Default I2C address for the HTU21D. */
#define HTU21DF_I2CADDR (0x40)

/** Read temperature register. */
#define HTU21DF_READTEMP (0xE3)

/** Read humidity register. */
#define HTU21DF_READHUM (0xE5)

/** Write register command. */
#define HTU21DF_WRITEREG (0xE6)

/** Read register command. */
#define HTU21DF_READREG (0xE7)

/** Reset command. */
#define HTU21DF_RESET (0xFE)

/**
* Driver for the Adafruit HTU21DF breakout board.
*/
class Adafruit_HTU21DF {
public:
Adafruit_HTU21DF();
boolean begin(void);
float readTemperature(void);
float readHumidity(void);
void reset(void);
private:
boolean readData(void);
float humidity, temp;
public:
Adafruit_HTU21DF();

boolean begin(void);
float readTemperature(void);
float readHumidity(void);
void reset(void);

private:
boolean readData(void);
float _last_humidity, _last_temp;
};

#endif
#endif /* _ADAFRUIT_HTU21DF_H */
16 changes: 9 additions & 7 deletions examples/HTU21DFtest/HTU21DFtest.ino
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/***************************************************
/***************************************************
This is an example for the HTU21D-F Humidity & Temp Sensor
Designed specifically to work with the HTU21D-F sensor from Adafruit
----> https://www.adafruit.com/products/1899
These displays use I2C to communicate, 2 pins are required to
These displays use I2C to communicate, 2 pins are required to
interface
****************************************************/

Expand All @@ -28,9 +28,11 @@ void setup() {
}
}


void loop() {
Serial.print("Temp: "); Serial.print(htu.readTemperature());
Serial.print("\t\tHum: "); Serial.println(htu.readHumidity());
delay(500);
}
float temp = htu.readTemperature();
float rel_hum = htu.readHumidity();
Serial.print("Temp: "); Serial.print(temp); Serial.print(" C");
Serial.print("\t\t");
Serial.print("Humidity: "); Serial.print(rel_hum); Serial.println(" \%");
delay(500);
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Adafruit HTU21DF Library
version=1.0.1
version=1.1.0
author=Adafruit
maintainer=Adafruit <info@adafruit.com>
sentence=Arduino library for the HTU21D-F sensors in the Adafruit shop
Expand Down

0 comments on commit 0042e4e

Please sign in to comment.