Home STM32 STM32 Nucleo and HDC1008 sensor Arduino example

STM32 Nucleo and HDC1008 sensor Arduino example

by shedboy71

OK we are now going to look at an HDC1008 attached to the STM32 Nucleo and all development will be done on the Arduino IDE – Install STM32 support in the Arduino IDE

The HDC1008 is a digital humidity sensor with integrated temperature sensor that provides excellent measurement accuracy at very low power. The device measures humidity based on a novel capacitive sensor. The humidity and temperature sensors are factory calibrated.

The sensing element of the HDC1008 is placed on the bottom part of the device, which makes the HDC1000 more robust against dirt, dust, and other environmental contaminants. The HDC1000 is functional within the full –40°C to +125°C temperature range.

Key Features

  • Relative Humidity (RH) Operating Range 0% to 100%
  • 14 Bit Measurement Resolution
  • Relative Humidity Accuracy ±3%
  • Temperature Accuracy ±0.2°C
  • Supply Voltage 3 V to 5 V
  • I2C Interface

Connection

Code

[codesyntax lang=”python”]

#include<Wire.h>

// HDC1008 I2C address is 0x40(64)
#define hdcAddr 0x40

void setup()
{
  Wire.begin();
  Serial.begin(9600);

  // Starts I2C communication
  Wire.beginTransmission(hdcAddr);
  // 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];

  Wire.beginTransmission(hdcAddr);
  // Send temp measurement command
  Wire.write(0x00);
  Wire.endTransmission();
  delay(500);

  // Request 2 bytes of data
  Wire.requestFrom(hdcAddr, 2);

  // Read 2 bytes of data for temperature
  if (Wire.available() == 2)
  {
    data[0] = Wire.read();
    data[1] = Wire.read();
  }

  int temp = (data[0] * 256) + data[1];
  float celsTemp = (temp / 65536.0) * 165.0 - 40;
  float fahrTemp = celsTemp * 1.8 + 32;


  Wire.beginTransmission(hdcAddr);
  // Send humidity measurement command
  Wire.write(0x01);
  Wire.endTransmission();
  delay(500);

  // Request 2 bytes of data
  Wire.requestFrom(hdcAddr, 2);
  // Read 2 bytes of data to get humidity
  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;

  Serial.print("Humidity : ");
  Serial.print(humidity);
  Serial.println(" %RH");
  Serial.print("Celsius : ");
  Serial.print(celsTemp);
  Serial.println(" C");
  Serial.print("Fahrenheit : ");
  Serial.print(fahrTemp);
  Serial.println(" F");
  delay(500);
}

[/codesyntax]

 

Links

HDC1008 Temperature Humidity Sensor Breakout Board for Arduino

You may also like