Hi,
I am a newbie here, Recently I was working with H3LIS331DL 3-Axis Linear Accelerometer I²C Mini Module

Here is code for the X, Y, Z-axis.

#include <Wire.h>

// H3LIS331DL I2C address is 0x18(24)
#define Addr 0x18

void setup()
{
  // Initialise I2C communication as MASTER
  Wire.begin();
  // Initialise Serial Communication, set baud rate = 9600
  Serial.begin(9600);

  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Select control register 1
  Wire.write(0x20);
  // Enable X, Y, Z axis, power on mode, data output rate 50Hz
  Wire.write(0x27);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Select control register 4
  Wire.write(0x23);
  // Set full scale, +/- 100g, continuous update
  Wire.write(0x00);
  // Stop I2C Transmission
  Wire.endTransmission();
  delay(300);
}

void loop()
{
  unsigned int data[6];
  for(int i = 0; i < 6; i++)
  {
    // Start I2C Transmission
    Wire.beginTransmission(Addr);
    // Select data register
    Wire.write((40+i));
    // Stop I2C Transmission
    Wire.endTransmission();

    // Request 1 byte of data
    Wire.requestFrom(Addr, 1);
    // Read 6 bytes of data
    // xAccl lsb, xAccl msb, yAccl lsb, yAccl msb, zAccl lsb, zAccl msb
    if(Wire.available() == 1)
    {
      data[i] = Wire.read();
    }
  }
  delay(300);

  // Convert the data
  int xAccl = ((data[1] * 256) + data[0]);
  int yAccl = ((data[3] * 256) + data[2]);
  int zAccl = ((data[5] * 256) + data[4]);

  // Output data to serial monitor
  Serial.print("Acceleration in X-Axis : ");
  Serial.println(xAccl);
  Serial.print("Acceleration in Y-Axis : ");
  Serial.println(yAccl);
  Serial.print("Acceleration in Z-Axis : ");
  Serial.println(zAccl);
  delay(300);
}

So my question is

how can I use this sensor to detect the harsh braking of the vehicle using Arduino nano?

Dani AI

Generated

Good start, — and ’s point about filtering and using the vector magnitude is on target. Practical plan: configure the H3LIS331DL for a suitable ODR, read full X/Y/Z samples (MSB+LSB, signed), convert counts → g using the device sensitivity, remove gravity (filter), then test a longitudinal (forward) component against a tuned deceleration threshold. The H3LIS331DL supports selectable full scales (±100/200/400 g), ODRs up to 1 kHz, and block-data-update to avoid MSB/LSB mismatch — see the datasheet for register details. ()

Convert and filter (essentials)

  • Combine OUT_X_H/OUT_X_L into a signed 16-bit value, then scale: accel_g = counts * (sensitivity_mg / 1000). For FS = ±100 g the datasheet lists 49 mg/LSB (use the FS you set). Convert g → m/s² by multiplying by 9.80665. ()

Example core logic (focusing on math, not I2C calls):

float sens_mg = 49.0;          // ±100g example (datasheet)
int16_t raw = (msb<<8) | lsb;  // combine and interpret as signed
float a_m_s2 = raw * (sens_mg/1000.0) * 9.80665;

gravity = gravity*0.99 + a_m_s2*0.01;   // low-pass gravity estimate
float linear = a_m_s2 - gravity;       // remove gravity
if (linear < -threshold_m_s2) { /* harsh braking */ }

Thresholds, interrupts and tuning
Start by tuning thresholds per vehicle — literature and telematics implementations frequently use values around 2.5 m/s² for “harsh braking” as a starting point, then increase/decrease based on real runs. (link.springer.com) The H3LIS331DL has programmable interrupt threshold/duration registers (INT1_THS, INT1_DURATION and INT1_CFG) so you can offload event detection to the sensor and wake the Nano only when the configured condition occurs; consult the INTx register descriptions. ()

Practical tips
Mount the board rigidly and orient X to face forward if possible (or calibrate the forward axis by recording steady straight-line motion). Log raw and filtered traces on short test drives to pick a threshold and a minimum duration (debounce) to reject potholes and bumps. Enable BDU (CTRL_REG4) to avoid partial reads and choose an ODR high enough for your braking profile (100 Hz or higher is common). ()

If more detail is wanted: register values to set ODR/FS/BDU, a short Arduino sketch that shows correct MSB/LSB + two’s-complement handling, and a suggested tuning procedure (static calibration → controlled braking runs → threshold choice) can be posted.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.