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

›UART

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 - Serial Mode

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

Library usage requirements
Type UART
Name SRF_SERIAL
Version 1_00_00
Code size used 3.9KB
Resources used UART × 1, Timer × 1

Related Documents

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



Abstracts

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

{SRF_SERIAL} Instance

Methods()/PropertiesSummaryNote
.write()Executes the write command.
.read()Executes the read command.
.isReady()Checks if the sensor at the specified device address is ready.
.scanAddress()Scans for all device addresses supported by the sensor.
.init()Initializes the sensor.
.changeAddress()Rewrites the device 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_SERIAL(uart[,config])

Creates an SRF_SERIAL instance.

NameTypeM/ODescriptionNote                                  
uart{UART}mandatorySpecify a UART instance that has been opened in advance.
The UART settings differ depending on the model number of the sensor. Confirm the technical specifications of the sensor to be used.
For using SRF02, the following settings are used.
Baudrate: 9600bps
Hardware flow: Invalid
Parity bit: None
Data bit: 8-bit
Stop bit: 2-bit
configObjectoptionalConfigurations
For details, refer to config.
return{SRF_SERIAL}-{SRF_SERIAL} : Generated {SRF_SERIAL}When an error occurs, an exception is raised.

config

NameTypeM/ODescriptionNote
addressnumberoptionalTarget device address
Range: 0x00 - 0x0F
The default value is 0x00.
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(cmd)

Executes the write command.
This method is used to access the sensor directly.

NameTypeM/ODescriptionNote
cmdnumbermandatoryWrite command
Range: 0x00 - 0xFF
returnboolean-true: Success
false: Failure

.read(cmd,len)

Executes the read command.
This method is used to access the sensor directly.

NameTypeM/ODescriptionNote
cmdnumbermandatoryRead command
Range: 0x00 - 0xFF
lennumbermandatorySpecify the byte length of the command response data.
Range: 1 - 512
returnArrayBuffer-Command response data
If the command fails, the returned size will be 0.

.isReady([address])

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

NameTypeM/ODescriptionNote          
addressnumberoptionalDevice Address
Range: 0x00 - 0x0F
If omitted, it will be the target device address that is currently set.
returnboolean-true: Ready
false: Not ready
If a parameter is invalid, an exception is raised.

.scanAddress()

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

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

.init([address[,statusCheck]])

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

Specify the "switching destination device address" to be used for dynamically switching between multiple sensors with different device addresses on the same bus. In essence, switching is necessary when the sensor at the currently selected target device 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 device address on the same bus.

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

.changeAddress(address)

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

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

NameTypeM/ODescription                    Note
addressnumbermandatorySpecify the new device address.
Range: 0x00 - 0x0F
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_SERIAL' in this)) { throw new Error('Please import the SRF_SERIAL library.'); }

//Specify the UART connection settings for the SRFxx ultrasonic range finder.
var uart = new UART(1); //UART node number
var ready = uart.open(9600,false,0,0,1); //UART settings for SRF02
if(!ready) {
    throw new Error('UART open failed. Check the UART node number.');
}
//var srf = new SRF_SERIAL(uart, { address: 0x00 });
var srf = new SRF_SERIAL(uart);
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);
uart.close();

Sample 2

This is a sample that measures the distance while switching to multiple sensors with different device addresses in order.
First, .scanAddress() 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_SERIAL' in this)) { throw new Error('Please import the SRF_SERIAL library.'); }

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

//Specify the UART connection settings for the SRFxx ultrasonic range finder.
var uart = new UART(1); //UART node number
var ready = uart.open(9600,false,0,0,1); //UART settings for SRF02
if(!ready) {
    throw new Error('UART open failed. Check the UART node number.');
}
var srf = new SRF_SERIAL(uart);

var list = srf.scanAddress();
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);
uart.close();

Sample 3

This is a sample that rewrites the device address of the target sensor to the new device address.
After the device 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_SERIAL' in this)) { throw new Error('Please import the SRF_SERIAL library.'); }

var TARGET_ADDRESS = 0x00;
var NEW_ADDRESS = 0x01;

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

//Specify the UART connection settings for the SRFxx ultrasonic range finder.
var uart = new UART(1); //UART node number
var ready = uart.open(9600,false,0,0,1); //UART settings for SRF02
if(!ready) {
    throw new Error('UART open failed. Check the UART node number.');
}
var srf = new SRF_SERIAL(uart);
ready = srf.init(TARGET_ADDRESS);
if(!ready) {
    print('target srf not found');
    while(1);
}

ready = srf.changeAddress(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);
uart.close();


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

Updated: 2022-04-14
← GM65 Barcode ReaderHTS221 v2 Temperature and Humidity Sensor →
  • Abstracts
    • {SRF_SERIAL} Instance
  • Details
    • new SRF_SERIAL(uart[,config])
    • .write(cmd)
    • .read(cmd,len)
    • .isReady([address])
    • .scanAddress()
    • .init([address[,statusCheck]])
    • .changeAddress(address)
    • .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.