MMA8452Q detect direction of motion


I am planning to mount a MMA8452Q accelerometer on my window pane, which slides to open or close.

And, I want to detect when the window is slid open or shut, by detecting motion and direction of acceleration (and in the future, duration of motion).

I am currently able to detect motion using ‘Transient (HPF) acceleration detection’ on one axis. It is also supposed to give me ‘polarity’ of the acceleration, which, I suppose, should indicate the direction of initial acceleration. But the polarity is extremely inaccurate.

If I try moving it 10 times in the same direction, 50% of the time, I get a wrong polarity.

Since I want the instantaneous direction of acceleration, I haven’t averaged the reading.

I tried taking 50 samples immediately after the motion was detected, but failed to see a “trend” that I could detect. May be a seasoned eye can spot it (both attached images are for motion in the same direction, but one is for a short duration and another for much longer)

Can anyone please tell me what I am doing wrong here, or if I am missing something?

Negative Y long.png

Negative Y short.png

What you should be doing is integrating the acceleration to get an estimate of velocity, since
the sign of the velocity is what you want - not the sign of the acceleration.

You also have to cancel drift in the integration by gating on the acceleration absolute value
(ie when stationary for a few seconds, stop integrating, reset velocity to zero)

Thanks, I'll try this out!

My assumption was that when starting from rest, the sign of velocity and acceleration will be the same (and since all I wanted was the sign, I was looking for shortcuts)

What you should be doing is integrating the acceleration to get an estimate of velocity

This worked perfectly! Thanks :slight_smile: