HDC1080 humidity and temperature sensor micro:bit example

The HDC1080 is a digital humidity sensor with integrated temperature sensor that provides excellent measurement accuracy at very low power. The HDC1080 operates over a wide supply range, and is a low cost, low power alternative to competitive solutions in a wide range of common applications. The humidity and temperature sensors are factory calibrated.

Features

  • Relative Humidity Accuracy ±2% (typical)
  • Temperature Accuracy ±0.2°C (typical)
  • Excellent Stability at High Humidity
  • 14 Bit Measurement Resolution
  • 100 nA Sleep Mode Current

Schematics

 

microbit and hdc1080

microbit and hdc1080

 

Code

 

#include<Wire.h>
 
// HDC1000 I2C address is 0x40(64)
#define Addr 0x40
 
void setup()
{
// Initialise I2C communication as MASTER
Wire.begin();
// Initialise serial communication, set baud rate = 9600
Serial.begin(9600);
 
// Starts I2C communication
Wire.beginTransmission(Addr);
// Select configuration register
Wire.write(0x02);
// Temperature, humidity enabled, resolultion = 14-bits, heater on
Wire.write(0x30);
// Stop I2C Transmission
Wire.endTransmission();
delay(300);
}
 
void loop()
{
unsigned int data[2];
 
// Starts I2C communication
Wire.beginTransmission(Addr);
// Send temp measurement command
Wire.write((byte)0x00);
// Stop I2C Transmission
Wire.endTransmission();
delay(500);
 
// Request 2 bytes of data
Wire.requestFrom(Addr, 2);
 
// Read 2 bytes of data
// temp msb, temp lsb
if (Wire.available() == 2)
{
data[0] = Wire.read();
data[1] = Wire.read();
}
// Convert the data
int temp = (data[0] * 256) + data[1];
float cTemp = (temp / 65536.0) * 165.0 - 40;
float fTemp = cTemp * 1.8 + 32;
 
// Starts I2C communication
Wire.beginTransmission(Addr);
// Send humidity measurement command
Wire.write(0x01);
// Stop I2C Transmission
Wire.endTransmission();
delay(500);
 
// Request 2 bytes of data
Wire.requestFrom(Addr, 2);
 
// Read 2 bytes of data
// humidity msb, humidity lsb
if (Wire.available() == 2)
{
data[0] = Wire.read();
data[1] = Wire.read();
}
 
// Convert the data
float humidity = (data[0] * 256) + data[1];
humidity = (humidity / 65536.0) * 100.0;
 
// Output data to serial monitor
Serial.print("Relative Humidity :");
Serial.print(humidity);
Serial.println(" %RH");
Serial.print("Temperature in Celsius :");
Serial.print(cTemp);
Serial.println(" C");
Serial.print("Temperature in Fahrenheit :");
Serial.print(fTemp);
Serial.println(" F");
delay(500);
}

 

 

Output

In the serial monitor

Temperature in Celsius :20.95 C
Temperature in Fahrenheit :69.71 F
Relative Humidity :34.31 %RH
Temperature in Celsius :26.02 C
Temperature in Fahrenheit :78.84 F
Relative Humidity :36.02 %RH
Temperature in Celsius :27.09 C
Temperature in Fahrenheit :80.77 F
Relative Humidity :35.77 %RH

 

Links

1PCS HDC1080 high-precision temperature and humidity sensor module

Leave a Reply