Trying to use low pass filter on mpu6050

I have looked through all other posts I can find on using the MPU6050 with the filter on yet most code is outdated. I was looking for the current accepted solution. When I tap even the other side of the table my mpu6050 is sitting on, I get high frequency sudden readings on my sensor. I was told to use a low pass filter.

Thanks, S

The MPU6050 has been obsolete for five years or so, so why worry about "outdated" code?

Because the arduino isn't out of date. Also, why is the sensor obsolete? What new sensor are people using.

It is number 1

The company that designed the MPU6050 stopped making it years ago. What you can buy today are counterfeits, rejects and clones. Some work, some don't at all.

The new sensors work much better, e.g. LSM6DS33

It is number 1

Do believe everything you read on the web, especially pages with big, fat click buttons linking to Chinese Amazon resellers?

1 Like

I don't plan to switch sensors. A work around for the MPU6050 would be nice.

Thanks,
S

Here is a digital low pass filter. You can modify it to use floating point if you like.

unsigned int smooth(unsigned int newVal) {
  static unsigned int oldVal = 0;
  unsigned long sum;
    //  optimize sum = (oldVal * 3 + newVal) / 4;
    sum = ( (oldVal << 1) + oldVal + newVal) >> 2;
  }
  oldVal = sum;
  return oldVal;
}
...
  // example use
  smoothedValue = smooth( analogRead(A0) );

...

My 'C' is a bit rusty. Will the << and >> work ok with floating point?

No, that is why I included the normal math in the comment line above it. You can also use different coefficients, e.g.

sum = (oldVal * 7 + newVal) / 8;

The general pattern is (oldVal * (n-1) ) + newVal / n

I chose an 'n' that is power of 2, so that the shifts would be possible. But with floating point, you don't need that.

I'm only assuming your data is float, if you can obtain raw integer data from the sensor, it is better to filter that because then you can use the faster integer math with shifts.

Notice that the integer version is unsigned. You can't safely shift a signed integer, to perform fast multiplication or division.

The usual advice is to use a Kalman filter. It's more complicated, but you can find a Kalman library too. My post is a "quick and dirty get the job done" but I've tested and used it in real applications.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.