├── HTU21DF_test.py ├── README.md ├── LICENSE.md └── HTU21DF.py /HTU21DF_test.py: -------------------------------------------------------------------------------- 1 | # A simple program to test the driver 2 | 3 | import time 4 | import HTU21DF 5 | 6 | while True: 7 | print("sending reset...") 8 | HTU21DF.htu_reset 9 | temperature = HTU21DF.read_temperature() 10 | print("The temperature is %f C." % temperature) 11 | time.sleep(1) 12 | humidity = HTU21DF.read_humidity() 13 | print("The humidity is %F percent." % humidity) 14 | time.sleep(1) 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | RaspberryPI_HTU21DF 2 | =================== 3 | This is a python driver to use with Adafruit's HTU21D-F breakout board. 4 | https://www.adafruit.com/product/1899 5 | 6 | This driver requires the pigpio library, available at http://abyz.co.uk/rpi/pigpio/index.html 7 | 8 | To install this library please navagate to its folder and sudo python HTU21DF.py install 9 | 10 | I wanted to use this sensor in a project with a Raspberry Pi but I only found drivers written in C, so I wrote my own. 11 | 12 | Please keep in mind that it does not use every function offered by the sensor. When temperature and humidity are read the i2c clock is held though there is a no hold mode available. I also have not writted functions to read and write to the user register. 13 | The only functions are read_temperature, read_humidity, and reset. 14 | 15 | If you use an old revision A Raspberry Pi you will have to change the i2c bus in the driver from 1 to 0. 16 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 D. Alex Gray 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /HTU21DF.py: -------------------------------------------------------------------------------- 1 | # Raspberry Pi Driver for Adafruit HTU21D-F 2 | # Go buy one at https://www.adafruit.com/products/1899 3 | # written by D. Alex Gray dalexgray@mac.com 4 | # Thanks to egutting at the adafruit.com forums 5 | # Thanks to Joan on the raspberrypi.org forums 6 | # This requires the pigpio library 7 | # Get pigpio at http://abyz.co.uk/rpi/pigpio/index.html 8 | # 9 | # Copyright (c) 2014 D. Alex Gray 10 | # Permission is hereby granted, free of charge, to any person obtaining a copy 11 | # of this software and associated documentation files (the "Software"), to deal 12 | # in the Software without restriction, including without limitation the rights 13 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 | # copies of the Software, and to permit persons to whom the Software is 15 | # furnished to do so, subject to the following conditions: 16 | # 17 | # The above copyright notice and this permission notice shall be included in 18 | # all copies or substantial portions of the Software. 19 | # 20 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 26 | # THE SOFTWARE. 27 | import time 28 | import pigpio 29 | import math 30 | 31 | pi = pigpio.pi() 32 | 33 | # HTU21D-F Address 34 | addr = 0x40 35 | 36 | # i2c bus, if you have a Raspberry Pi Rev A, change this to 0 37 | bus = 1 38 | 39 | # HTU21D-F Commands 40 | rdtemp = 0xE3 41 | rdhumi = 0xE5 42 | wtreg = 0xE6 43 | rdreg = 0xE7 44 | reset = 0xFE 45 | 46 | def htu_reset(): 47 | handle = pi.i2c_open(bus, addr) # open i2c bus 48 | pi.i2c_write_byte(handle, reset) # send reset command 49 | pi.i2c_close(handle) # close i2c bus 50 | time.sleep(0.2) # reset takes 15ms so let's give it some time 51 | 52 | def read_temperature(): 53 | handle = pi.i2c_open(bus, addr) # open i2c bus 54 | pi.i2c_write_byte(handle, rdtemp) # send read temp command 55 | time.sleep(0.055) # readings take up to 50ms, lets give it some time 56 | (count, byteArray) = pi.i2c_read_device(handle, 3) # vacuum up those bytes 57 | pi.i2c_close(handle) # close the i2c bus 58 | t1 = byteArray[0] # most significant byte msb 59 | t2 = byteArray[1] # least significant byte lsb 60 | temp_reading = (t1 * 256) + t2 # combine both bytes into one big integer 61 | temp_reading = math.fabs(temp_reading) # I'm an idiot and can't figure out any other way to make it a float 62 | temperature = ((temp_reading / 65536) * 175.72 ) - 46.85 # formula from datasheet 63 | return temperature 64 | 65 | def read_humidity(): 66 | handle = pi.i2c_open(bus, addr) # open i2c bus 67 | pi.i2c_write_byte(handle, rdhumi) # send read humi command 68 | time.sleep(0.055) # readings take up to 50ms, lets give it some time 69 | (count, byteArray) = pi.i2c_read_device(handle, 3) # vacuum up those bytes 70 | pi.i2c_close(handle) # close the i2c bus 71 | h1 = byteArray[0] # most significant byte msb 72 | h2 = byteArray[1] # least significant byte lsb 73 | humi_reading = (h1 * 256) + h2 # combine both bytes into one big integer 74 | humi_reading = math.fabs(humi_reading) # I'm an idiot and can't figure out any other way to make it a float 75 | uncomp_humidity = ((humi_reading / 65536) * 125 ) - 6 # formula from datasheet 76 | # to get the compensated humidity we need to read the temperature 77 | temperature = read_temperature() 78 | humidity = ((25 - temperature) * -0.15) + uncomp_humidity 79 | return humidity 80 | --------------------------------------------------------------------------------