Arduino library for interfacing with the STMicroelectronics LIS2DH12 ultra-low-power high-performance three-axis linear accelerometer.
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 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. This library provides a simple interface to access these features via I2C or SPI.
Install via the Arduino Library Manager by searching for "Sitron Labs LIS2DH12".
Alternatively, install manually:
- Download or clone this repository
- Place it in your Arduino
librariesfolder - Restart the Arduino IDE
Install via the PlatformIO Library Manager by searching for "Sitron Labs LIS2DH12".
Alternatively, add the library manually to your platformio.ini file:
lib_deps =
https://github.com/sitronlabs/SitronLabs_STMicroelectronics_LIS2DH12_Arduino_Library.gitConnect the LIS2DH12 to your Arduino using I2C:
- VCC → 3.3V (check your board's specifications)
- GND → GND
- SDA → SDA (I2C Data)
- SCL → SCL (I2C Clock)
- SA0 → GND or VCC (I2C address selection)
The I2C address is determined by the SA0 pin:
- SA0 LOW → 0x18 (default)
- SA0 HIGH → 0x19
Connect the LIS2DH12 to your Arduino using SPI:
- VCC → 3.3V (check your board's specifications)
- GND → GND
- CS → Any digital pin (Chip Select)
- SCL/SCLK → SCK (SPI Clock)
- SDA/SDI → MOSI (SPI Master Out Slave In)
- SDO → MISO (SPI Master In Slave Out)
#include <Wire.h>
#include <lis2dh12.h>
// Create device object
lis2dh12 sensor;
// I2C address (0x18 when SA0 is LOW, 0x19 when SA0 is HIGH)
const uint8_t I2C_ADDRESS = 0x18;
void setup() {
Serial.begin(9600);
// Initialize I2C
Wire.begin();
// Setup the LIS2DH12 (I2C library, I2C address)
if (sensor.setup(Wire, I2C_ADDRESS) != 0) {
Serial.println("Failed to setup LIS2DH12");
return;
}
// Detect the device
if (!sensor.detect()) {
Serial.println("LIS2DH12 not detected");
return;
}
// Configure sensor settings
sensor.range_set(LIS2DH12_RANGE_2G);
sensor.resolution_set(LIS2DH12_RESOLUTION_12BITS);
sensor.sampling_set(LIS2DH12_SAMPLING_100HZ);
sensor.axis_enabled_set(true, true, true); // Enable X, Y, Z axes
Serial.println("LIS2DH12 initialized");
}
void loop() {
// Read acceleration values
float accel_x, accel_y, accel_z;
if (sensor.acceleration_read(accel_x, accel_y, accel_z) == 0) {
Serial.print("X: ");
Serial.print(accel_x);
Serial.print(" g, Y: ");
Serial.print(accel_y);
Serial.print(" g, Z: ");
Serial.print(accel_z);
Serial.println(" g");
}
delay(100);
}#include <SPI.h>
#include <lis2dh12.h>
// Create device object
lis2dh12 sensor;
// SPI chip select pin
const int PIN_CS = 10;
void setup() {
Serial.begin(9600);
// Initialize SPI
SPI.begin();
// Setup the LIS2DH12 (SPI library, CS pin, SPI speed)
if (sensor.setup(SPI, PIN_CS, 10000000) != 0) {
Serial.println("Failed to setup LIS2DH12");
return;
}
// Detect the device
if (!sensor.detect()) {
Serial.println("LIS2DH12 not detected");
return;
}
// Configure sensor settings
sensor.range_set(LIS2DH12_RANGE_4G);
sensor.resolution_set(LIS2DH12_RESOLUTION_12BITS);
sensor.sampling_set(LIS2DH12_SAMPLING_200HZ);
sensor.axis_enabled_set(true, true, true);
Serial.println("LIS2DH12 initialized");
}
void loop() {
// Read acceleration values
int16_t accel_x, accel_y, accel_z;
if (sensor.acceleration_read(accel_x, accel_y, accel_z) == 0) {
Serial.print("X: ");
Serial.print(accel_x);
Serial.print(", Y: ");
Serial.print(accel_y);
Serial.print(", Z: ");
Serial.println(accel_z);
}
delay(50);
}#include <Wire.h>
#include <lis2dh12.h>
lis2dh12 sensor;
const uint8_t I2C_ADDRESS = 0x18;
void setup() {
Serial.begin(9600);
Wire.begin();
sensor.setup(Wire, I2C_ADDRESS);
if (!sensor.detect()) {
Serial.println("LIS2DH12 not detected");
return;
}
// Configure sensor
sensor.range_set(LIS2DH12_RANGE_2G);
sensor.sampling_set(LIS2DH12_SAMPLING_100HZ);
sensor.axis_enabled_set(true, true, true);
// Configure activity detection
// Threshold: 500 mg, Duration: 2 seconds
sensor.activity_configure(500, 2000);
// Route activity status to INT2 pin
sensor.activity_int2_routed_set(true);
Serial.println("Activity detection configured");
}
void loop() {
// Your code here - check INT2 pin for activity status
delay(100);
}Initializes the LIS2DH12 device for I2C communication.
i2c_library: I2C library instance to use (typicallyWire)i2c_address: I2C address of the device (0x18 when SA0 is LOW, 0x19 when SA0 is HIGH)
Returns 0 on success, or a negative error code otherwise.
Initializes the LIS2DH12 device for SPI communication.
spi_library: SPI library instance to use (typicallySPI)spi_pin_cs: GPIO pin number connected to the device's chip select pinspi_speed: SPI clock speed in Hz (default: 10000000)
Returns 0 on success, or a negative error code otherwise.
Detects if the LIS2DH12 device is present by reading the device ID register.
Returns true if device is detected, false otherwise.
Sets the measurement range of the accelerometer.
range_g: Desired range (LIS2DH12_RANGE_2G, LIS2DH12_RANGE_4G, LIS2DH12_RANGE_8G, or LIS2DH12_RANGE_16G)
Returns 0 on success, or a negative error code otherwise.
Gets the current measurement range of the accelerometer.
range_g: Output parameter for the current range
Returns 0 on success, or a negative error code otherwise.
Sets the resolution of the accelerometer.
resolution: Desired resolution (LIS2DH12_RESOLUTION_8BITS, LIS2DH12_RESOLUTION_10BITS, or LIS2DH12_RESOLUTION_12BITS)
Returns 0 on success, or a negative error code otherwise.
Sets the output data rate (sampling rate) of the accelerometer.
sampling_hz: Desired sampling rate (LIS2DH12_SAMPLING_1HZ, LIS2DH12_SAMPLING_10HZ, LIS2DH12_SAMPLING_25HZ, LIS2DH12_SAMPLING_50HZ, LIS2DH12_SAMPLING_100HZ, LIS2DH12_SAMPLING_200HZ, LIS2DH12_SAMPLING_400HZ, LIS2DH12_SAMPLING_1344HZ, LIS2DH12_SAMPLING_1620HZ, or LIS2DH12_SAMPLING_5376HZ)
Returns 0 on success, or a negative error code otherwise.
Enables or disables individual axes.
enabled_x: Enable X axis (true) or disable (false)enabled_y: Enable Y axis (true) or disable (false)enabled_z: Enable Z axis (true) or disable (false)
Returns 0 on success, or a negative error code otherwise.
Reads acceleration values for all three axes in g-force units.
accel_x: Output parameter for X-axis acceleration in gaccel_y: Output parameter for Y-axis acceleration in gaccel_z: Output parameter for Z-axis acceleration in g
Returns 0 on success, or a negative error code otherwise.
Reads raw acceleration values for all three axes.
accel_x: Output parameter for X-axis raw acceleration valueaccel_y: Output parameter for Y-axis raw acceleration valueaccel_z: Output parameter for Z-axis raw acceleration value
Returns 0 on success, or a negative error code otherwise.
Configures the activity/inactivity detection feature.
threshold_mg: Threshold in millig above which movements will be considered as activityduration_ms: Duration in milliseconds before entering inactivity state when movement is below threshold
Returns 0 on success, or a negative error code otherwise.
Routes the activity/inactivity status to the INT2 pin.
routed: true to route to INT2, false to disable
Returns 0 on success, or a negative error code otherwise.
- Communication interfaces: I2C or SPI
- I2C addresses: 0x18 (SA0 LOW) or 0x19 (SA0 HIGH)
- Measurement ranges: ±2g, ±4g, ±8g, ±16g
- Resolutions: 8-bit, 10-bit, or 12-bit
- Output data rates: 1 Hz to 5.3 kHz
- Features: Activity detection, double tap detection, interrupt generation