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

SRF Ultrasonic Range Finder - I2C Mode

This library is a built-in class that provides a control function for the Devantech SRF series ultrasonic distance sensors in I2C mode.

Library usage requirements
Type I2C
Name SRF_I2C
Version 1_00_00
Code size used 4.1KB
Resources used I2C × 1, Timer × 1

Related Documents

  • SRF02 Ultrasonic Range Finder Technical Specification
  • SRF02 Ultrasonic Range Finder Technical Specification - I2C Mode



Abstracts

Methods()/PropertiesSummaryNote
new SRF_I2C()Creates an SRF_I2C instance.

{SRF_I2C} Instance

Methods()/PropertiesSummaryNote
.write()Writes 1 byte of data to the specified sensor register.
.read()Reads the specified byte length of data from the specified sensor register.
.isReady()Checks if the sensor at the specified slave address is ready.
.scanSlaveAddress()Scans for all slave addresses supported by the sensor.
.init()Initializes the sensor.
.changeSlaveAddress()Rewrites the slave address of the sensor.
.startMeasurement()Starts measurement.
.checkMeasComp()Checks the measurement completion.
.getResult()Retrieves the measurement results.
.startMeasAndGetResult()Starts measurement, waits for measurement completion, and then retrieves the measurement result.
.VERSIONVersion information
maj : {number} Main Version
min : {number} Minor Version
rev : {number} Revision



Details

new SRF_I2C(i2c[,config])

Creates an SRF_I2C instance.

NameTypeM/ODescription                            Note
i2c{I2C}mandatorySpecify an I2C instance that has been opened in advance.
configObjectoptionalConfigurations
For details, refer to config.
return{SRF_I2C}-{SRF_I2C} : Generated {SRF_I2C}When an error occurs, an exception is raised.

config

NameTypeM/ODescriptionNote
slaveAddressnumberoptionalTarget slave address (7 bits)
Range: 0x70 - 0x7F
The default value is 0x70.
rangingModenumberoptionalRanging mode
Specify the output unit of the measurement result.
0x50: In inches
0x51: In centimeters
0x52: In micro-seconds
The default value is 0x51.

.write(reg,val)

Writes 1 byte of data to the specified sensor register.
This method is used to access the sensor directly.

NameTypeM/ODescriptionNote
regnumbermandatoryTarget register
Range: 0x00 - 0xFF
valnumbermandatoryWrite data
Range: 0x00 - 0xFF
returnboolean-true: Success
false: Failure

.read(reg,len)

Reads the specified byte length of data from the specified sensor register.
This method is used to access the sensor directly.

NameTypeM/ODescriptionNote
regnumbermandatoryTarget register
Range: 0x00 - 0xFF
lennumbermandatorySpecify the read byte length.
Range: 1- 255
returnArrayBuffer-Read data
If reading fails, the returned size will be 0.

.isReady([slaveAddress])

Checks if the sensor at the specified slave address is ready.
If the sensor at the specified slave address does not exist or if the sensor is busy measuring, 'Not ready' will be returned.

NameTypeM/ODescriptionNote          
slaveAddressnumberoptionalSlave address (7 bits)
Range: 0x70 - 0x7F
If omitted, it will be the target slave address that is currently set.
returnboolean-true: Ready
false: Not ready
If a parameter is invalid, an exception is raised.

.scanSlaveAddress()

Scans for all slave addresses supported by the sensor.
Performs a status check with .isReady() for each slave address.

NameTypeM/ODescriptionNote
returnArray-Scan list
The list of slave addresses for which a ready status is detected is returned as an array type. The array elements contain slave address values of type number. In case of no detection, the number of elements in the array will be 0.

.init([slaveAddress[,statusCheck]])

Initializes the sensor.
Performs a status check with .isReady() on the sensor at the target slave address.

Specify the "switching destination slave address" to be used for dynamically switching between multiple sensors with different slave addresses on the same bus. In essence, switching is necessary when the sensor at the currently selected target slave address has completed measurement.
In the default operation, the status check by .isReady() is performed for the sensor after switching. However, if the status of each sensor is always monitored, it is not necessary to check the status at each switching. In this case, it is possible to skip the status check process by setting statusCheck to false.

Note that there must not be multiple sensors with the same slave address on the same bus.

NameTypeM/ODescription                    Note
slaveAddressnumberoptionalSwitching destination slave address (7bits)
Range: 0x70 - 0x7F
If omitted, initialization is performed for the sensor with the currently set target slave address.
statusCheckbooleanoptionalStatus check after sensor switching
true: Enabled
false: Disabled
The default value is true.
This parameter is valid only when " Switching destination slave address" is specified.
returnboolean-true: Success
false: Failure
If a parameter is invalid, an exception is raised.

.changeSlaveAddress(slaveAddress)

Rewrites the slave address of the sensor.
Before executing this method, the slave address of the sensor to be rewritten must be selected by .init() and become ready.
Issuing a rewrite of the slave address to the sensor will rewrite the ROM in the sensor and reboot the sensor. Afterwards, the slave address of the sensor becomes the new slave address.
This method then switches the target slave address to the new slave address, executes .init(), and then returns the result. In doing so, it is possible to start measurement immediately after this method.

Rewriting the slave address involves rewriting the ROM in the sensor. To avoid unexpected failures, frequent use of this method should be avoided.

NameTypeM/ODescription                    Note
slaveAddressnumbermandatorySpecify the new slave address (7 bits).
Range: 0x70 - 0x7F
returnboolean-true: Success
false: Failure
If a parameter is invalid, an exception is raised.

.startMeasurement()

Starts measurement.
After executing this method, it is necessary to wait for the measurements to complete with .checkMeasComp().
After the measurement is completed, it is necessary to call this method again to start a new measurement.

