0
Offline
Newbie
Karma: 0
Posts: 35
Arduino rocks
|
 |
« on: March 04, 2011, 11:44:31 am » |
Hi,im trying to measure tiltangles with a MMA7260Q accelerometer. I'm using the following code: float x, y, z; // X and Y in-plane sensing int i = 0; float Ax, Ay, Az; float angleX, angleY, angleZ; float sens = 800; // mV/g float Voff = 1650; // mV const float pi = 3.14;
void setup() { Serial.begin(9600); // sets the serial port to 9600 }
void loop() { if (i == 10) { x = x/10; y = y/10; z = z/10; Ax = x-Voff/sens; Ay = y-Voff/sens; Az = z-Voff/sens; angleX = atan(Ax/sqrt(pow(Ay,2)+pow(Az,2))); angleY = atan(Ay/sqrt(pow(Ax,2)+pow(Az,2))); angleZ = atan(Az/sqrt(pow(Ay,2)+pow(Ax,2))); angleX = angleX*180/pi; angleY = angleY*180/pi; angleZ = angleZ*180/pi; Serial.print("Angles: "); Serial.print(angleX); // print the rotational rate in the X axis Serial.print(" "); // prints a space between the numbers Serial.println(angleY); // print the rotational rate in the Y axis Serial.print(" "); // prints a space between the numbers Serial.println(angleZ); // print the rotational rate in the Y axis delay(100); // wait 100ms for next reading i = 0; x = 0; y = 0; z = 0; } else { x = x + analogRead(0); y = y + analogRead(1); z = z + analogRead(2); i = i + 1; delay(50); } } 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?! Thx
|
|
|
|
|
Logged
|
|
|
|
|
Torino, Italy
Offline
Sr. Member
Karma: 2
Posts: 312
Arduino rocks
|
 |
« Reply #1 on: March 04, 2011, 12:43:55 pm » |
Ax = x-Voff/sens; 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
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Faraday Member
Karma: 15
Posts: 2852
Gorm deficient
|
 |
« Reply #2 on: March 04, 2011, 01:32:38 pm » |
analogRead() will return a value from 0 to 1023 proportional to the voltage on the associated pin
|
|
|
|
|
Logged
|
Per Arduino ad Astra
|
|
|
|
Torino, Italy
Offline
Sr. Member
Karma: 2
Posts: 312
Arduino rocks
|
 |
« Reply #3 on: March 04, 2011, 01:45:49 pm » |
ops.. sure.. replace 1024 with 1023 in my post above.
|
|
|
|
|
Logged
|
|
|
|
|
Central MN, USA
Offline
Faraday Member
Karma: 35
Posts: 5915
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
|
 |
« Reply #4 on: March 04, 2011, 02:28:26 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 35
Arduino rocks
|
 |
« Reply #5 on: March 04, 2011, 05:31:17 pm » |
Thx for the replies!
I'll try this as soon as possible and post the result here.
|
|
|
|
|
Logged
|
|
|
|
|
Torino, Italy
Offline
Sr. Member
Karma: 2
Posts: 312
Arduino rocks
|
 |
« Reply #6 on: March 04, 2011, 08:05:31 pm » |
@liudr he is actually offsetting 1650 mV from the read value so it seems that he implemented what you suggested.
|
|
|
|
|
Logged
|
|
|
|
|
Barcelona
Offline
Newbie
Karma: 0
Posts: 23
Arduino hurts my brain... but i like it
|
 |
« Reply #7 on: March 07, 2011, 11:48:44 am » |
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.htmlthe posted code, here, is really bad, i will post my last one tonight, please get a look on it.... regards Benjamin
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 35
Arduino rocks
|
 |
« Reply #8 on: March 09, 2011, 05:55:32 am » |
Ax = x-Voff/sens; 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 ~ 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!
|
|
|
|
« Last Edit: March 09, 2011, 05:59:51 am by deviukk »
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 35
Arduino rocks
|
 |
« Reply #9 on: March 09, 2011, 05:56:24 am » |
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.htmlthe 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 
|
|
|
|
|
Logged
|
|
|
|
|
Torino, Italy
Offline
Sr. Member
Karma: 2
Posts: 312
Arduino rocks
|
 |
« Reply #10 on: March 09, 2011, 03:13:33 pm » |
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 http://arduino.cc/en/Reference/AnalogReferenceanalogReference(EXTERNAL)
|
|
|
|
|
Logged
|
|
|
|
|
New Hampshire
Offline
God Member
Karma: 13
Posts: 776
There are 10 kinds of people, those who know binary, and those who don't.
|
 |
« Reply #11 on: March 09, 2011, 04:24:30 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 35
Arduino rocks
|
 |
« Reply #12 on: March 10, 2011, 06:25:28 pm » |
Whats the use of the doing that? So its not possible to use the sensor with only your laptop and an Arduino board? 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!
|
|
|
|
|
Logged
|
|
|
|
|
Torino, Italy
Offline
Sr. Member
Karma: 2
Posts: 312
Arduino rocks
|
 |
« Reply #13 on: March 12, 2011, 03:57:34 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 35
Arduino rocks
|
 |
« Reply #14 on: March 14, 2011, 09:37:48 am » |
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
|
|
|
|
|
Logged
|
|
|
|
|
|