Introduction: Humidity and Temperature Measurement Using HTS221 and Arduino Nano

HTS221 is an ultra compact capacitive digital sensor for relative humidity and temperature. It includes a sensing element and a mixed signal application specific integrated circuit(ASIC) to provide the measurement information through digital serial interfaces. Integrated with so many features this is one of the most appropriate sensors for critical humidity and temperature measurements.

In this tutorial the interfacing of the HTS221 sensor module with arduino nano has been illustrated. To read the humidity and temperature values, we have used arduino with an I2c adapter.This I2C adapter makes the connection to the sensor module easy and more reliable.

Step 1: Hardware Required:

Picture of Hardware Required:

The materials that we need for accomplishing our goal includes the following hardware components:

1. HTS221

2. Arduino Nano

3. I2C Cable

4. I2C Shield for Arduino Nano

Step 2: Hardware Hookup:

Picture of Hardware Hookup:

The hardware hookup section basically explains the wiring connections required between the sensor and the arduino nano. Ensuring correct connections is the basic necessity while working on any system for the desired output. So, the requisite connections are as follows:

The HTS221 will work over I2C . Here is the example wiring diagram, demonstrating how to wire up each interface of the sensor.

Out-of-the-box, the board is configured for an I2C interface, as such we recommend using this hookup if you’re otherwise agnostic.

All you need is four wires! Only four connections are required Vcc, Gnd, SCL and SDA pins and these are connected with the help of I2C cable.

These connections are demonstrated in the pictures above.

Step 3: Code for Humidity and Temperature Measurement:

Picture of Code for Humidity and Temperature Measurement:

Lets start with the arduino code now.

While using the sensor module with the arduino, we include Wire.h library. "Wire" library contains the functions which facilitate the i2c communication between the sensor and the arduino board.

The entire arduino code is given below for the convenience of the user:

#include<Wire.h>

// HTS221 I2C address is 0x5F

#define Addr 0x5F

void setup()

{

// Initialise I2C communication as MASTER

Wire.begin();

// Initialise serial communication, set baud rate = 9600

Serial.begin(9600);

// Start I2C Transmission

Wire.beginTransmission(Addr);

// Select average configuration register

Wire.write(0x10);

// Temperature average samples = 256, Humidity average samples = 512

Wire.write(0x1B);

// Stop I2C Transmission

Wire.endTransmission();

// Start I2C Transmission

Wire.beginTransmission(Addr);

// Select control register1

Wire.write(0x20);

// Power ON, Continuous update, Data output rate = 1 Hz

Wire.write(0x85);

// Stop I2C Transmission

Wire.endTransmission();

delay(300);

}

void loop()

