Wemos and LIS2DH three-axis linear accelerometer example

The LIS2DH is an ultra low-power high performance three-axis linear accelerometer belonging to the “femto” family, with digital I2C/SPI serial interface standard output.

The LIS2DH has dynamically user selectable full scales of ±2g/±4g/±8g/±16g and it is capable of measuring accelerations with output data rates from 1 Hz to 5.3 kHz.

The self-test capability allows the user to check the functioning of the sensor in the final application.

The device may be configured to generate interrupt signals by two independent inertial wake-up/free-fall events as well as by the position of the device itself.

The LIS2DH is available in small thin plastic land grid array package (LGA) and is guaranteed to operate over an extended temperature range from -40 °C to +85 °C.

 

 

Key Features

Wide supply voltage, 1.71 V to 3.6 V
Independent IOs supply (1.8 V) and supply voltage compatible
Ultra low-power mode consumptiondown to 2 µA
±2g/±4g/±8g/±16g dynamically selectable full-scale
I2 C/SPI digital output interface
2 independent programmable interrupt generators for free-fall and motion detection
6D/4D orientation detection
“Sleep to wake” and “return to sleep” function
Freefall detection
Motion detection
Embedded temperature sensor
Embedded FIFO

Here is a schematic of the module

 

Connection

 

LIS2DH Wemos
VCC 3V3
GND GND
SDA D2(SDA)
SCL D1(SCL)

 

Code

This uses https://github.com/DFRobot/DFRobot_LIS2DH12/archive/master.zip , I had to change the I2C address for my board

I had to change the I2C address in the library, the default is 0x18 – the file was DFRobot_LIS2DH12.cpp

uint8_t DFRobot_LIS2DH12::sensorAddress = 0x19; /

 

[codesyntax lang=”cpp”]

#include <Wire.h>
#include <DFRobot_LIS2DH12.h>


DFRobot_LIS2DH12 LIS; //Accelerometer

void setup(){
Wire.begin();
Serial.begin(115200);
while(!Serial);
delay(100);

// Set measurement range
// Ga: LIS2DH12_RANGE_2GA
// Ga: LIS2DH12_RANGE_4GA
// Ga: LIS2DH12_RANGE_8GA
// Ga: LIS2DH12_RANGE_16GA
while(LIS.init(LIS2DH12_RANGE_16GA) == -1){ //Equipment connection exception or I2C address error
Serial.println("No I2C devices found");
delay(1000);
}
}

void loop(){
acceleration();
}

/*!
* @brief Print the position result.
*/
void acceleration(void)
{
int16_t x, y, z;

delay(100);
LIS.readXYZ(x, y, z);
LIS.mgScale(x, y, z);
Serial.print("Acceleration x: "); //print acceleration
Serial.print(x);
Serial.print(" mg \ty: ");
Serial.print(y);
Serial.print(" mg \tz: ");
Serial.print(z);
Serial.println(" mg");
}

[/codesyntax]

 

 

Output

Open the serial monitor

Acceleration x: 0 mg y: -250 mg z: -375 mg
Acceleration x: 0 mg y: -625 mg z: -375 mg
Acceleration x: -125 mg y: -375 mg z: -500 mg
Acceleration x: -125 mg y: -500 mg z: -375 mg
Acceleration x: -125 mg y: -500 mg z: -375 mg
Acceleration x: 125 mg y: -375 mg z: -375 mg
Acceleration x: 0 mg y: -625 mg z: -375 mg
Acceleration x: 0 mg y: -375 mg z: -625 mg
Acceleration x: 0 mg y: -625 mg z: -375 mg
Acceleration x: -125 mg y: -500 mg z: -500 mg

 

Link

http://www.st.com/resource/en/datasheet/lis2dh.pdf