NEQTO Docs
  • Languages iconEnglish
    • 日本語
  • Docs
  • Region API
  • Global API
  • FAQ

›I2C

Getting Started

  • NEQTO Hello World!
  • Step 1. Console Settings
  • Step 2. Device Setting & Start Service

    • When using NEQTO Bridge
    • When using Spresense
  • Step 3. Using Sensors

NEQTO

  • NEQTO Account Registration
  • API Usage
  • Batch Registration
  • Support Guidelines

NEQTO Console

  • Introduction
  • Fundamentals
  • Administrative Actions
  • Device Management
  • Scripts
  • Actions and Contacts
  • NEQTO Apps
  • Machine Driver
  • Recommended Browsers
  • Billing Information

SPRESENSE

    Hardware Specifications

    • 01. About Spresense

    Software Specifications

    • 01. Operational Flow
    • 02. Initial Installation
    • 03. Spresense Wi-Fi Initial Setup
    • 05. Debug Log Acquisition
    • 06. System LED Indications
    • 07. Event Messages
    • 08. Updating Firmware

    neqto.js

    • 01. About neqto.js
    • 02. Log
    • 03. Timers
    • 04. HTTP
    • 05. HTTPS
    • 06. MQTT
    • 07. Secure
    • 08. Storage
    • 10. RTC
    • 12. GPIO
    • 13. UART
    • 15. I2C
    • 17. Camera
    • 18. nqSpresense
    • 19. nqService
    • 20. nqMqtt
    • 21. nqFOTA
    • 22. nqWiFi

NEQTO Bridge Series

    Hardware Specifications

    • 01. NEQTO Bridge Module
    • 02. NEQTO Bridge Wi-Fi Module
    • 03. NEQTO Bridge LTE-1 Module
    • 04. NEQTO Bridge LTE-M/NB Module
    • 05. NEQTO Bridge IO Board
    • 06. NEQTO Bridge Digital IO Board

    Software Specifications

    • 01. Operational Flow
    • 02. NEQTO Bridge Wi-Fi Module Initial Setup
    • 03. NEQTO Bridge LTE Module Initial Setup
    • 04. Debug Log Acquisition
    • 05. System LED Indications
    • 06. Event Messages
    • 07. Updating Firmware

    neqto.js

    • 01. About neqto.js
    • 02. Log
    • 03. Timers
    • 04. HTTP
    • 05. HTTPS
    • 06. MQTT
    • 07. Secure
    • 08. Storage
    • 09. Sleep
    • 10. RTC
    • 11. UserSW
    • 12. GPIO
    • 13. UART
    • 14. SPI
    • 15. I2C
    • 16. ADC
    • 17. BLE
    • 18. nqBridge
    • 19. nqService
    • 20. nqMqtt
    • 21. nqFOTA
    • 22. nqWiFi
    • 23. nqLte
    • 24. nqLAN
    • 25. nqEx

neqto.js Libraries

    I2C

    • LIS2DW12 v2 Accelerometer
    • HTS221 v2 Temperature and Humidity Sensor
    • [Archive] LIS2DW12 Accelerometer
    • [Archive] HTS221 Temperature and Humidity Sensor

    Integration

    • AWS IoT Core v2 Library
    • AWS S3 v2 Library
    • Azure IoT v2 Library
    • GCP IoT Core Library
    • [Archive] AWS S3 Library
    • [Archive] AWS IoT Core Library

neqto.js Snippets

  • DataDog Snippet
  • Dropbox Snippet
  • Google Sheets Snippet
  • InfluxDB Snippet
  • Oracle Cloud Object Storage Snippet
  • Salesforce Snippet
  • SAP Cloud Platform Internet of Things Snippet
  • Splunk Snippet
  • Niagara Snippet

Release Notes

  • NEQTO Console Updates
  • NEQTO Firmware (Bridge Wi-Fi/LTE Module) Releases
  • NEQTO Firmware (Spresense Wi-Fi) Releases

HTS221 v2 Temperature and Humidity Sensor

CategoryLibraryLibrary VersionSizeNEQTO BridgeSPRESENSE
I2CHTS221_V22020-11-043.4KBv01.00.00+v01.00.00+

For information on how to import neqto.js libraries, please refer to this documentation.

This library uses the following resources:

  • I2C x 1
  • Timers x 1

Abstracts

Methods()/PropertiesSummary
new HTS221()Creates an HTS221 instance.

For information on the STMicroelectronics HTS221 Temperature and Humidity sensor, please refer to the datasheet.

{HTS221} Instance

Methods()/PropertiesSummary
.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.

NameTypeDefaultSummary
regnumberMANDATORYTarget register
Range: 0x00-0xFF
datanumberMANDATORYWrite data
Range: 0x00-0xFF
returnboolean-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.

NameTypeDefaultSummary
regnumberMANDATORYTarget register
Range: 0x00-0xFF
returnnumber, 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.

NameTypeDefaultSummary
avgTnumber7Number of averaged temperature samples
Range: 0 - 7
avgHnumber7Number of averaged humidity samples
Range: 0 - 7
returnboolean-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.

NameTypeDefaultSummary
returnundefined-

.measOff()

Powers off the sensor, reducing power consumption.

NameTypeDefaultSummary
returnundefined-

.checkStatus()

Checks the measurement status of the sensor.

NameTypeDefaultSummary
returnboolean-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.

NameTypeDefaultSummary
returnobject, undefined-Returns an object containing temperature (°C) and humidity (%RH) measurements in the {"temp": XX.X, "humi": XX.X} format. The object's values are of type number.
If the measurement fails, undefined will be returned.

.checkStatusAndGetResult()

Waits for the sensor measurements to complete, then retrieves the temperature and humidity measurement results.

NameTypeDefaultSummary
returnobject, undefined-Returns an object containing temperature (°C) and humidity (%RH) measurements in the {"temp": XX.X, "humi": XX.X} format. The object's values are of type number.
If the measurement fails, undefined will be returned.

Object 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{
    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{
    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)
}
The company names and product names mentioned above are registered trademarks or trademarks of their respective companies.

Updated: 2021-02-19
← LIS2DW12 v2 Accelerometer[Archive] LIS2DW12 Accelerometer →
  • Abstracts
    • {HTS221} Instance
  • Details
    • new HTS221(i2c)
    • Instance Methods
  • Object Usage Examples
    • Sample 1: Single measurement
    • Sample 2: Interval-based measurements and then calculate average
AboutNewsProductsFAQPrivacy Policy}
NEQTO Console
IntroductionFundamentalsAdministrative ActionsDevice Management NEQTO Apps
NEQTO Bridge Series
NEQTO Bridge ModuleNEQTO Bridge Wi-Fi ModuleNEQTO Bridge LTE-1 ModuleError Logging Event Messages
API Documentation
API UsageGlobal APIRegional APIAPI Terms of Service
Jigsaw, Inc.
© 2021 JIG-SAW INC.