I'm currently working on a system that calculates the velocity by integrating the values of the acceleration.

I'm using the ADXL345 accelerometer with a ESP32, and so far, everything's going good, i'm getting correlated velocity and what not.

What i'm really confused about is the data rate, i've asked on another post and got a really good answer, but now i want to know that, if my output data rate from my acceleoremter is 100Hz should i synchronize it with my arduino code to get the data every 10 ms?

for(int i=0; i<340; i++)
  Wire.requestFrom(0x53, 2);
  values_y0[i] = Wire.read();
  values_y1[i] = Wire.read();

} // the loop takes 3,11 seconds to finish

for(int i=0; i<340; i++)
  values_y1[i] = values_y1[i] & 0x03;
  y = (values_y1[i] << 8) + values_y0[i];
  if(y > 512)
    y = y - 1023;
  values_y[i] = y * 0.004 * 9.8;

  // some filtering
  if(values_y[i] <= 0.16 && values_y[i] >= -0.16)  
    values_y[i] = 0;

for (int i=0; i<340; i++)


I take 340 data points from the accelerometer, put them in an array and then work on them. The first loop does that, and it takes 3,11 seconds to finish which means 109Hz.


1 Answer 1


If you want go faster, you should change your algorithm.

"Moving" window and "circular buffer".

In your array, suppress the old data and insert one new data... with the fact that you have done some calculus ...

  • \$\begingroup\$ Alright, that's a good idea, but is my logic of synchronizing my code as I did above good? I'll probably apply some of the algorithms you showed me, but at this time, I only need to run the program once, so I don't really need to erase old data. \$\endgroup\$
    – B.Adlane
    Commented Feb 23, 2022 at 19:22
  • 1
    \$\begingroup\$ Ok. You should only use a timer task to take regular samples ( take sample in task + sample numbering). If you use a circular buffer, use a power of 2 (64, 128, 256 ...) \$\endgroup\$
    – Antonio51
    Commented Feb 23, 2022 at 19:39

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.