Adafruit_HTU21DF.cpp 3.5 KB

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