Home Code Raspberry Pi 3 and HDC2080 humidity and temperature sensor python example

Raspberry Pi 3 and HDC2080 humidity and temperature sensor python example

by shedboy71

In this article we look at yet another humidity and temperature sensor from TI – this time its the HDC2080 which we will connect up to a Raspberry Pi


Lets look at some of the technical information and data from TI

HDC2080 Information

The HDC2080 device is an integrated humidity and temperature sensor that provides high accuracy measurements with very low power consumption in a small DFN package. The capacitive-based sensor includes new integrated digital features and a heating element to dissipate condensation and moisture.

The HDC2080 digital features include programmable interrupt thresholds to provide alerts and system wake-ups without requiring a microcontroller to be continuously monitoring the system. Combined with programmable sampling intervals, a low power consumption, and a support for a 1.8-V supply voltage, the HDC2080 is designed for battery-operated systems.

The HDC2080 provides high accuracy measurement capability for a wide range of environmental monitoring and Internet of Things (IoT) applications such as smart thermostats and smart home assistants. For designs where printed-circuit board (PCB) area is critical, a smaller CSP package option is available thru the HDC2010 with complete software compatibility with the HDC2080.

For applications with strict power-budget restrictions, Auto Measurement Mode enables the HDC2080 to automatically initiate temperature and humidity measurements. This feature allows users to configure a microcontroller into deep sleep mode because the HDC2080 is no longer dependent upon the microcontroller to initiate a measurement.

Programable temperature and humidity thresholds in the HDC2080 allow the device to send a hardware interrupt to wake up the microcontroller when necessary. In addition, the power consumption of the HDC2080 is significantly reduced, which helps to minimize self-heating and improve measurement accuracy.

The HDC2080 is factory-calibrated to 0.2°C temperature accuracy and 2% relative humidity accuracy.

Features

Relative humidity range: 0% to 100%
Humidity accuracy: ±2% (typical), ±3% (maximum)
Temperature accuracy: ±0.2°C (typical), ±0.4°C (maximum)
Sleep mode current: 50 nA (typical), 100 nA (maximum)
Average supply current (1 measurement/second)
300 nA: RH% only (11 bit)
550 nA: RH% (11 bit) + temperature (11 bit)

Temperature range:
Operating: –40°C to 85°C
Functional: –40°C to 125°C
Supply voltage range: 1.62 V to 3.6 V

 

Parts Required

 

Name Link
Raspberry Pi Aliexpress product link

Amazon. com link

Ebay search

HDC2080 Sensor Aliexpress product link

Amazon link

ebay link

Connecting wire Aliexpress product link

Amazon.com link

Ebay link

Raspberry Pi 3 Expansion Board (optional) Raspberry Pi 3 Expansion Board

Schematic/Connection

This is a 3.3v rated sensor even though the pin says Vcc

Raspberry Pi Sensor
3v3 Vcc
GND Gnd
SDA SDA
SCL SCL

Code Example

I used the code from https://github.com/shadowpho/Pi_HDC2080

This is the library

[codesyntax lang=”python”]

HDC2000_ADDRESS =                       (0x40)    # 1000000 
# Register
#2 bytes
HDC2000_TEMPERATURE_REGISTER =          (0x00)
HDC2000_HUMIDITY_REGISTER =             (0x02)
HDC2000_RESET_REGISTER        =         (0x0E)
HDC2000_CONFIG_REGISTER =        (0x0F)
HDC2000_MANUFACTURERID_REGISTER =       (0xFC)
HDC2000_DEVICEID_REGISTER =         (0xFE)


#Configuration Register Bits

HDC2000_RESET_RESET_BIT =              (0x80)
HDC2000_RESET_HEATER_ENABLE =          (0x8)
HDC2000_CONFIG_GO =       (0x1)

I2C_SLAVE=0x0703

import struct, array, time, io, fcntl

HDC2000_fw= 0
HDC2000_fr= 0