{

unsigned int data[2];

unsigned int val[4];

unsigned int H0, H1, H2, H3, T0, T1, T2, T3, raw;

// Humidity calliberation values

for(int i = 0; i < 2; i++)

{

// Start I2C Transmission

Wire.beginTransmission(Addr);

// Send data register

Wire.write((48 + i));

// Stop I2C Transmission

Wire.endTransmission();

// Request 1 byte of data

Wire.requestFrom(Addr, 1);

// Read 1 byte of data

if(Wire.available() == 1)

{

data[i] = Wire.read();

}

}

// Convert Humidity data

H0 = data[0] / 2;

H1 = data[1] / 2;

for(int i = 0; i < 2; i++)

{

// Start I2C Transmission

Wire.beginTransmission(Addr);

// Send data register

Wire.write((54 + i));

// Stop I2C Transmission

Wire.endTransmission();

// Request 1 byte of data

Wire.requestFrom(Addr,1);

// Read 1 byte of data

if(Wire.available() == 1)

{

data[i] = Wire.read();

}

}

// Convert Humidity data

H2 = (data[1] * 256.0) + data[0];

for(int i = 0; i < 2; i++)

{

// Start I2C Transmission

Wire.beginTransmission(Addr);

// Send data register

Wire.write((58 + i));

// Stop I2C Transmission

Wire.endTransmission();

// Request 1 byte of data

Wire.requestFrom(Addr,1);

// Read 1 byte of data

if(Wire.available() == 1)

{

data[i] = Wire.read();

}

}

// Convert Humidity data

H3 = (data[1] * 256.0) + data[0];

// Temperature calliberation values

// Start I2C Transmission

Wire.beginTransmission(Addr);

// Send data register

Wire.write(0x32);

// Stop I2C Transmission

Wire.endTransmission();

// Request 1 byte of data

Wire.requestFrom(Addr,1);

// Read 1 byte of data

if(Wire.available() == 1)

{

T0 = Wire.read();

}

// Start I2C Transmission

Wire.beginTransmission(Addr);

// Send data register

Wire.write(0x33);

// Stop I2C Transmission

Wire.endTransmission();

// Request 1 byte of data

Wire.requestFrom(Addr,1);

// Read 1 byte of data

if(Wire.available() == 1)

{

T1 = Wire.read();

}

// Start I2C Transmission

Wire.beginTransmission(Addr);

// Send data register

Wire.write(0x35);

// Stop I2C Transmission

Wire.endTransmission();

// Request 1 byte of data

Wire.requestFrom(Addr, 1);

// Read 1 byte of data

if(Wire.available() == 1)

{

raw = Wire.read();

}

raw = raw & 0x0F;

// Convert the temperature calliberation values to 10-bits

T0 = ((raw & 0x03) * 256) + T0;

T1 = ((raw & 0x0C) * 64) + T1;

for(int i = 0; i < 2; i++)

{

// Start I2C Transmission

Wire.beginTransmission(Addr);

// Send data register

Wire.write((60 + i));

// Stop I2C Transmission

Wire.endTransmission();

// Request 1 byte of data

Wire.requestFrom(Addr,1);

// Read 1 byte of data

if(Wire.available() == 1)

{

data[i] = Wire.read();

}

}

// Convert the data

T2 = (data[1] * 256.0) + data[0];

for(int i = 0; i < 2; i++)

{

// Start I2C Transmission

Wire.beginTransmission(Addr);

// Send data register

Wire.write((62 + i));

// Stop I2C Transmission

Wire.endTransmission();

// Request 1 byte of data

Wire.requestFrom(Addr,1);

// Read 1 byte of data

if(Wire.available() == 1)

{

data[i] = Wire.read();

}

}

// Convert the data

T3 = (data[1] * 256.0) + data[0];

// Start I2C Transmission

Wire.beginTransmission(Addr);

// Send data register

Wire.write(0x28 | 0x80);

// Stop I2C Transmission

Wire.endTransmission();

// Request 4 bytes of data

Wire.requestFrom(Addr,4);

// Read 4 bytes of data

// humidity msb, humidity lsb, temp msb, temp lsb

if(Wire.available() == 4)

{

val[0] = Wire.read();

val[1] = Wire.read();

val[2] = Wire.read();

val[3] = Wire.read();

}

// Convert the data

float humidity = (val[1] * 256.0) + val[0];

humidity = ((1.0 * H1) - (1.0 * H0)) * (1.0 * humidity - 1.0 * H2) / (1.0 * H3 - 1.0 * H2) + (1.0 * H0);

int temp = (val[3] * 256) + val[2];

float cTemp = (((T1 - T0) / 8.0) * (temp - T2)) / (T3 - T2) + (T0 / 8.0);

float fTemp = (cTemp * 1.8 ) + 32;

// 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);

}

In wire library Wire.write() and Wire.read() is used to write the commands and read the sensor output.

Serial.print() and Serial.println() is used to display the output of the sensor on the serial monitor of the Arduino IDE.

The output of the sensor is shown in the picture above.

Step 4: Applications:

Picture of Applications:

HTS221 can be employed in various consumer products like air humidifiers and refrigerators etc. This sensor also find its application in a wider arena including Smart home automation, Industrial automation, respiratory equipments, asset and goods tracking.

Comments

About This Instructable

332views

0favorites

More by Dcube Tech Ventures:Home Automation with Raspberry Pi using Relay boardMotion Control with Raspberry Pi and LIS3DHTR, 3-Axis Accelerometer, using Python3-Axis Accelerometer, ADXL345 with Raspberry Pi using Python
Add instructable to: