LIS2DH12 linear accelerometer Micro:bit example

In this article we connect a LIS2DH12 to a Micro:bit

The LIS2DH12 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 LIS2DH12 has user-selectable full scales of ±2g/±4g/±8g/±16g and 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 functionality of the sensor in the final application.

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

The LIS2DH12 is available in a 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 IO supply (1.8 V) and supply voltage compatible
  • Ultra-low power consumption down to 2 μA
  • ±2g/±4g/±8g/±16g selectable full scales
  • I2C/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” functions
  • Free-fall detection
  • Motion detection
  • Embedded temperature sensor

Parts List

Name Link
Micro:bit Micro:bit Development Board
expansion board Edge Breakout I/O Expansion Extension Board for BBC micro:bit
LIS2DH12 sensor LIS2DH12TR CJMCU- three axis accelerometer module sensor development board
connecting wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

 

Layout

 

LIS2DH Micro:bit
VCC 3V3
GND GND
SDA 20(SDA)
SCL 19(SCL)

Code

I used the following library – Arduino LIS2DH Library

The first thing I observed was that nothing appeared in the Serial monitor, a quick run of an I2C scanner test revealed that the I2C address was 0x19, the library above has this set to 0x18. So I located the library and changed the line in DFRobot_LIS2DH12.cpp

Line17 : uint8_t DFRobot_LIS2DH12::sensorAddress = 0x19; // 0x18 is the default

 

Now for the code example – which is the default and works just fine

[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 and you should see something like this – move the sensor around

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

Links

https://www.st.com/resource/en/datasheet/lis2dh12.pdf

Leave a Comment

This div height required for enabling the sticky sidebar
Ad Clicks : Ad Views : Ad Clicks : Ad Views :