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

›I2C

Getting Started

  • NEQTO Hello World!
  • Tutorial Step 1. NEQTO Console Setting
  • Tutorial Step 2. Device Settings & Launching Service (NEQTO Bridge)
  • Tutorial Step 2. Device Settings & Launching Service (STM32 Discovery)
  • Tutorial Step 2. Device Settings & Launching Service (SPRESENSE)
  • Tutorial Step 3. Application development using scripts

NEQTO

  • NEQTO Account Registration
  • Sub-accounts
  • API Usage
  • NEQTO Engine Firmware List
  • Support Guidelines
  • For safe and secure use of the NEQTO products
  • Vulnerability Disclosure Policy

NEQTO Console

  • Introduction
  • Fundamentals
  • Administrative Actions
  • Device Management
  • Linux-based Device Management
  • Batch Registration
  • Scripts
  • Actions and Contacts
  • View Data from the Console
  • NEQTO Apps

    • About NEQTO Apps
    • NEQTO Infinitypool
    • NEQTO Insights
    • NEQTO Custodia
    • NEQTO Flow
  • 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
    • 04. Spresense LTE-M 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
    • 09. Sleep
    • 10. RTC
    • 11. GPIO
    • 12. UART
    • 13. SPI
    • 14. I2C
    • 15. ADC
    • 16. GNSS
    • 17. Camera
    • 18. Utils
    • 19. nqSpresense
    • 20. nqService
    • 21. nqMqtt
    • 22. nqFOTA
    • 23. nqWiFi
    • 24. nqLte

STM32 Discovery

    Hardware Specifications

    • 01. About STM32 Discovery Kit (B-L4S5I-IOT01A)

    Software Specifications

    • 01. Operational Flow
    • 02. Initial Installation
    • 03. STM32 Discovery Wi-Fi 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
    • 18. Utils
    • 19. nqDiscovery
    • 20. nqService
    • 21. nqMqtt
    • 22. nqFOTA
    • 23. nqWiFi

NEQTO Bridge

    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
    • 07. NEQTO Bridge Connector 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. Utils
    • 19. nqBridge
    • 20. nqService
    • 21. nqMqtt
    • 22. nqFOTA
    • 23. nqWiFi
    • 24. nqLte
    • 25. nqLAN
    • 26. nqEx

Linux-based device

    Software Specifications

    • 01. System Requirements
    • 02. Installation
    • 03. Software Configurations
    • 04. Operational Flow
    • 05. Debug Log Acquisition
    • 06. Event Messages
    • 07. Updating Software

    neqto.js

    • 01. About neqto.js
    • 02. Log
    • 03. Timers
    • 04. HTTP
    • 05. HTTPS
    • 06. MQTT
    • 07. Secure
    • 08. Storage
    • 09. RTC
    • 10. UNIXSocket
    • 11. FileSystem
    • 12. SubProcess
    • 13. SubTask
    • 14. Queue
    • 15. Utils
    • 16. nqLinux
    • 17. nqService
    • 18. nqMqtt
    • 19. nqFOTA
    • 20. nqLAN

neqto.js Libraries

  • About neqto.js Libraries
  • UART

    • GM65 Barcode Reader
    • SRF Ultrasonic Range Finder - Serial Mode

    I2C

    • HTS221 v2 Temperature and Humidity Sensor
    • LIS2DW12 v2 Accelerometer
    • SRF Ultrasonic Range Finder - I2C Mode
    • [Archive] HTS221 Temperature and Humidity Sensor
    • [Archive] LIS2DW12 Accelerometer

    Utils

    • RTC Alarm Synchronized Scheduler

    Integration

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

neqto.js Snippets

  • About neqto.js Snippets
  • DataDog
  • Dropbox
  • Google Sheets
  • InfluxDB
  • Oracle Cloud Object Storage
  • Salesforce
  • Splunk
  • Niagara
  • [Archive] SAP Cloud Platform Internet of Things

Release Notes

  • NEQTO Console Updates
  • NEQTO Firmware (Bridge Wi-Fi/LTE Module) Releases
  • NEQTO Firmware (STM32 Discovery Wi-Fi) Releases
  • NEQTO Firmware (Spresense Wi-Fi/LTE-M) Releases
  • NEQTO Engine for Linux Releases
  • neqto.js Libraries Releases

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()/PropertiesSummary
new HTS221()Creates an HTS221 instance.

{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-The measurement results are returned as {Results} objects.
If the measurement fails, undefined will be returned.

{Results}

NameTypeDescriptionNote
.tempnumberTemperature (°C)
.huminumberHumidity (%RH)

.checkStatusAndGetResult()

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

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

Updated: 2021-10-06
← SRF Ultrasonic Range Finder - Serial ModeLIS2DW12 v2 Accelerometer →
  • Abstracts
    • {HTS221} Instance
  • Details
    • new HTS221(i2c)
    • Instance Methods
  • Usage Examples
    • Sample 1: Single measurement
    • Sample 2: Interval-based measurements and then calculate average
AboutNewsProductsFAQPrivacy PolicyVulnerability Disclosure Policy
NEQTO Console
IntroductionFundamentalsAdministrative ActionsDevice Management NEQTO Apps
NEQTO Bridge
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.
© 2025 JIG-SAW INC.