Particle Photon - HTS221 Relative Humidity and Temperature Sensor Tutorial
by Dcube Tech Ventures in Circuits > Electronics
12 Views, 0 Favorites, 0 Comments
Particle Photon - HTS221 Relative Humidity and Temperature Sensor Tutorial
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. Here is the demonstration with particle photon.
What You Need..!!
Connections:
Take an I2C shield for particle photon and gently push it over the pins of particle photon.
Then connect the one end of I2C cable HTS221 sensor and the other end to the I2C shield.
Connections are shown in the picture above.
Code:
The particle code for HTS221 can be downloaded from our github repository- DCUBE Community.
Here is the link for the same :
https://github.com/DcubeTechVentures/HTS221/blob/master/Particle/HTS221.ino
We have used two libraries for particle code, which are application.h and spark_wiring_i2c.h. Spark_wiring_i2c library is required to facilitate the I2C communication with the sensor.
You can also copy the code from here, it is given as follows:
// Distributed with a free-will license.
// Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
// HTS221
// This code is designed to work with the HTS221_I2CS I2C Mini Module
#include
#include
// HTS221 I2C address is 0x5F
#define Addr 0x5F
float humidity = 0.0;
float cTemp = 0.0;
float fTemp = 0.0;
int temp = 0;
void setup()
{
// Set variable
Particle.variable("i2cdevice", "HTS221");
Particle.variable("Humidity", humidity);
Particle.variable("cTemp", cTemp);
// 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 Humiditydata
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
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);
temp = (val[3] * 256) + val[2];
cTemp = (((T1 - T0) / 8.0) * (temp - T2)) / (T3 - T2) + (T0 / 8.0);
fTemp = (cTemp * 1.8 ) + 32;
// Output data to dashboard
Particle.publish("Relative humidity : ", String(humidity));
Particle.publish("Temperature in Celsius : ", String(cTemp));
Particle.publish("Temperature in Fahrenheit : ", String(fTemp));
delay(1000);
}
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.