Adafruit_HTU21DF.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. /**
  29. * Constructor for the HTU21DF driver.
  30. */
  31. Adafruit_HTU21DF::Adafruit_HTU21DF() {
  32. /* Assign default values to internal tracking variables. */
  33. _last_humidity = 0.0f;
  34. _last_temp = 0.0f;
  35. }
  36. /**
  37. * Initialises the I2C transport, and configures the IC for normal operation.
  38. * @param theWire Pointer to TwoWire I2C object, uses &Wire by default
  39. * @return true (1) if the device was successfully initialised, otherwise
  40. * false (0).
  41. */
  42. bool Adafruit_HTU21DF::begin(TwoWire *theWire) {
  43. if (i2c_dev) {
  44. delete i2c_dev;
  45. }
  46. i2c_dev = new Adafruit_I2CDevice(HTU21DF_I2CADDR, theWire);
  47. if (!i2c_dev->begin()) {
  48. return false;
  49. }
  50. reset();
  51. Adafruit_BusIO_Register reg =
  52. Adafruit_BusIO_Register(i2c_dev, HTU21DF_READREG);
  53. return (reg.read() == 0x2); // after reset should be 0x2
  54. }
  55. /**
  56. * Sends a 'reset' request to the HTU21DF, followed by a 15ms delay.
  57. */
  58. void Adafruit_HTU21DF::reset(void) {
  59. uint8_t cmd = HTU21DF_RESET;
  60. i2c_dev->write(&cmd, 1);
  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 or NAN on failure.
  68. */
  69. float Adafruit_HTU21DF::readTemperature(void) {
  70. // OK lets ready!
  71. uint8_t cmd = HTU21DF_READTEMP;
  72. if (!i2c_dev->write(&cmd, 1)) {
  73. return NAN;
  74. }
  75. delay(50); // add delay between request and actual read!
  76. uint8_t buf[3];
  77. if (!i2c_dev->read(buf, 3)) {
  78. return NAN;
  79. }
  80. /* Read 16 bits of data, dropping the last two status bits. */
  81. uint16_t t = buf[0];
  82. t <<= 8;
  83. t |= buf[1] & 0b11111100;
  84. // 3rd byte is the CRC
  85. float temp = t;
  86. temp *= 175.72f;
  87. temp /= 65536.0f;
  88. temp -= 46.85f;
  89. /* Track the value internally in case we need to access it later. */
  90. _last_temp = temp;
  91. return temp;
  92. }
  93. /**
  94. * Performs a single relative humidity conversion.
  95. *
  96. * @return A single-precision (32-bit) float value indicating the relative
  97. * humidity in percent (0..100.0%).
  98. */
  99. float Adafruit_HTU21DF::readHumidity(void) {
  100. /* Prepare the I2C request. */
  101. uint8_t cmd = HTU21DF_READHUM;
  102. if (!i2c_dev->write(&cmd, 1)) {
  103. return NAN;
  104. }
  105. /* Wait a bit for the conversion to complete. */
  106. delay(50);
  107. uint8_t buf[3];
  108. if (!i2c_dev->read(buf, 3)) {
  109. return NAN;
  110. }
  111. /* Read 16 bits of data, dropping the last two status bits. */
  112. uint16_t h = buf[0];
  113. h <<= 8;
  114. h |= buf[1] & 0b11111100;
  115. // 3rd byte is the CRC
  116. float hum = h;
  117. hum *= 125.0f;
  118. hum /= 65536.0f;
  119. hum -= 6.0f;
  120. /* Track the value internally in case we need to access it later. */
  121. _last_humidity = hum;
  122. return hum;
  123. }