I use time-averaging over 10 samples.. Offset and Sensitivity came from the datasheet..

When I align the axis of the accelerometer with the ground, I receive angles of x: 27°, y:28° and z: 47° when i accept them to be 0°...

Now when I start tilting the accelerometer, the angles change (dont think correctly). The problem is that the highest angle is 50° and the lowest 20°. Behind those values the angles start moving in the other direction again.. Must be something wrong in the code. Can someone please help?!

You are using the read value (x above) as it would be expressed in mV as the other numbers. But this is not the case. analogRead() will return a value from 0 to 1024 proportional to the voltage on the associated pin.

The following holds true:read value from analogRead() : 1024 = read value in mV : 3.3 V (provided that you connected the AREF to 3.3V)

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.

You are using the read value (x above) as it would be expressed in mV as the other numbers. But this is not the case. analogRead() will return a value from 0 to 1024 proportional to the voltage on the associated pin.

The following holds true:read value from analogRead() : 1024 = read value in mV : 3.3 V (provided that you connected the AREF to 3.3V)

so if you want x expressed in mV.. x = (analogRead() / 1024) * 3300mV

Hope this helps,

Fabio Varesano

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

x ~ 1020y ~ 1050z ~ 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)?

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).

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).

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.

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?