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

LIS2DW12 v2 Accelerometer

CategoryLibraryLibrary VersionSizeNEQTO BridgeSPRESENSE
I2CLIS2DW12_V22020-12-103.7KBv01.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 LIS2DW12()Creates an LIS2DW12 instance using an activated I2C object.

For information on the STMicroelectronics LIS2DW12 Accelerometer, please refer to the datasheet.

{LIS2DW12} 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.
.readBuff()Reads data of specified length from the specified register of the I2C device.
.init()Initializes the sensor.
.measOn()Powers on the sensor and starts the measurement.
.measOff()Powers off the sensor.
.getFifoNum()Retrieves the number of stored samples in FIFO buffer.
.readFifo()Retrieves the linear acceleration measurement results for the X, Y, and Z axes.
.readFifoMaxAcc()Retrieves the magnitude of the maximum acceleration measurement of the X, Y, and Z axes, and the linear acceleration values for that sample.

Details

new LIS2DW12(i2c)

Creates an LIS2DW12 instance using an activated I2C object.

Instance Methods

.write(reg,data)

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.

.readBuff(reg,len)

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

NameTypeDefaultSummary
regnumberMANDATORYTarget register
Range: 0x00-0xFF
lennumberMANDATORYLength of data to be read (in bytes)
The maximum length that can be specified is 255 bytes.
returnArrayBuffer, undefined-Read data
If the read function fails, undefined will be returned.

.init()

Initializes the sensor.
This method must be called first after the instance is created.

NameTypeDefaultSummary
returnboolean-true: Success
false: Failure

.measOn(odr,mode,bwfilter,fs,fds,lownoise)

Powers on the sensor and starts the measurement.
Various acceleration measurement modes can be selected. For more information, please refer to the sensor datasheet.

NameTypeDefaultSummary
odrnumberMANDATORYOutput data rate
[ value: At High-Performance Mode / At Low-Power Mode ]
1: 12.5 / 1.6 Hz
2: 12.5 / 12.5 Hz
3: 25 / 25 Hz
4: 50 / 50 Hz
5: 100 / 100 Hz
6: 200 / 200 Hz
7: 400 / 200 Hz
modenumberMANDATORYPower mode and resolution
0: High-Performance Mode (14-bit)
1: Low-Power Mode 1 (12-bit)
2: Low-Power Mode 2 (14-bit)
3: Low-Power Mode 3 (14-bit)
4: Low-Power Mode 4 (14-bit)
bwfilternumberMANDATORYFilter bandwidth
0: ODR/2
1: ODR/4
2: ODR/10
3: ODR/20
fsnumberMANDATORYFull-scale
0: ±2g
1: ±4g
2: ±8g
3: ±16g
fdsbooleanMANDATORYFilter type
true: High-pass filter
false: Low-pass filter
lownoisebooleanMANDATORYLow noise operation mode
true: Enabled
false: Disabled
returnboolean-true: Success
false: Failure

.measOff()

Powers off the sensor, reducing power consumption.

NameTypeDefaultSummary
returnundefined--

.getFifoNum()

Retrieves the number of stored samples in FIFO buffer.
The sensor's FIFO buffer can store up to 32 samples of 3-axis acceleration data.
Therefore, if the method returns the number of samples as 32, this may indicate that some samples have been overwritten and the data was lost.
To prevent data loss, optimal output data rate and distributed application processing must be implemented.

NameTypeDefaultSummary
returnnumber-Number of stored samples

.readFifo(num)

Retrieves the linear acceleration measurement results for the X, Y, and Z axes.

NameTypeDefaultSummary
numnumberMANDATORYThe number of samples to be read.
The number of samples that can be specified must be less than or equal to the value obtained by the .getFifoNum() method.
returnarray-Returns a two-dimensional number array of the 3-axis linear acceleration (g) measurements in the [ x[], y[], z[] ] format.
The elements of the array are of type number.
If the reading fails, an empty array [ [], [], [] ] will be returned.

.readFifoMaxAcc(num)

Retrieves the magnitude of the maximum acceleration measurement of the X, Y, and Z axes, and the linear acceleration values for that sample.
Of the num samples read, the sample with the highest magnitude of acceleration is returned.

NameTypeDefaultSummary
numnumberMANDATORYThe number of samples to be read.
The number of samples that can be specified must be less than or equal to the value obtained by the .getFifoNum() method.
returnarray-Returns an array containing the maximum magnitude m of acceleration (g) and the 3-axis linear acceleration (g) measurement results in the [m, x, y, z] format.
The elements of the array are of type number.
If the reading fails, a maximum magnitude of acceleration of -1 will be returned.

Object Usage Examples

Sample 1: Continuous measurement with exponential smoothing

