Home Code HTU31 Humidity and Temperature Sensor and Arduino Uno

HTU31 Humidity and Temperature Sensor and Arduino Uno

by shedboy71
[lebox id="1"]

In this article we connect an HTU31 Humidity and Temperature Combination Sensor to an Arduino Uno – other arduino boards should be Ok as well

Sensor Information

The HTU31 is one of the smallest and most accurate humidity sensors on the market. TE Connectivity precision engineering and 20+ years of experience in humidity and temperature combination sensors, enabled this new product with fast response time, precision measurement, low hysteresis, robustness to reflow assembly process and sustained performance even when exposed to extreme temperature [-40° to 125°C] and humidity [0%RH to 100%RH] environments.

HTU31 humidity & temperature sensor includes both digital (D) and analog (V) versions and combines multiple functions with an application-friendly operating supply voltage range from 3V to 5.5V

Product Type Features

Humidity Sensor Component Product Type  RHT
Humidity Sensor Component Sensor Type  Digital

Electrical Characteristics

Supply Voltage (Peak) (V) – 6v maximum
Current Consumption (mA) .76, .77, .78

Usage Conditions

Humidity Operating Range (%RH) – Humidity measurement range is 0 to 100% relative humidity
Operating Temperature Range (°C) – Temperature measurement range is -40 -degrees C to +125-degrees C
Operating Temperature Range (°F) -40 – 257, -40 – 258, -40 – 259

Parts Required

The sensor you can pick up in the $3 price range – you can connect to the sensor using a standard header the classic dupont style jumper wire.

Name Link
Arduino Uno UNO R3 CH340G with usb cable
HTU31D HTU31D temperature and humidity sensor module
Connecting cables Male to Male + Male to Female and Female to Female Jumper Wire Dupont Cable

 

Schematic/Connection

I used 3.3v from the Arduino Uno – 5v should be ok. The part says HTU21 but HTU31 is the same.

Arduino Uno and HTU31 layout

Arduino Uno and HTU31 layout

Code Example

This uses a library from Adafruit installed using the Library Manager in the Arduino IDE. search for HTU31, and select the Adafruit_HTU31D library. You will also need to add another couple of libraries Adafruit BusIO library and the Adafruit Unified Sensor library

[codesyntax lang=”cpp”]

#include <Wire.h>
#include "Adafruit_HTU31D.h"

Adafruit_HTU31D htu = Adafruit_HTU31D();
uint32_t timestamp;
bool heaterEnabled = false;

void setup() {
  Serial.begin(115200);
  while (!Serial) {
    delay(10); // wait till serial port opens
  }
  Serial.println("Adafruit HTU31D test");

  if (!htu.begin(0x40)) {
    Serial.println("Couldn't find sensor!");
    while (1);
  }
  timestamp = millis();
}

void loop() {
  sensors_event_t humidity, temp;
  
  htu.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data

  Serial.print("Temp: "); 
  Serial.print(temp.temperature);
  Serial.println(" C");
  
  Serial.print("Humidity: ");
  Serial.print(humidity.relative_humidity);
  Serial.println(" \% RH");

  // every 5 seconds
  if ((millis() - timestamp) > 5000) {
    // toggle the heater
    heaterEnabled = !heaterEnabled;
    if (heaterEnabled) {
      Serial.println("Turning on heater");
    } else {
      Serial.println("Turning off heater");
    }
    if (! htu.enableHeater(heaterEnabled)) {
      Serial.println("Command failed");
    }

    timestamp = millis();
  }

  delay(500);
}

[/codesyntax]

Output

Here is  an example of what I saw in the serial monitor window – you may see some different results

Temp: 23.11 C
Humidity: 55.05 % RH
Temp: 22.79 C
Humidity: 51.46 % RH
Temp: 22.47 C
Humidity: 47.98 % RH

 

Links

 

 

 

 

Share
[lebox id="2"]

You may also like

Leave a Comment