Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions examples/Basic/Basic.ino
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,7 @@ void loop()

// Read temperature - need to calibrate.
Serial.print(" Temperature = ");
#ifdef LOW_POWER
Serial.println( tempCalibrate + (int8_t) myIMU.readTemperature());
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since LIS3DH::readTemperature() now returns int8_t, the explicit cast to (int8_t) here is redundant. Dropping it makes the example a bit clearer and ensures the printed value reflects the API directly.

Suggested change
Serial.println( tempCalibrate + (int8_t) myIMU.readTemperature());
Serial.println( tempCalibrate + myIMU.readTemperature());

Copilot uses AI. Check for mistakes.
#else
int16_t tempTemp;
tempTemp = (int16_t) myIMU.readTemperature();
Serial.println( tempTemp + tempCalibrate );
Serial.print("BIN : ");
Serial.print( tempTemp & 0xFF, BIN);
Serial.print("\t");
Serial.println( tempTemp >> 8, BIN);
#endif

delay(3000); // every second temperature is sometimes erratic.

Expand Down
4 changes: 2 additions & 2 deletions library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "LIS3DH motion detection",
"version": "0.0.6",
"version": "0.0.7",
"description": "Motion detection without bells and whistles, simply works, low power",
"keywords": "sensors, accelerometer, motion detection, motion detector, low power, mobile, battery, web, cloud, iot, gsm, gprs, gps, gnss",
"authors":
Expand All @@ -24,4 +24,4 @@
"frameworks": "arduino",
"platforms": "*",
"examples": "examples/*/*.ino"
}
}
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=LIS3DH motion detection
version=0.0.6
version=0.0.7
author=Leonardo Bispo
license=MIT
maintainer=Leonardo Bispo <l.bispo@live.com>
Expand All @@ -8,4 +8,4 @@ paragraph=Motion detection without bells and whistles, simply works, low power.
category=Sensors
url=https://github.com/ldab/lis3dh-motion-detection
architectures=*
includes=LIS3DH-motion-detection.h
includes=LIS3DH-motion-detection.h
25 changes: 3 additions & 22 deletions src/lis3dh-motion-detection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ imu_status_t LIS3DH::readRegisterRegion(uint8_t *outputPointer , uint8_t offset,
}
else //OK, all worked, keep going
{
// request 6 bytes from slave device
// request length bytes from slave device
Wire.requestFrom(I2CAddress, length);
while ( (Wire.available()) && (i < length)) // slave may send less than requested
{
Expand Down Expand Up @@ -158,7 +158,6 @@ imu_status_t LIS3DH::readRegister(uint8_t* outputPointer, uint8_t offset) {
imu_status_t LIS3DH::readRegisterInt16( int16_t* outputPointer, uint8_t offset )
{
{
//offset |= 0x80; //turn auto-increment bit on
uint8_t myBuffer[2];
imu_status_t returnError = readRegisterRegion(myBuffer, offset, 2); //Does memory transfer
int16_t output = (int16_t)myBuffer[0] | int16_t(myBuffer[1] << 8);
Expand Down Expand Up @@ -485,41 +484,23 @@ void LIS3DH::temperatureEnable(bool command){
}
}

// Only in low power we have 8bit. Otherwise 10bit
#ifdef LOW_POWER
// 8bits in all modes
int8_t LIS3DH::readTemperature(){
uint8_t r;

temperatureEnable(1); // Although temperature is enabled, it must
// re-run to re-apply settings. Otherwise
// the temperature report is always the same.

// do we have data?
readRegister(&r, LIS3DH_STATUS_REG_AUX);
// check if we have data
if ( ( r & 0x04 ) == 0x04 ) {
readRegister(&r, LIS3DH_OUT_ADC3_H); // here are the data
return (int8_t) r;
Comment on lines 491 to 499
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

readTemperature() enables BDU in temperatureEnable(true) but then only reads OUT_ADC3_H. With BDU enabled, the sensor’s output registers typically don’t update until both bytes are read, so reading only the high byte can cause the temperature value to appear “stuck” and forces this function to re-apply settings every time. Consider either (a) reading both OUT_ADC3_L and OUT_ADC3_H in one burst and using the high byte as the 8-bit value, or (b) not enabling BDU for this path.

Copilot uses AI. Check for mistakes.
}
// we don't have data. Return deep freezing for error.
return 0xFF;
}
Comment on lines 495 to 503
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

readTemperature() uses 0xFF as an error sentinel, but as an int8_t this is -1, which can also be a legitimate relative temperature reading. Returning a value that can’t be distinguished from valid data makes error handling unreliable; consider returning a status (e.g., imu_status_t + out param) or using a separate “data ready” query so callers can distinguish error/no-data from a valid -1 reading.

Copilot uses AI. Check for mistakes.
#else
// 10 bit value, UNTESTED
int16_t LIS3DH::readTemperature(){
int16_t r;

temperatureEnable(1); // Read comment above

// do we have data?
readRegisterInt16(&r, LIS3DH_STATUS_REG_AUX);
if ( ( r & 0x04 ) == 0x04 ) {
readRegisterInt16(&r, LIS3DH_OUT_ADC3_L);
return (int16_t) r;
}
// we don't have data. Return deep freezing for error.
return 0xFFFF;
}
#endif

// disconnect pullup on SDO/SA for lower power consumption
void LIS3DH::disconnectPullUp(bool command){
Expand Down
12 changes: 1 addition & 11 deletions src/lis3dh-motion-detection.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ Distributed as-is; no warranty is given.
#ifndef __LIS3DH_IMU_H__
#define __LIS3DH_IMU_H__

// Temperature works only if I define LOW_POWER here (!).
// Tested with arduino, not with platformio
#if !defined LOW_POWER & !defined NORMAL_MODE & !defined HIGH_RESOLUTION
#define LOW_POWER
#endif

#include "stdint.h"

#if defined(ARDUINO) && ARDUINO >= 100
Comment on lines 15 to 20
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By removing the block that implicitly defined LOW_POWER when no mode macro is set, the library’s default operating mode changes for consumers who don’t define any of LOW_POWER, NORMAL_MODE, or HIGH_RESOLUTION. Previously they would compile in low-power (8-bit) mode; now they’ll compile with LPen=0 and HR=0 (normal mode). If this is intended, it should be called out in docs/release notes; otherwise consider restoring an explicit default to avoid a silent behavior/power/resolution change.

Copilot uses AI. Check for mistakes.
Expand Down Expand Up @@ -125,11 +119,7 @@ class LIS3DH
float axisAccel( axis_t _axis);
uint8_t readClick();
uint8_t readAxisEvents();
#ifdef LOW_POWER
int8_t readTemperature();
#else
int16_t readTemperature();
#endif

void temperatureEnable(bool command);

Expand Down Expand Up @@ -170,7 +160,7 @@ class LIS3DH
#define LIS3DH_CTRL_REG2 0x21
#define LIS3DH_CTRL_REG3 0x22
#define LIS3DH_CTRL_REG4 0x23
#define LIS3DH_CTRL_REG5 0x24 //not included
#define LIS3DH_CTRL_REG5 0x24
#define LIS3DH_CTRL_REG6 0x25
#define LIS3DH_REFERENCE 0x26
#define LIS3DH_STATUS_REG2 0x27
Expand Down