Adafruit_HTU21DF.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /***************************************************
  2. This is a library for the HTU21DF Humidity & Temp Sensor
  3. Designed specifically to work with the HTU21DF sensor from Adafruit
  4. ----> https://www.adafruit.com/products/1899
  5. These displays use I2C to communicate, 2 pins are required to
  6. interface
  7. Adafruit invests time and resources providing this open source code,
  8. please support Adafruit and open-source hardware by purchasing
  9. products from Adafruit!
  10. Written by Limor Fried/Ladyada for Adafruit Industries.
  11. BSD license, all text above must be included in any redistribution
  12. ****************************************************/
  13. #include "Adafruit_HTU21DF.h"
  14. #if defined(__AVR__)
  15. #include <util/delay.h>
  16. #endif
  17. /**
  18. * Constructor for the HTU21DF driver.
  19. */
  20. Adafruit_HTU21DF::Adafruit_HTU21DF() {
  21. /* Assign default values to internal tracking variables. */
  22. _last_humidity = 0.0f;
  23. _last_temp = 0.0f;
  24. }
  25. /**
  26. * Initialises the I2C transport, and configures the IC for normal operation.
  27. *
  28. * @return true (1) if the device was successfully initialised, otherwise
  29. * false (0).
  30. */
  31. boolean Adafruit_HTU21DF::begin(void) {
  32. Wire.begin();
  33. reset();
  34. Wire.beginTransmission(HTU21DF_I2CADDR);
  35. Wire.write(HTU21DF_READREG);
  36. Wire.endTransmission();
  37. Wire.requestFrom(HTU21DF_I2CADDR, 1);
  38. return (Wire.read() == 0x2); // after reset should be 0x2
  39. }
  40. /**
  41. * Sends a 'reset' request to the HTU21DF, followed by a 15ms delay.
  42. */
  43. void Adafruit_HTU21DF::reset(void) {
  44. Wire.beginTransmission(HTU21DF_I2CADDR);
  45. Wire.write(HTU21DF_RESET);
  46. Wire.endTransmission();
  47. delay(15);
  48. }
  49. /**
  50. * Performs a single temperature conversion in degrees Celsius.
  51. *
  52. * @return a single-precision (32-bit) float value indicating the measured
  53. * temperature in degrees Celsius.
  54. */
  55. float Adafruit_HTU21DF::readTemperature(void) {
  56. // OK lets ready!
  57. Wire.beginTransmission(HTU21DF_I2CADDR);
  58. Wire.write(HTU21DF_READTEMP);
  59. Wire.endTransmission();
  60. delay(50); // add delay between request and actual read!
  61. uint8_t count = Wire.requestFrom(HTU21DF_I2CADDR, 3);
  62. /* Make sure we got 3 bytes back. */
  63. if (count != 3) {
  64. return 0.0f;
  65. }
  66. /* Read 16 bits of data, dropping the last two status bits. */
  67. uint16_t t = Wire.read();
  68. t <<= 8;
  69. t |= Wire.read() & 0b11111100;
  70. uint8_t crc = Wire.read();
  71. (void)crc;
  72. float temp = t;
  73. temp *= 175.72f;
  74. temp /= 65536.0f;
  75. temp -= 46.85f;
  76. /* Track the value internally in case we need to access it later. */
  77. _last_temp = temp;
  78. return temp;
  79. }
  80. /**
  81. * Performs a single relative humidity conversion.
  82. *
  83. * @return A single-precision (32-bit) float value indicating the relative
  84. * humidity in percent (0..100.0%).
  85. */
  86. float Adafruit_HTU21DF::readHumidity(void) {
  87. /* Prepare the I2C request. */
  88. Wire.beginTransmission(HTU21DF_I2CADDR);
  89. Wire.write(HTU21DF_READHUM);
  90. Wire.endTransmission();
  91. /* Wait a bit for the conversion to complete. */
  92. delay(50);
  93. /* Read the conversion results. */
  94. uint8_t count = Wire.requestFrom(HTU21DF_I2CADDR, 3);
  95. /* Make sure we got 3 bytes back. */
  96. if (count != 3) {
  97. return 0.0f;
  98. }
  99. /* Read 16 bits of data, dropping the last two status bits. */
  100. uint16_t h = Wire.read();
  101. h <<= 8;
  102. h |= Wire.read() & 0b11111100;
  103. uint8_t crc = Wire.read();
  104. (void)crc;
  105. float hum = h;
  106. hum *= 125.0f;
  107. hum /= 65536.0f;
  108. hum -= 6.0f;
  109. /* Track the value internally in case we need to access it later. */
  110. _last_humidity = hum;
  111. return hum;
  112. }