// IMPORTED LIBRARIES
// - LIS2DW12_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 lis2dw12 = new LIS2DW12(i2c);
var ready = lis2dw12.init();

var results;
var ewmaVals = []; // Smoothed results (Exponential Weighted Moving Average)
var prevVals = []; // Stored previous results
var isFirstRun = true;
var alpha = 0.5;

var count = 0;
var maxCount = 100;
var loopFlag = true;

if (!ready) {
    print("Error: Unable to connect to LIS2DW12");
}
else{
    // 100 Hz, High Perf, ODR/2, +-2g, Low pass filter, Low noise enabled
    var ready2 = lis2dw12.measOn(5, 0, 0, 0, false, true);
    if(!ready2) print('measOn unsuccessful');
    else {
        print('measOn successful');
        while(loopFlag){
            var num = lis2dw12.getFifoNum();
            // print('accnum: '+num);
            if(num == 32) print('Warning: Fifo buffer possible overrun!');

            if(num!=0){
                results = lis2dw12.readFifo(num);

                for(var i=0; i<num; i++) {
                    if(isFirstRun){
                       isFirstRun = false;
                       print('|  cnt  |   x  |   y  |   z  |  smx |  smy |  smz |');
                       print(`|   ${count}   | ${results[0][0].toFixed(2)} | ${results[1][0].toFixed(2)} | ${results[2][0].toFixed(2)} |  { } |  { } |  { } |`);
                        count++;
                    } else {
                        for (var j = 0; j < 3; j++) { // 3-Axis X, Y, Z
                            if (ewmaVals.length < 3) { // Initial values for ewmaVals
                                ewmaVals[j] = results[j][0];
                            } else {
                                ewmaVals[j] = (alpha * prevVals[j]) + ((1-alpha) * ewmaVals[j]);
                            }
                            prevVals[j] = results[j][i];
                        }
                        print(`|   ${count}   | ${results[0][i].toFixed(2)} | ${results[1][i].toFixed(2)} | ${results[2][i].toFixed(2)} | ` +
                              `${ewmaVals[0].toFixed(2)} | ${ewmaVals[1].toFixed(2)} | ${ewmaVals[2].toFixed(2)} |`);
                        if(count++ >= maxCount){
                            loopFlag = false;
                            break;
                        }
                    }
                }
            }
        }
    }
    lis2dw12.measOff();
}
i2c.close();

Sample 2: Shock detection

// IMPORTED LIBRARIES
// - LIS2DW12_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 lis2dw12 = new LIS2DW12(i2c);
var ready = lis2dw12.init();
var dateTime;

var results;
var highThreshold = 2.00; // Shock threshold engage
var lowThreshold = 0.50; // Shock threshold release
var detection = false;
var maxShock = [0, 0, 0, 0];
var maxShockTime;

var count = 0;
var maxCount = 100;
var loopFlag = true;

if (!ready) {
    print("Error: Unable to connect to LIS2DW12");
}
else{
    // 400 Hz, High Perf, ODR/2, +-16g, High pass filter, Low noise enabled
    var ready2 = lis2dw12.measOn(7, 0, 0, 3, true, true);
    if(!ready2) print('measOn unsuccessful');
    else {
        print('measOn successful');
        while(loopFlag){
            var num = lis2dw12.getFifoNum();
            // print('accnum: '+num);
            if(num == 32) print('Warning: Fifo buffer possible overrun!');

            if(num!=0){
                results = lis2dw12.readFifoMaxAcc(num);

                if(!detection && results[0] > highThreshold){
                    print("Warning: Shock detected!");
                    detection = true;
                    if(maxShock[0] <= results[0]){
                      maxShock = results.slice();
                      maxShockTime = Date.now();
                    }
                } else if(detection && results[0] < lowThreshold){
                    detection = false;
                    print(`[Max Shock] ${maxShockTime}, Magnitude: ${maxShock[0].toFixed(2)}, X: ${maxShock[1].toFixed(2)}, Y: ${maxShock[2].toFixed(2)}, Z: ${maxShock[3].toFixed(2)}`);
                    // loopFlag = false; // One-shock detection
                    var maxShock = [0, 0, 0, 0]; // Reset
                }
            }
        }
    }
    lis2dw12.measOff();
}
i2c.close();
The company names and product names mentioned above are registered trademarks or trademarks of their respective companies.

Updated: 2021-02-19
← 25. nqExHTS221 v2 Temperature and Humidity Sensor →
  • Abstracts
    • {LIS2DW12} Instance
  • Details
    • new LIS2DW12(i2c)
    • Instance Methods
  • Object Usage Examples
    • Sample 1: Continuous measurement with exponential smoothing
    • Sample 2: Shock detection
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.