Measuring tilt with an MMA7260Q accelerometer

analogRead() will return a value from 0 to 1023 proportional to the voltage on the associated pin

ops.. sure.. replace 1024 with 1023 in my post above.

Besides the problems pointed out already, could you link to the spec sheet of the part? Some accelerometers have on board 5V-3.3V converter and only output 3.3/2=1.65V when there is no acceleration. I want to make sure you're not overlooking this possibility.

Thx for the replies!

I'll try this as soon as possible and post the result here.

@liudr he is actually offsetting 1650 mV from the read value so it seems that he implemented what you suggested.

hi,

i'm also working on a tilt sensor but with a MMA7361.
the code must be quite the same, as my code works more or less the same also with an ADXL335. just changing constant like sensibility.

for the moment, the result of my code is really far from perfection. but maybe together we will find a good compromise.

actually i'm working on the "tilt sensing", but it's just a first step of what i'm trying to do...

http://arduino.cc/forum/index.php/topic,52514.0.html

the posted code, here, is really bad, i will post my last one tonight, please get a look on it....

regards

Benjamin

Hi, I tried this but with no good results.. The outputs in mV from x = (analogRead() / 1024) * 3300mV are (when flattened):

x ~ 1020
y ~ 1050
z ~ 1650

When using the same code for calculating the angles:

x ~ 27.5°
y ~ 28.3°
z ~ 48.5°

What would be the right output when the accelerometer is flattened? Im using range of 1.5g. So does this mean that I can't measure big angles (no problem for my project, just asking)?

Thx!

lebenj:
hi,

i'm also working on a tilt sensor but with a MMA7361.
the code must be quite the same, as my code works more or less the same also with an ADXL335. just changing constant like sensibility.

for the moment, the result of my code is really far from perfection. but maybe together we will find a good compromise.

actually i'm working on the "tilt sensing", but it's just a first step of what i'm trying to do...

http://arduino.cc/forum/index.php/topic,52514.0.html

the posted code, here, is really bad, i will post my last one tonight, please get a look on it....

regards

Benjamin

Hi, if you have a working code for this kind of accelerometer, this would really help me :slight_smile:

Ideally, when the accelerometer is flat you should read x=0g, y=0g, z=1g.

Do you have your AREF connected to a 3.3Volts source? You have also to set this in the code analogReference() - Arduino Reference
analogReference(EXTERNAL)

Ax = x-Voff/sens;

This calculation will not produce the value you want as the order of operations is off. Division will occur before subtraction, but you want the subtraction to occur first. IE, offset the x value, then divide it by the sensitivity (which is not the same as dividing the offset by the sensitivity, and subtracting that value from the x value, which basically means you're just subtracting 2 from all your values).

Ax = (x-Voff)/sens;

Paranthesis have the highest order of operations, so operation inside paranthesis will always be performed before operations outside them.

Whats the use of the doing that? So its not possible to use the sensor with only your laptop and an Arduino board?

jraskell:

Ax = x-Voff/sens;

This calculation will not produce the value you want as the order of operations is off. Division will occur before subtraction, but you want the subtraction to occur first. IE, offset the x value, then divide it by the sensitivity (which is not the same as dividing the offset by the sensitivity, and subtracting that value from the x value, which basically means you're just subtracting 2 from all your values).

Ax = (x-Voff)/sens;

Paranthesis have the highest order of operations, so operation inside paranthesis will always be performed before operations outside them.

Off course! Stupid mistake. Thanks!

Whats the use of the doing that? So its not possible to use the sensor with only your laptop and an Arduino board?

Any voltage applied to the AREF pin after calling analogReference(EXTERNAL) will be taken as the reference voltage for the analog-digital converter.. this means that if you have a 3.3V voltage on the AREF pin, when you use the analog input you will get 1023 when the voltage read on it is 3.3V.

Without doing so, the default voltage (on any 16Mhz arduino) would be 5 Volts. So if you don't do the above and you expect your analog voltages to be 3.3V maximum you are loosing almost 1/3 of precision.

Ok,

But normally this should also work without doing that thing with Aref. Just with less accuracy. But I'm just receiving crap.. I can't see what wrong in the code.. Maybe I need to do some sort of calibration?

Thx

Why don't try taking the math out of the equation and just focus on what analogRead is returning first. At the lowest sensitivity setting for that device with it laying "perfectly flat" you should get returned values of ~337 for X and Y and ~501 for Z. This is assuming you are powering the device with 3.3volts and are using the DEFAULT analogReference. If you're values are not close to those numbers and semi stable, you may have a wiring problem.

This is the output of my analogRead (with time-averaging of 20 samples):

x: fluctuating between 307-330
y: fluctuating between 321-333
z: fluctuating between 508-516

The values do fluctuate serious, even with the time-averaging. This is probably not normal?!

Remind me what range of acceleration in g can the accelerometer measure? (spec sheet)
Also do you have a stable surface for the accelerometer or is it too close to a computer? The computer cooling fan could be a source of vibration. May be important if your accelerometer has a high sensitivity.

It could also be instability in the reference(supply) voltage to the ADC (Arduino). This accelerometer is ratiometric which means the voltage output is relative to the supply voltage at any given acceleration. This is useful if using the same supply voltage for the accelerometer and A/D reference. In this case I'm assuming you're using the 3.3V pin as the supply and the Arduino is setup to use the DEFAULT 5V analogreference.

First off try reading just a single axis and see if it's less noisy. If it is maybe you need a delay between axis' to let the voltage stabilize.

liudr:
Remind me what range of acceleration in g can the accelerometer measure? (spec sheet)

I'm using the 1.5g range, with a sensitivity of 800 mV/g. Maybe the sensitivity is to high? I can select other ranges: 2, 4 and 6g, but with lower sensitivity.

liudr:
Also do you have a stable surface for the accelerometer or is it too close to a computer? The computer cooling fan could be a source of vibration. May be important if your accelerometer has a high sensitivity.

It was close to a computer. I can try to put it further away from it.

I implemented a Kalman filter to fix the problem of the unstable readings. This works very well. The accelerometer responds well to changed and the value is very stable.

This sensor has 3 sensitivity settings ... Checkthe data sheet to get what you want, also I had to put a pullup resistor on the sleep pin to get it to stay on ... And third this sensor puts out a value of 0 - 3v3 with the center range being 3v3/2 , negative and positive g will either be more or less ... The mv values posted earlier look correct ... You can reduce noise slightly by using a rc circuit as destroyed In the data sheet... you will need to calculate g's from the mv if that is what your looking for.