# Reducing jitter and improving speed of accelerometer output

I'm not sure if this is an EE question or a StackExchange question due to the programming involved, but anyway here goes:

I just built a tilt sensor for my tractor's front loader. I wanted something easy to read in bright daylight, wireless, magnetic attachment, and reasonably accurate to indicate the tilt and level of my front loader so I can use it to level some ground easily.

I used my own custom-built wireless MCU as the controller, interfacing via I2C with a GY-521 accelerometer/gyroscope module.

The display is just a servo motor connected to an indicator hand and a laser-printed dial face, and 5 LEDs also.

Here's a video of the device working: https://www.youtube.com/watch?v=xWT8AyE1JLU (sorry about the noise, I was trying to show how jitter doesn't seem to affect it as bad!)

In testing, it worked great. But as soon as I snapped the sensor to my tractor's bucket, the vibration from the engine caused the accelerometer readings to go crazy. The needle basically jumps from one end to the other, making the readings useless.

So first I tried to average the output. (I am sampling at approx 5-10 samples per sec) That still yielded shaky results.

So I implemented a low-pass filter, using a coefficient of 0.1 for new readings. As you can see in the video, this helped to eliminate the jitter, but now the readings are too laggy. Turning the device from one end of its range to the other, it takes several seconds for the needle to catch up to the input. This also makes it unusable, as it needs to be more realtime. And as you can see, it takes a long time to get back to zero.

Does anyone have suggestions/ideas on how to reduce jitter from the engine vibrations and bumps/thumps experienced by the bucket, while yielding a fairly fast and accurate output?

Note that my MCU won't be able to handle lots of math for each sample, so don't know if that eliminates FFT or not - it's only a basic 8051 running at 16MHz.

Here are the links for spec sheet/register map for the Accel/Gyro: https://www.cubby.com/pli/Spec+Sheet.pdf/_506bd71d53d3454282f06fa698666f04 https://www.cubby.com/pli/Register+Map.pdf/_17685da9808c42099c5ebdc1058c63b3

Any info appreciated!

• Can you use the gyro without accelerometer? Can you mount it on shock absorbing material? – gsills May 8 '14 at 22:58
• Remember that if you want to use the math of signal processing you have to ensure that their assumptions are met. You cannot use digital filtering if any of the frequency content of the input signal is above half the sampling frequency. More precisely: you can do it. But the results of the computations may not be what you expect them to be. – Blup1980 May 9 '14 at 6:18
• I'm currently using only the accel values, not the gyro. – Ryan Griggs May 9 '14 at 12:33

It sounds like the display response time you need is fairly and squarely coincident with some of the dominant vibration frequencies seen on the tractor's front loader and this could be a right royal b*tch to solve. In my opinion you do need to sample at a much higher rate to prevent aliasing - this is high frequency vibrations becoming aliased down to lower frequencies due to inadequate sampling rate. This may be enough to overcome your problem. Next is higher order low pass filtering like 2nd order of third/fourth order. It seems, in your question you have implemented a 1st order filter but this could produce much better results at a higher order BUT, you're running out of CPU speed.

OK, as a definite must-do thing, you must remove frequencies that can cause aliasing at your sample rate and maybe consider a fourth order analogue low-pass filter by cascading two sallen-key filter stages. Here is a calculator that you can set for a low pass frequency of 2Hz (the best you're going to get with 10Hz sampling and lots of nasty high frequency stuff.

Good luck - I'm not going to say you will be successful without some form of mechanical dampening and a high order analogue LPF at the front-end.

• You have it, I'd sample it at least at 1kHz. And I'd use the accelerometer only, gyroscope for such a slow application is just a noise injector. – Vladimir Cravero May 9 '14 at 7:59
• Can you point me to any resources on how to write a fourth order lpf in code? I have looked at analog lpfs but this accel chip outputs in digital format. So i have to process the data in code, not at the analog signal level. – Ryan Griggs May 9 '14 at 12:30
• Go to my profile page and follow the link to my music page then look for an article on digital filters I wrote - it can be downloaded as a pdf - it was written to get analogue engineers familiar with digital filtering so it might be appropriate to you - it covers basic 2nd order digital filters and these can be cascaded to make higher orders. – Andy aka May 9 '14 at 12:37

I was able to get the sample rate up to about 26HZ and that's about all my little MCU can do. However, it stabilized the output quite well.

So currently, I am doing the following:

1) Sampling the accel. at 26Hz (as fast as possible)

2) Low-pass filtering with a coefficient of 0.05

Here's the code to do the low-pass filtering (in C):

new_accel_x_value = read_accel_x();   // Read value from X accelerometer
accel_x = (new_accel_x_value * 0.05) + (accel_x * 0.95);   // accel_x stores the "filtered" accelerometer X value and passes it on to the display.


There is a bit of lag in the output (about 500ms) , but the engine vibrations are completely eliminated and the output is very stable and accurate. Used it yesterday for several hours with good results.