HTS221 v2 Temperature and Humidity Sensor
This library is a built-in class that provides functions to control the STMicroelectronics HTS221 temperature and humidity sensor.
Library usage requirements | |
---|---|
Type | I2C |
Name | HTS221_V2 |
Version | 2020-11-04 |
Code size used | 3.4KB |
Resources used | I2C x 1, Timers x 1 |
Related Documents
For information on the STMicroelectronics HTS221 Temperature and Humidity sensor, please refer to the datasheet.
Abstracts
Methods()/Properties | Summary |
---|---|
new HTS221() | Creates an HTS221 instance. |
{HTS221} Instance
Methods()/Properties | Summary |
---|---|
.write() | Writes 1 byte of data to the specified register of the I2C device. |
.read() | Reads 1 byte of data from the specified register of the I2C device. |
.init() | Initializes the sensor. |
.measOn() | Powers on the sensor and starts a single measurement. |
.measOff() | Powers off the sensor. |
.checkStatus() | Checks the measurement status of the sensor. |
.getResult() | Retrieves the temperature and humidity measurement results. |
.checkStatusAndGetResult() | Waits for the sensor measurements to complete, then reads the temperature and humidity measurement results. |
Details
new HTS221(i2c)
Creates an HTS221
instance using an activated I2C object.
Instance Methods
.write()
Writes 1 byte of data to the specified register of the I2C device.
This method is used to access the sensor registers directly.
Name | Type | Default | Summary |
---|---|---|---|
reg | number | MANDATORY | Target register Range: 0x00-0xFF |
data | number | MANDATORY | Write data Range: 0x00-0xFF |
return | boolean | - | true: Success false: Failure |
.read(reg)
Reads 1 byte of data from the specified register of the I2C device.
This method is used to access the sensor registers directly.
Name | Type | Default | Summary |
---|---|---|---|
reg | number | MANDATORY | Target register Range: 0x00-0xFF |
return | number, undefined | - | Read data If the read function fails, undefined will be returned. |
.init([avgT[,avgH]])
Initializes the sensor.
This method must be called first after the instance is created.
It is possible to change the resolution mode of the sensor for temperature and humidity measurements. For more information, please refer to the sensor datasheet.
Name | Type | Default | Summary |
---|---|---|---|
avgT | number | 7 | Number of averaged temperature samples Range: 0 - 7 |
avgH | number | 7 | Number of averaged humidity samples Range: 0 - 7 |
return | boolean | - | true: Success false: Failure |
.measOn()
Powers on the sensor and uses the sensor's one-shot
measurement function to start a single measurement of temperature and humidity.
It is necessary to wait for the measurements to complete with .checkStatus()
.
It will also be necessary to call this method again to start a new measurement.
Name | Type | Default | Summary |
---|---|---|---|
return | undefined | - |
.measOff()
Powers off the sensor, reducing power consumption.
Name | Type | Default | Summary |
---|---|---|---|
return | undefined | - |
.checkStatus()
Checks the measurement status of the sensor.
Name | Type | Default | Summary |
---|---|---|---|
return | boolean | - | true: The measurements are complete and the data is ready to be read. false: The measurements are in progress. |
.getResult()
Retrieves the temperature and humidity measurement results.
Name | Type | Default | Summary |
---|---|---|---|
return | Object, undefined | - | The measurement results are returned as {Results} objects. If the measurement fails, undefined will be returned. |
{Results}
Name | Type | Description | Note |
---|---|---|---|
.temp | number | Temperature (°C) | |
.humi | number | Humidity (%RH) |
.checkStatusAndGetResult()
Waits for the sensor measurements to complete, then retrieves the temperature and humidity measurement results.
Name | Type | Default | Summary |
---|---|---|---|
return | Object, undefined | - | The measurement results are returned as {Results} objects. If the measurement fails, undefined will be returned. |
Usage Examples
Sample 1: Single measurement
// IMPORTED LIBRARIES
// - HTS221_V2
// Logging setup
log.setLevel(0,2); //-1:NONE 0:ERROR 1:WARNING 2:DEBUG 3:TRACE, 0:DISABLE 1:LOG 2:CONSOLE 3:BOTH
log.printLevel(2); //0:DISABLE 1:LOG 2:CONSOLE 3:BOTH
// I2C Configuration
var i2c;
if('nqBridge' in this){
if(nqEx.getBoardType()){
// Using NEQTO Bridge IO Board or NEQTO Bridge Digital IO Board.
nqEx.enI2CS(true);
nqEx.enI2CL(true);
}
i2c = new I2C(1);
} else if('nqSpresense' in this){
i2c = new I2C(0);
} else if('nqDiscovery' in this){
i2c = new I2C(2);
} else{
throw("Incompatible device.");
}
i2c.open(400000); // Baudrate: 400000
var hts221 = new HTS221(i2c);
var ready = hts221.init();
if (!ready) {
print("Error: Unable to connect to HTS221");
} else {
var dateTime = new Date();
hts221.measOn();
while(!hts221.checkStatus());
var results = hts221.getResult();
hts221.measOff();
if(!results) {
print("Error: Failed measurement.");
} else{
print(dateTime.toString() + ", Temp: " + results.temp + ", Humi: " + results.humi);
}
}
i2c.close();
Sample 2: Interval-based measurements and then calculate average
// IMPORTED LIBRARIES
// - HTS221_V2
// Logging setup
log.setLevel(0,2); //-1:NONE 0:ERROR 1:WARNING 2:DEBUG 3:TRACE, 0:DISABLE 1:LOG 2:CONSOLE 3:BOTH
log.printLevel(2); //0:DISABLE 1:LOG 2:CONSOLE 3:BOTH
// I2C Configuration
var i2c;
if('nqBridge' in this){
if(nqEx.getBoardType()){
// Using NEQTO Bridge IO Board or NEQTO Bridge Digital IO Board.
nqEx.enI2CS(true);
nqEx.enI2CL(true);
}
i2c = new I2C(1);
} else if('nqSpresense' in this){
i2c = new I2C(0);
} else if('nqDiscovery' in this){
i2c = new I2C(2);
} else{
throw("Incompatible device.");
}
i2c.open(400000); // Baudrate: 400000
var hts221 = new HTS221(i2c);
var ready = hts221.init();
var results;
var count = 0;
var sums = [0, 0];
var AVE_NUM = 10;
var MEAS_INTERVAL = 1000; // in milliseconds
if (!ready) {
print("Error: Unable to connect to HTS221");
} else {
var to = setInterval(function () {
var dateTime = new Date();
hts221.measOn(); // Necessary to start a new measurement.
results = hts221.checkStatusAndGetResult(); // Blocking
hts221.measOff();
if(!results) {
print("Error: Failed measurement.");
}
else{
count++;
sums[0] += results.temp;
sums[1] += results.humi;
print(dateTime.toString() + ", Temp: " + results.temp + ", Humi: " + results.humi);
if (count == AVE_NUM) {
print("[Average] Temp: " + (sums[0] / AVE_NUM).toFixed(2) + ", [Average] Humi: " + (sums[1] / AVE_NUM).toFixed(2));
// Finished measuring.
clearInterval(to);
i2c.close();
}
}
}, MEAS_INTERVAL)
}