NameTypeM/ODescriptionNote
returnboolean-true: Success
false: Failure

.checkMeasComp()

Checks the measurement completion.

NameTypeM/ODescriptionNote
returnboolean-true: Measurement completed
false: Measurement in progress

.getResult()

Retrieves the measurement results.
It is necessary to wait for measurement completion by using .checkMeasComp() in advance.

NameTypeM/ODescriptionNote
returnnumber-Measurement results
The measurement result will be output in the unit specified in the ranging mode. If the distance could not be measured, the value will be 0. If a sensor error occurred, the value will be -1.

.startMeasAndGetResult()

Starts measurement, waits for measurement completion, and then retrieves the measurement result.

NameTypeM/ODescriptionNote
returnnumber-Measurement results
The measurement result will be output in the unit specified in the ranging mode. If the distance could not be measured, the value will be 0. If a sensor error occurred, the value will be -1.



Usage Examples

Sample 1

This is a sample that measures distance at periodic intervals.

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

if(!('SRF_I2C' in this)) { throw new Error('Please import the SRF_I2C library.'); }

//Specify the I2C connection settings for the SRFxx ultrasonic range finder.
var i2c = new I2C(1); //I2C node number
var ready = i2c.open(400000); //I2C baudrate
if(!ready) {
    throw new Error('I2C open failed. Check the I2C node number.');
}
//var srf = new SRF_I2C(i2c, { slaveAddress: 0x70 });
var srf = new SRF_I2C(i2c);
ready = srf.init();
if(!ready) {
    print('srf init failed');
    while(1);
}

var actFlag = true;

var measFlag = false;
var MEAS_INTERVAL = 1000; //ms
var tout = setInterval(function() { measFlag = true; }, MEAS_INTERVAL);

while(actFlag) {
    if(measFlag) {
        var utime = Date.now();
        var dist = srf.startMeasAndGetResult();
        print(utime + ': ' + dist);
        measFlag = false;
    }
}

clearInterval(tout);
i2c.close();

Sample 2

This is a sample that measures the distance while switching to multiple sensors with different slave addresses in order.
First, .scanSlaveAddress() is used to find all the sensors that exist on the bus.

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

if(!('SRF_I2C' in this)) { throw new Error('Please import the SRF_I2C library.'); }

var toHexStr = function(val) { return '0x' + ('00' + val.toString(16).toUpperCase()).substr(-2); };

//Specify the I2C connection settings for the SRFxx ultrasonic range finder.
var i2c = new I2C(1); //I2C node number
var ready = i2c.open(400000); //I2C baudrate
if(!ready) {
    throw new Error('I2C open failed. Check the I2C node number.');
}
var srf = new SRF_I2C(i2c);

var list = srf.scanSlaveAddress();
if(!list.length) {
    print('srf not found');
    while(1);
}
print('List[' + list.length + ']: ' + list);

var actFlag = true;
var index = 0;

var measFlag = false;
var MEAS_INTERVAL = 1000; //ms
var tout = setInterval(function() { measFlag = true; }, MEAS_INTERVAL);

while(actFlag) {
    if(measFlag) {
        var utime = Date.now();
        var addr = list[index];
        srf.init(addr, false); //Skip status check
        var dist = srf.startMeasAndGetResult();
        print(utime + ': [' + toHexStr(addr) + '] '+ dist);
        if(++index >= list.length) index = 0;
        measFlag = false;
    }
}

clearInterval(tout);
i2c.close();

Sample 3

This is a sample that rewrites the slave address of the target sensor to the new slave address.
After the slave address is rewritten, it will proceed to measure distance.

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

if(!('SRF_I2C' in this)) { throw new Error('Please import the SRF_I2C library.'); }

var TARGET_ADDRESS = 0x70;
var NEW_ADDRESS = 0x71;

var toHexStr = function(val) { return '0x' + ('00' + val.toString(16).toUpperCase()).substr(-2); };

//Specify the I2C connection settings for the SRFxx ultrasonic range finder.
var i2c = new I2C(1); //I2C node number
var ready = i2c.open(400000); //I2C baudrate
if(!ready) {
    throw new Error('I2C open failed. Check the I2C node number.');
}
var srf = new SRF_I2C(i2c);
ready = srf.init(TARGET_ADDRESS);
if(!ready) {
    print('target srf not found');
    while(1);
}

ready = srf.changeSlaveAddress(NEW_ADDRESS);
if(!ready) {
    print('new address srf not found');
    while(1);
}

print('Address change successful! [' + toHexStr(TARGET_ADDRESS) + ' to ' + toHexStr(NEW_ADDRESS) + ']');

var actFlag = true;

var measFlag = false;
var MEAS_INTERVAL = 1000; //ms
var tout = setInterval(function() { measFlag = true; }, MEAS_INTERVAL);

while(actFlag) {
    if(measFlag) {
        var utime = Date.now();
        var dist = srf.startMeasAndGetResult();
        print(utime + ': ' + dist);
        measFlag = false;
    }
}

clearInterval(tout);
i2c.close();


The company names and product names mentioned above are registered trademarks or trademarks of their respective companies.

Updated: 2022-04-14
← LIS2DW12 v2 Accelerometer[Archive] HTS221 Temperature and Humidity Sensor →
  • Abstracts
    • {SRF_I2C} Instance
  • Details
    • new SRF_I2C(i2c[,config])
    • .write(reg,val)
    • .read(reg,len)
    • .isReady([slaveAddress])
    • .scanSlaveAddress()
    • .init([slaveAddress[,statusCheck]])
    • .changeSlaveAddress(slaveAddress)
    • .startMeasurement()
    • .checkMeasComp()
    • .getResult()
    • .startMeasAndGetResult()
  • Usage Examples
    • Sample 1
    • Sample 2
    • Sample 3
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.