-1

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 <movingAvg.h>                 
 #include <Wire.h>

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

       movingAvg xAxisAverage(5);

       int readingIndex=0;
       int readingArray[10];
       int firstFiveReadingAverage;
       int lastFiveReadingAverage;

    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]);

    if(readingIndex<10)
  {
     readingArray[readingIndex]=xAccl;
     readingIndex++;
     if(readingIndex == 10)
  {
    firstFiveReadingAverage=(readingArray[0]+readingArray[1]+readingArray[2]+readingArray[3]+readingArray[4])/5;
    lastFiveReadingAverage= (readingArray[5]+readingArray[6]+readingArray[7]+readingArray[8]+readingArray[9])/5  ;
   if(lastFiveReadingAverage>firstFiveReadingAverage)
    {
     Serial.print("Hardbrake Pressed");
     Serial.println();
    }
     readingIndex=0;
    }  
    }


  //int avg = avgAccl.reading(xAccl);
  // calculate the moving average


     // 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);
        Serial.println();

     int average=xAxisAverage.reading(xAccl);

    if (average < -1000)
    {
     Serial.print("Hardbraking: ");
    //Serial.print(average/100.0);
      Serial.print(average);
      Serial.println();
        }
       }

I have changed the code, but values are keeps on repeating when I trigger the hard braking and it keep on repeating even I released the brake.

Here is the output enter image description here

4
  • A harsh braking is nothing more than a strong negative acceleration, so you could compare your accelaration values to a threshold. How is this connected to averaging? Commented Dec 12, 2019 at 14:05
  • Acutally, I am a newbie in arduino, can you please share me some examples. Commented Dec 12, 2019 at 14:36
  • 1
    Examples of what? Your title and your question seem totally different to me? It is unclear, what you are actually asking Commented Dec 12, 2019 at 14:46
  • how can I use this sensor to detect the harsh braking of the vehicle using Arduino nano? Commented Dec 12, 2019 at 17:41

1 Answer 1

0

A harsh braking is nothing more than a strong negative acceleration, so you could compare your accelaration values to a threshold. Let's assume, that the positive x-axis is the moving direction of your vehicle. This means, that braking will give an acceleration in the negative x direction. Then you first define a specific threshold for the acceleration. An acceleration in the negative x direction above this level means, that it is a "harsh" braking (you will have to test yourself, what value you want for this). Then you test, if the read values are below the negative of our threshold (since acceleration in the negative direction will be presented by negative number).

int braking_threshold=1000;
...
void loop(){
    ...
    // Convert the data
    int xAccl = ((data[1] * 256) + data[0]);
    int yAccl = ((data[3] * 256) + data[2]);
    int zAccl = ((data[5] * 256) + data[4]);

    if(xAccl < -1*braking_threshold){
        // Do whatever you want to do at a harsh braking here
    }
    ...
}

Note, that often the accelerometer data is rather noisy, especially, when mounted on a vehicle, that will vibrate during the motion (due to motors and such). You might need to apply some filter to the read values, before you can use them. That can be as easy as averaging some values, but can also go on to more complex filter algorithms, depending on how noise free you need the data. You will have to test, what values you realistically get in your application and how noisy they are. But data noise reduction is a complete different topic and thus off-topic for this question.

Also note, that your accelerometer measures a range of 200g. That's quite much I would say. When measuring relatively low accelerations the signal to noise ratio will be rather bad. So I hope, that you really want to measure accelerations, that fill this range, rather than only using a very small percentage of the range.

7
  • Thanks for the help, I am thinking this way only but not sure how to use. and i thinking to store the values in an array, lets say 5 values stored and then average of those 5 values is taken and then new 5 values stored and average of those values is taken.After that we will compare the average and this thing is happening in a loop.How to write code for this? Commented Dec 12, 2019 at 18:40
  • You can google for "moving average". There is plenty of example code on the web and on this site. Commented Dec 12, 2019 at 19:24
  • Thanx for the help. Commented Dec 16, 2019 at 9:17
  • I have made this code but when pressing hard brake, then the average value keeps on repeating itself even I have released it. Commented Dec 16, 2019 at 12:35
  • You can check the code above.. Commented Dec 16, 2019 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.