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()/Properties | Summary |
---|---|
new LIS2DW12() | Creates an LIS2DW12 instance using an activated I2C object. |
{LIS2DW12} Instance
Methods()/Properties | Summary |
---|---|
.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.
Name | Type | Default | Summary |
---|---|---|---|
reg | number | MANDATORY | Target register Range: 0x00-0xFF |
data | number | MANDATORY | Write data Range: 0x00-0xFF |
return | boolean | - | 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.
Name | Type | Default | Summary |
---|---|---|---|
reg | number | MANDATORY | Target register Range: 0x00-0xFF |
return | number, 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.
Name | Type | Default | Summary |
---|---|---|---|
reg | number | MANDATORY | Target register Range: 0x00-0xFF |
len | number | MANDATORY | Length of data to be read (in bytes) The maximum length that can be specified is 255 bytes. |
return | ArrayBuffer, 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.
Name | Type | Default | Summary |
---|---|---|---|
return | boolean | - | 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.
Name | Type | Default | Summary |
---|---|---|---|
odr | number | MANDATORY | Output 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 |
mode | number | MANDATORY | Power 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) |
bwfilter | number | MANDATORY | Filter bandwidth 0: ODR/2 1: ODR/4 2: ODR/10 3: ODR/20 |
fs | number | MANDATORY | Full-scale 0: ±2g 1: ±4g 2: ±8g 3: ±16g |
fds | boolean | MANDATORY | Filter type true: High-pass filter false: Low-pass filter |
lownoise | boolean | MANDATORY | Low noise operation mode true: Enabled false: Disabled |
return | boolean | - | true: Success false: Failure |
.measOff()
Powers off the sensor, reducing power consumption.
Name | Type | Default | Summary |
---|---|---|---|
return | undefined | - | - |
.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.
Name | Type | Default | Summary |
---|---|---|---|
return | number | - | Number of stored samples |
.readFifo(num)
Retrieves the linear acceleration measurement results for the X, Y, and Z axes.
Name | Type | Default | Summary |
---|---|---|---|
num | number | MANDATORY | The 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. |
return | array | - | 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.
Name | Type | Default | Summary |
---|---|---|---|
num | number | MANDATORY | The 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. |
return | array | - | 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();