├── .github ├── ISSUE_TEMPLATE.md ├── PULL_REQUEST_TEMPLATE.md └── workflows │ └── githubci.yml ├── Adafruit_HTU21DF.cpp ├── Adafruit_HTU21DF.h ├── README.md ├── examples └── HTU21DFtest │ └── HTU21DFtest.ino └── library.properties /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thank you for opening an issue on an Adafruit Arduino library repository. To 2 | improve the speed of resolution please review the following guidelines and 3 | common troubleshooting steps below before creating the issue: 4 | 5 | - **Do not use GitHub issues for troubleshooting projects and issues.** Instead use 6 | the forums at http://forums.adafruit.com to ask questions and troubleshoot why 7 | something isn't working as expected. In many cases the problem is a common issue 8 | that you will more quickly receive help from the forum community. GitHub issues 9 | are meant for known defects in the code. If you don't know if there is a defect 10 | in the code then start with troubleshooting on the forum first. 11 | 12 | - **If following a tutorial or guide be sure you didn't miss a step.** Carefully 13 | check all of the steps and commands to run have been followed. Consult the 14 | forum if you're unsure or have questions about steps in a guide/tutorial. 15 | 16 | - **For Arduino projects check these very common issues to ensure they don't apply**: 17 | 18 | - For uploading sketches or communicating with the board make sure you're using 19 | a **USB data cable** and **not** a **USB charge-only cable**. It is sometimes 20 | very hard to tell the difference between a data and charge cable! Try using the 21 | cable with other devices or swapping to another cable to confirm it is not 22 | the problem. 23 | 24 | - **Be sure you are supplying adequate power to the board.** Check the specs of 25 | your board and plug in an external power supply. In many cases just 26 | plugging a board into your computer is not enough to power it and other 27 | peripherals. 28 | 29 | - **Double check all soldering joints and connections.** Flakey connections 30 | cause many mysterious problems. See the [guide to excellent soldering](https://learn.adafruit.com/adafruit-guide-excellent-soldering/tools) for examples of good solder joints. 31 | 32 | - **Ensure you are using an official Arduino or Adafruit board.** We can't 33 | guarantee a clone board will have the same functionality and work as expected 34 | with this code and don't support them. 35 | 36 | If you're sure this issue is a defect in the code and checked the steps above 37 | please fill in the following fields to provide enough troubleshooting information. 38 | You may delete the guideline and text above to just leave the following details: 39 | 40 | - Arduino board: **INSERT ARDUINO BOARD NAME/TYPE HERE** 41 | 42 | - Arduino IDE version (found in Arduino -> About Arduino menu): **INSERT ARDUINO 43 | VERSION HERE** 44 | 45 | - List the steps to reproduce the problem below (if possible attach a sketch or 46 | copy the sketch code in too): **LIST REPRO STEPS BELOW** 47 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thank you for creating a pull request to contribute to Adafruit's GitHub code! 2 | Before you open the request please review the following guidelines and tips to 3 | help it be more easily integrated: 4 | 5 | - **Describe the scope of your change--i.e. what the change does and what parts 6 | of the code were modified.** This will help us understand any risks of integrating 7 | the code. 8 | 9 | - **Describe any known limitations with your change.** For example if the change 10 | doesn't apply to a supported platform of the library please mention it. 11 | 12 | - **Please run any tests or examples that can exercise your modified code.** We 13 | strive to not break users of the code and running tests/examples helps with this 14 | process. 15 | 16 | Thank you again for contributing! We will try to test and integrate the change 17 | as soon as we can, but be aware we have many GitHub repositories to manage and 18 | can't immediately respond to every request. There is no need to bump or check in 19 | on a pull request (it will clutter the discussion of the request). 20 | 21 | Also don't be worried if the request is closed or not integrated--sometimes the 22 | priorities of Adafruit's GitHub code (education, ease of use) might not match the 23 | priorities of the pull request. Don't fret, the open source community thrives on 24 | forks and GitHub makes it easy to keep your changes in a forked repo. 25 | 26 | After reviewing the guidelines above you can delete this text from the pull request. 27 | -------------------------------------------------------------------------------- /.github/workflows/githubci.yml: -------------------------------------------------------------------------------- 1 | name: Arduino Library CI 2 | 3 | on: [pull_request, push, repository_dispatch] 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | 9 | steps: 10 | - uses: actions/setup-python@v4 11 | with: 12 | python-version: '3.x' 13 | - uses: actions/checkout@v3 14 | - uses: actions/checkout@v3 15 | with: 16 | repository: adafruit/ci-arduino 17 | path: ci 18 | 19 | - name: pre-install 20 | run: bash ci/actions_install.sh 21 | 22 | - name: test platforms 23 | run: python3 ci/build_platform.py main_platforms 24 | 25 | - name: clang 26 | run: python3 ci/run-clang-format.py -e "ci/*" -e "bin/*" -r . 27 | 28 | - name: doxygen 29 | env: 30 | GH_REPO_TOKEN: ${{ secrets.GH_REPO_TOKEN }} 31 | PRETTYNAME : "Adafruit HTU21D-F Arduino Library" 32 | run: bash ci/doxy_gen_and_deploy.sh 33 | -------------------------------------------------------------------------------- /Adafruit_HTU21DF.cpp: -------------------------------------------------------------------------------- 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 | 28 | #include "Adafruit_HTU21DF.h" 29 | 30 | /** 31 | * Constructor for the HTU21DF driver. 32 | */ 33 | Adafruit_HTU21DF::Adafruit_HTU21DF() { 34 | /* Assign default values to internal tracking variables. */ 35 | _last_humidity = 0.0f; 36 | _last_temp = 0.0f; 37 | } 38 | 39 | /** 40 | * Initialises the I2C transport, and configures the IC for normal operation. 41 | * @param theWire Pointer to TwoWire I2C object, uses &Wire by default 42 | * @return true (1) if the device was successfully initialised, otherwise 43 | * false (0). 44 | */ 45 | bool Adafruit_HTU21DF::begin(TwoWire *theWire) { 46 | if (i2c_dev) { 47 | delete i2c_dev; 48 | } 49 | i2c_dev = new Adafruit_I2CDevice(HTU21DF_I2CADDR, theWire); 50 | 51 | if (!i2c_dev->begin()) { 52 | return false; 53 | } 54 | 55 | reset(); 56 | 57 | Adafruit_BusIO_Register reg = 58 | Adafruit_BusIO_Register(i2c_dev, HTU21DF_READREG); 59 | 60 | return (reg.read() == 0x2); // after reset should be 0x2 61 | } 62 | 63 | /** 64 | * Sends a 'reset' request to the HTU21DF, followed by a 15ms delay. 65 | */ 66 | void Adafruit_HTU21DF::reset(void) { 67 | uint8_t cmd = HTU21DF_RESET; 68 | i2c_dev->write(&cmd, 1); 69 | 70 | delay(15); 71 | } 72 | 73 | /** 74 | * Performs a single temperature conversion in degrees Celsius. 75 | * 76 | * @return a single-precision (32-bit) float value indicating the measured 77 | * temperature in degrees Celsius or NAN on failure. 78 | */ 79 | float Adafruit_HTU21DF::readTemperature(void) { 80 | // OK lets ready! 81 | uint8_t cmd = HTU21DF_READTEMP; 82 | if (!i2c_dev->write(&cmd, 1)) { 83 | return NAN; 84 | } 85 | 86 | delay(50); // add delay between request and actual read! 87 | 88 | uint8_t buf[3]; 89 | if (!i2c_dev->read(buf, 3)) { 90 | return NAN; 91 | } 92 | 93 | /* Read 16 bits of data, dropping the last two status bits. */ 94 | uint16_t t = buf[0]; 95 | t <<= 8; 96 | t |= buf[1] & 0b11111100; 97 | 98 | // 3rd byte is the CRC 99 | 100 | float temp = t; 101 | temp *= 175.72f; 102 | temp /= 65536.0f; 103 | temp -= 46.85f; 104 | 105 | /* Track the value internally in case we need to access it later. */ 106 | _last_temp = temp; 107 | 108 | return temp; 109 | } 110 | 111 | /** 112 | * Performs a single relative humidity conversion. 113 | * 114 | * @return A single-precision (32-bit) float value indicating the relative 115 | * humidity in percent (0..100.0%). 116 | */ 117 | float Adafruit_HTU21DF::readHumidity(void) { 118 | /* Prepare the I2C request. */ 119 | uint8_t cmd = HTU21DF_READHUM; 120 | if (!i2c_dev->write(&cmd, 1)) { 121 | return NAN; 122 | } 123 | 124 | /* Wait a bit for the conversion to complete. */ 125 | delay(50); 126 | 127 | uint8_t buf[3]; 128 | if (!i2c_dev->read(buf, 3)) { 129 | return NAN; 130 | } 131 | 132 | /* Read 16 bits of data, dropping the last two status bits. */ 133 | uint16_t h = buf[0]; 134 | h <<= 8; 135 | h |= buf[1] & 0b11111100; 136 | 137 | // 3rd byte is the CRC 138 | 139 | float hum = h; 140 | hum *= 125.0f; 141 | hum /= 65536.0f; 142 | hum -= 6.0f; 143 | 144 | /* Track the value internally in case we need to access it later. */ 145 | _last_humidity = hum; 146 | 147 | return hum; 148 | } 149 | -------------------------------------------------------------------------------- /Adafruit_HTU21DF.h: -------------------------------------------------------------------------------- 1 | /*! 2 | * @file Adafruit_HTU21DF.h 3 | */ 4 | 5 | #ifndef _ADAFRUIT_HTU21DF_H 6 | #define _ADAFRUIT_HTU21DF_H 7 | 8 | #include 9 | #include 10 | 11 | /** Default I2C address for the HTU21D. */ 12 | #define HTU21DF_I2CADDR (0x40) 13 | 14 | /** Read temperature register. */ 15 | #define HTU21DF_READTEMP (0xE3) 16 | 17 | /** Read humidity register. */ 18 | #define HTU21DF_READHUM (0xE5) 19 | 20 | /** Write register command. */ 21 | #define HTU21DF_WRITEREG (0xE6) 22 | 23 | /** Read register command. */ 24 | #define HTU21DF_READREG (0xE7) 25 | 26 | /** Reset command. */ 27 | #define HTU21DF_RESET (0xFE) 28 | 29 | /** 30 | * Driver for the Adafruit HTU21DF breakout board. 31 | */ 32 | class Adafruit_HTU21DF { 33 | public: 34 | Adafruit_HTU21DF(); 35 | 36 | bool begin(TwoWire *theWire = &Wire); 37 | float readTemperature(void); 38 | float readHumidity(void); 39 | void reset(void); 40 | 41 | private: 42 | Adafruit_I2CDevice *i2c_dev = NULL; ///< Pointer to I2C bus interface 43 | float _last_humidity, _last_temp; 44 | }; 45 | 46 | #endif /* _ADAFRUIT_HTU21DF_H */ 47 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Adafruit HTU21D-F Humidity/Temp Sensor for Arduino [![Build Status](https://github.com/adafruit/Adafruit_HTU21DF_Library/workflows/Arduino%20Library%20CI/badge.svg)](https://github.com/adafruit/Adafruit_HTU21DF_Library/actions)[![Documentation](https://github.com/adafruit/ci-arduino/blob/master/assets/doxygen_badge.svg)](http://adafruit.github.io/Adafruit_HTU21DF_Library/html/index.html) 2 | 3 | ![sensors_1899-00](https://user-images.githubusercontent.com/181073/46350691-b53d1880-c655-11e8-8415-452aec129b44.jpg) 4 | 5 | This is a library for the HTU21D-F Humidity + Temperature sensor. 6 | 7 | Designed specifically to work with the HTU21D-F in the Adafruit shop: 8 | 9 | - https://www.adafruit.com/products/1899 10 | 11 | These boards use **I2C** to communicate. 2 pins are required to interface. 12 | Adafruit invests time and resources providing this open source code, 13 | please support Adafruit and open-source hardware by purchasing 14 | products from Adafruit! 15 | 16 | Written by Limor Fried/Ladyada for Adafruit Industries. 17 | BSD license, all text above must be included in any redistribution. 18 | 19 | ## Installing this Library 20 | 21 | Check out the links above for our tutorials and wiring diagrams. Use the Arduino library manager to install 22 | 23 | We also have a great tutorial on Arduino library installation at: 24 | http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use 25 | 26 | ## Learning Guide and API Documentation 27 | 28 | ### Learning Guide 29 | 30 | For a practical guide on how to use this breakout, see our comprehensive [Adafruit HTU21D-F Learning Guide](https://learn.adafruit.com/adafruit-htu21d-f-temperature-humidity-sensor/overview). 31 | 32 | ### API Documentation 33 | 34 | > **NOTE:** This documentation is automatically generated based on the doxygen comments in the code. 35 | 36 | For specific programming issues, see the [API Documentation](http://adafruit.github.io/Adafruit_HTU21DF_Library/html/class_adafruit___h_t_u21_d_f.html) describing the `Adafruit_HTU21DF` class available in this library. 37 | -------------------------------------------------------------------------------- /examples/HTU21DFtest/HTU21DFtest.ino: -------------------------------------------------------------------------------- 1 | /*************************************************** 2 | This is an example for the HTU21D-F Humidity & Temp Sensor 3 | 4 | Designed specifically to work with the HTU21D-F sensor from Adafruit 5 | ----> https://www.adafruit.com/products/1899 6 | 7 | These displays use I2C to communicate, 2 pins are required to 8 | interface 9 | ****************************************************/ 10 | 11 | #include 12 | #include "Adafruit_HTU21DF.h" 13 | 14 | // Connect Vin to 3-5VDC 15 | // Connect GND to ground 16 | // Connect SCL to I2C clock pin (A5 on UNO) 17 | // Connect SDA to I2C data pin (A4 on UNO) 18 | 19 | Adafruit_HTU21DF htu = Adafruit_HTU21DF(); 20 | 21 | void setup() { 22 | Serial.begin(9600); 23 | Serial.println("HTU21D-F test"); 24 | 25 | if (!htu.begin()) { 26 | Serial.println("Couldn't find sensor!"); 27 | while (1); 28 | } 29 | } 30 | 31 | void loop() { 32 | float temp = htu.readTemperature(); 33 | float rel_hum = htu.readHumidity(); 34 | Serial.print("Temp: "); Serial.print(temp); Serial.print(" C"); 35 | Serial.print("\t\t"); 36 | Serial.print("Humidity: "); Serial.print(rel_hum); Serial.println(" \%"); 37 | delay(500); 38 | } 39 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=Adafruit HTU21DF Library 2 | version=1.1.2 3 | author=Adafruit 4 | maintainer=Adafruit 5 | sentence=Arduino library for the HTU21D-F sensors in the Adafruit shop 6 | paragraph=Arduino library for the HTU21D-F sensors in the Adafruit shop 7 | category=Sensors 8 | url=https://github.com/adafruit/Adafruit_HTU21DF_Library 9 | architectures=* 10 | depends=Adafruit Unified Sensor, Adafruit BusIO 11 | --------------------------------------------------------------------------------