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 CloudSync for GCP
  • NEQTO Engine Firmware List
  • Support Guidelines

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. Utils
    • 14. nqLinux
    • 15. nqService
    • 16. nqMqtt
    • 17. nqFOTA
    • 18. 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
    • 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
  • SAP Cloud Platform Internet of Things
  • Splunk
  • Niagara

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

LIS2DW12 v2 Accelerometer

This library is a built-in class that provides functions to control the STMicroelectronics LIS2DW12 Accelerometer.

Library usage requirements
Type I2C
Name LIS2DW12_V2
Version 2020-12-10
Code size used 3.7KB
Resources used I2C x 1, Timers x 1

Related Documents

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



Abstracts

Methods()/PropertiesSummary
new LIS2DW12()Creates an LIS2DW12 instance using an activated I2C object.

{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.

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 if('nqDiscovery' in this){
    i2c = new I2C(1);
} 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 if('nqDiscovery' in this){
    i2c = new I2C(1);
} 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-10-06
← HTS221 v2 Temperature and Humidity SensorSRF Ultrasonic Range Finder - I2C Mode →
  • Abstracts
    • {LIS2DW12} Instance
  • Details
    • new LIS2DW12(i2c)
    • Instance Methods
  • Usage Examples
    • Sample 1: Continuous measurement with exponential smoothing
    • Sample 2: Shock detection
AboutNewsProductsFAQPrivacy 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.
© 2023 JIG-SAW INC.