class Pi_HDC2080:

        def readManufacturerID(self):
            s = [HDC2000_MANUFACTURERID_REGISTER] 
            s2 = bytearray( s )
            HDC2000_fw.write( s2 )
            time.sleep(0.0625)          
            data = HDC2000_fr.read(2) #read 2 byte config data
            buf = array.array('B', data)
            return buf[0] * 256 + buf[1]

        def readDeviceID(self):
            s = [HDC2000_DEVICEID_REGISTER] 
            s2 = bytearray( s )
            HDC2000_fw.write( s2 )
            time.sleep(0.0625)              
            data = HDC2000_fr.read(2) #read 2 byte config data
            buf = array.array('B', data)
            return buf[0] * 256 + buf[1]


        def __init__(self, twi=1, addr=HDC2000_ADDRESS ):
                global HDC2000_fr, HDC2000_fw
                
                HDC2000_fr= io.open("/dev/i2c-"+str(twi), "rb", buffering=0)
                HDC2000_fw= io.open("/dev/i2c-"+str(twi), "wb", buffering=0)

                a1 = fcntl.ioctl(HDC2000_fr, I2C_SLAVE, HDC2000_ADDRESS)
                a2 = fcntl.ioctl(HDC2000_fw, I2C_SLAVE, HDC2000_ADDRESS)
                if (a1 <0 or a2 < 0):
                    exit() #abort
                time.sleep(0.1) 
                config = HDC2000_RESET_RESET_BIT
                s = [HDC2000_RESET_REGISTER,config]
                s2 = bytearray( s )
                HDC2000_fw.write( s2 ) 
                time.sleep(0.1)             
                if(self.readManufacturerID() != 0x4954):
                    print("ERROR CRITICAL MANUFACTURE ID NOT MATCH")
                    exit()
                if(self.readDeviceID() != 0xD007):
                    print("ERROR CRITICAL DEVICE ID NOT MATCH")
                    exit()


        # public functions

        def readTemperature(self):
                s = [HDC2000_CONFIG_REGISTER, HDC2000_CONFIG_GO ]
                HDC2000_fw.write(bytearray(s)) #GO
                s = [HDC2000_TEMPERATURE_REGISTER] # temp
                HDC2000_fw.write(bytearray(s))
                time.sleep(0.1)  #wait for measure to be done            
                data = HDC2000_fr.read(2) 
                buf = array.array('B', data)
                
                # Convert the data
                temp = (buf[0]) + (buf[1]*256)
                cTemp = (temp / 65536.0) * 165.0 - 40
                return cTemp


        def readHumidity(self):

                s = [HDC2000_CONFIG_REGISTER, HDC2000_CONFIG_GO ]
                HDC2000_fw.write(bytearray(s)) #GO
                s = [HDC2000_HUMIDITY_REGISTER] #humidity
                HDC2000_fw.write(bytearray(s))
                time.sleep(0.1)              
                data = HDC2000_fr.read(2) #read 2 byte humidity data
                buf = array.array('B', data)
                humidity = (buf[0]) + (buf[1]*256)
                humidity = (humidity / 65536.0) * 100.0
                return humidity
        
        def readConfigRegister(self):
                s = [HDC2000_CONFIG_REGISTER] # temp
                s2 = bytearray( s )
                HDC2000_fw.write( s2 )
                data = HDC2000_fr.read(1) #read 2 byte config data
                buf = array.array('B', data)
                return buf[0]

       
        def turnHeaterOn(self):
                return

        def turnHeaterOff(self):
                return

        

        def setHumidityResolution(self,resolution):
                return

        def setTemperatureResolution(self,resolution):
                return

[/codesyntax]

And you call it like this

[codesyntax lang=”python”]

#!/usr/bin/python
import sys
import Pi_HDC2080

hdc2000 = Pi_HDC2080.Pi_HDC2080()

print ("temperature : %3.1f,"humidity : %3.1f " %(hdc2000.readTemperature(), hdc2000.readHumidity()))

[/codesyntax]

Output

Here are a few test runs

>>> %Run testHDC2080.py
temperature : 27.9, humidity : 35.4
>>> %Run testHDC2080.py
temperature : 27.8, humidity : 35.6
>>> %Run testHDC2080.py
temperature : 27.7, humidity : 35.7

Links

https://www.ti.com/lit/gpn/hdc2080

 

Summary

A nice little sensor but at just over $8 there are cheaper sensors available which have the same functionality, the library and code may need some work to work more reliably.

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More