Humidity and Temperature Measurement Using HTS221 and Particle Photon


322

Introduction: Humidity and Temperature Measurement Using HTS221 and Particle Photon

We are a group of makers. We work in IoT, IOS app, android app, embedded design, sensor design, ras…

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 particle photon has been illustrated. To read the humidity and temperature values, we have used particle with an I2c adapter.This I2C adapter makes the connection to the sensor module easy and more reliable.

Step 1: Hardware Required:

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

1. HTS221

2. Particle Photon

3. I2C Cable

4. I2C Shield for particle photon

Step 2: Hardware Hookup:

The hardware hookup section basically explains the wiring connections required between the sensor and the particle photon. 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:

Lets start with the particle code now.

While using the sensor module with the particle, we include application.h and spark_wiring_i2c.h library. "application.h" and spark_wiring_i2c.h library contains the functions which facilitate the i2c communication between the sensor and the particle.

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

<p>#include<application.h> </p><p>#include<spark_wiring_i2c.h> </p><p>// HTS221 I2C address is 0x5F</p><p>#define Addr 0x5F</p><p>double humidity = 0.0;</p><p>double cTemp = 0.0;</p><p>double fTemp = 0.0;</p><p>int temp = 0;</p><p>void setup() </p><p>{  </p><p>// Set variable  </p><p>Particle.variable("i2cdevice", "HTS221");  </p><p>Particle.variable("Humidity", humidity);  </p><p>Particle.variable("cTemp", cTemp);    </p><p>// Initialise I2C communication as MASTER   </p><p>Wire.begin();  </p><p>// Initialise serial communication, set baud rate = 9600  </p><p>Serial.begin(9600);    </p><p>// Start I2C Transmission  </p><p>Wire.beginTransmission(Addr);  </p><p>// Select average configuration register  </p><p>Wire.write(0x10);  </p><p>// Temperature average samples = 256, Humidity average samples = 512  </p><p>Wire.write(0x1B);  </p><p>// Stop I2C Transmission  </p><p>Wire.endTransmission();</p><p>// Start I2C Transmission  </p><p>Wire.beginTransmission(Addr);  </p><p>// Select control register1  </p><p>Wire.write(0x20);  </p><p>// Power ON, Continuous update, Data output rate = 1 Hz  </p><p>Wire.write(0x85);  </p><p>// Stop I2C Transmission  </p><p>Wire.endTransmission();  </p><p>delay(300);</p><p>}
void loop() </p><p>{  </p><p>unsigned int data[2];  </p><p>unsigned int val[4];  </p><p>unsigned int H0, H1, H2, H3, T0, T1, T2, T3, raw;</p><p>// Humidity calliberation values  </p><p>for(int i = 0; i < 2; i++)  </p><p>{    </p><p>// Start I2C Transmission    </p><p>Wire.beginTransmission(Addr);    </p><p>// Send data register    </p><p>Wire.write((48 + i));    </p><p>// Stop I2C Transmission    </p><p>Wire.endTransmission();        </p><p>// Request 1 byte of data    </p><p>Wire.requestFrom(Addr, 1);        </p><p>// Read 1 byte of data    </p><p>if(Wire.available() == 1)    </p><p>{      </p><p>data[i] = Wire.read();    </p><p>}  </p><p>}    </p><p>// Convert Humidity data  </p><p>H0 = data[0] / 2;  </p><p>H1 = data[1] / 2;    </p><p>for(int i = 0; i < 2; i++)  </p><p>{    </p><p>// Start I2C Transmission    </p><p>Wire.beginTransmission(Addr);    </p><p>// Send data register    </p><p>Wire.write((54 + i));    </p><p>// Stop I2C Transmission    </p><p>Wire.endTransmission();        </p><p>// Request 1 byte of data    </p><p>Wire.requestFrom(Addr,1);        </p><p>// Read 1 byte of data    </p><p>if(Wire.available() == 1)    </p><p>{      </p><p>data[i] = Wire.read();    </p><p>}  </p><p>}  </p><p>// Convert Humidity data  </p><p>H2 = (data[1] * 256.0) + data[0];    </p><p>for(int i = 0; i < 2; i++)  </p><p>{    </p><p>// Start I2C Transmission    </p><p>Wire.beginTransmission(Addr);    </p><p>// Send data register    </p><p>Wire.write((58 + i));    </p><p>// Stop I2C Transmission    </p><p>Wire.endTransmission();        </p><p>// Request 1 byte of data    </p><p>Wire.requestFrom(Addr,1);        </p><p>// Read 1 byte of data    </p><p>if(Wire.available() == 1)    </p><p>{     </p><p> data[i] = Wire.read();    </p><p>} </p><p> } </p><p> // Convert Humidity data  </p><p>H3 = (data[1] * 256.0) + data[0];</p><p>// Temperature calliberation values  </p><p>// Start I2C Transmission  </p><p>Wire.beginTransmission(Addr);  </p><p>// Send data register  </p><p>Wire.write(0x32);  </p><p>// Stop I2C Transmission  </p><p>Wire.endTransmission();</p><p>// Request 1 byte of data  </p><p>Wire.requestFrom(Addr,1);</p><p>// Read 1 byte of data  </p><p>if(Wire.available() == 1)  </p><p>{    </p><p>T0 = Wire.read();  </p><p>}</p><p>// Start I2C Transmission  </p><p>Wire.beginTransmission(Addr);  </p><p>// Send data register  </p><p>Wire.write(0x33);  </p><p>// Stop I2C Transmission  </p><p>Wire.endTransmission();</p><p>// Request 1 byte of data  </p><p>Wire.requestFrom(Addr,1);</p><p>// Read 1 byte of data  </p><p>if(Wire.available() == 1)  </p><p>{    </p><p>T1 = Wire.read();  </p><p>}    </p><p>// Start I2C Transmission  </p><p>Wire.beginTransmission(Addr);  </p><p>// Send data register  </p><p>Wire.write(0x35);  </p><p>// Stop I2C Transmission  </p><p>Wire.endTransmission();    </p><p>// Request 1 byte of data  </p><p>Wire.requestFrom(Addr, 1); </p><p>// Read 1 byte of data  </p><p>if(Wire.available() == 1)  </p><p>{    </p><p>raw = Wire.read();  </p><p>}</p><p>raw = raw & 0x0F;    </p><p>// Convert the temperature calliberation values to 10-bits  </p><p>T0 = ((raw & 0x03) * 256) + T0;  </p><p>T1 = ((raw & 0x0C) * 64) + T1;</p><p>for(int i = 0; i < 2; i++)  </p><p>{    </p><p>// Start I2C Transmission    </p><p>Wire.beginTransmission(Addr);    </p><p>// Send data register    </p><p>Wire.write((60 + i));    </p><p>// Stop I2C Transmission    </p><p>Wire.endTransmission();        </p><p>// Request 1 byte of data    </p><p>Wire.requestFrom(Addr,1);        </p><p>// Read 1 byte of data    </p><p>if(Wire.available() == 1)    </p><p>{      </p><p>data[i] = Wire.read();    </p><p>}  </p><p>}  </p><p>// Convert the data  </p><p>T2 = (data[1] * 256.0) + data[0];      </p><p>for(int i = 0; i < 2; i++)  </p><p>{    </p><p>// Start I2C Transmission    </p><p>Wire.beginTransmission(Addr);    </p><p>// Send data register    </p><p>Wire.write((62 + i));    </p><p>// Stop I2C Transmission    </p><p>Wire.endTransmission();        </p><p>// Request 1 byte of data    </p><p>Wire.requestFrom(Addr,1);        </p><p>// Read 1 byte of data    </p><p>if(Wire.available() == 1)    </p><p>{      </p><p>data[i] = Wire.read();    </p><p>}  </p><p>}  </p><p>// Convert the data </p><p> T3 = (data[1] * 256.0) + data[0];    </p><p>// Start I2C Transmission  </p><p>Wire.beginTransmission(Addr);  </p><p>// Send data register  </p><p>Wire.write(0x28 | 0x80);  </p><p>// Stop I2C Transmission  </p><p>Wire.endTransmission();    </p><p>// Request 4 bytes of data  </p><p>Wire.requestFrom(Addr,4);    </p><p>// Read 4 bytes of data  </p><p>// humidity msb, humidity lsb, temp msb, temp lsb  </p><p>if(Wire.available() == 4)  </p><p>{    </p><p>val[0] = Wire.read();    </p><p>val[1] = Wire.read();    </p><p>val[2] = Wire.read();    </p><p>val[3] = Wire.read();  </p><p>}    </p><p>// Convert the data  </p><p>humidity = (val[1] * 256.0) + val[0];  </p><p>humidity = ((1.0 * H1) - (1.0 * H0)) * (1.0 * humidity - 1.0 * H2) / (1.0 * H3 - 1.0 * H2) + (1.0 * H0);  </p><p>temp = (val[3] * 256) + val[2];  cTemp = (((T1 - T0) / 8.0) * (temp - T2)) / (T3 - T2) + (T0 / 8.0);  </p><p>fTemp = (cTemp * 1.8 ) + 32;    </p><p>// Output data to dashboard  </p><p>Particle.publish("Relative humidity : ", String(humidity));  </p><p>delay(1000);  </p><p>Particle.publish("Temperature in Celsius : ", String(cTemp));  </p><p>delay(1000);  </p><p>Particle.publish("Temperature in Fahrenheit : ", String(fTemp));  </p><p>delay(1000);</p><p>}</p>

Particle.variable() function creates the variables to store the output of the sensor and Particle.publish() function displays the output on the dashboard of the site.

The sensor output is shown in the picture above for your reference.

Step 4: 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.

Be the First to Share

    Recommendations

    • Mason Jar Speed Challenge

      Mason Jar Speed Challenge
    • Bikes Challenge

      Bikes Challenge
    • Remix Contest

      Remix Contest

    Discussions