how to convert to radians or degree

I put #include <math.h> at the top of my coding. which looks like this:

#include <math.h>
const int xPin = 4; // X output of the accelerometer
const int yPin = 5; // Y output of the accelerometer
int tiltx=0;
int tilty=0;
void setup() {
// initialize serial communications:
Serial.begin(9600);
// initialize the pins connected to the accelerometer
tiltx=analogRead(4);
tilty=analogRead(5);
}

void loop() {

tiltx=analogRead(4);
tilty=analogRead(5);

// print the tilt angle X
Serial.print ("Tilt X = ");
Serial.print(asin(tiltx/9.81));

// print the tilt angle Y
Serial.print("\t");
Serial.print ("Tilt Y = ");

Serial.print(asin(tilty/9.81));
Serial.println();
delay (800);
}

BUT STILL NO WORKS. :frowning:

JOE

Here is part of your code in code tags

// these constants won't change:
const int xPin = 4;     // X output of the accelerometer
const int yPin = 5;     // Y output of the accelerometer
int tiltx=0;
int tilty=0;

Highlight the text to be in code tags in your post then click the # icon to add the tags. It makes code more readable and stops it being mangled if it happens to contain letter sequences that are interpreted as formatting commands. You will, of course, have read Read this before posting a programming question ... - Programming Questions - Arduino Forum before posting your question.

OK, we've now covered 1) and 2) but still word on what "ridiculous" means in this context.
Is it, perhaps, something to do with the fact that you assume "analogRead" returns a value in units of g?

Can you please check on my full code. It wont work to get the angle anyway. I have no idea. Thanks.

#include <math.h>
const int xPin = 4; // X output of the accelerometer
const int yPin = 5; // Y output of the accelerometer
int tiltx=0;
int tilty=0;
void setup() {
// initialize serial communications:
Serial.begin(9600);
// initialize the pins connected to the accelerometer
tiltx=analogRead(4);
tilty=analogRead(5);
}

void loop() {

tiltx=analogRead(4);
tilty=analogRead(5);

// print the tilt angle X
Serial.print ("Tilt X = ");
Serial.print(asin(tiltx/9.81));

// print the tilt angle Y
Serial.print("\t");
Serial.print ("Tilt Y = ");

Serial.print(asin(tilty/9.81));
Serial.println();
delay (800);
}

Regards,
JOE

Be prepared for someone to say that they will check your code once it is presented in code tags. Using the Auto Format tool from the IDE on it before posting it would help too.

What board are you using ?

What raw values do you get for tiltx and tilty ?

Forget all that, if you have 3-axis accelerometer you need 3D geometry and calculate
angle using atan2().

  Serial.println (atan2 (sqrt (x*x+y*y), z)) ;  // radians
  Serial.println (180.0 * atan2 (sqrt (x*x+y*y), z) / PI) ;  // degrees

sqrt (xx+yy) gives the horizontal component, z is the vertical component
(relative to the accelerometer), then atan2 computes the correct angle. No
scaling is required as atan2 already takes a ratio. It can never give wild values.

Basically I'm using dual axis accelerometer (ADXL210JQC) to measure the angle on X and Y axis only. I'm bit struggling and spent couple of days to sort this out but it wont work. I read through the datasheet of that accelerometer and there is a formula to convert acceleration to tilt which is looks like this:

Pitch= ASIN (Ax/1 g)
Roll= ASIN (Ay/1 g)

All I know is ASIN stand for inverse sin and Ax is the value of the acceleration given by x axis. That is why I put this formula into my code programme but it wont work. The results come out with number 1 and 0 even I rotate my acceleration randomly. I do tried to rotate it according to its axis but still no response.

Hope you understand my problem. Many thanks.

JOE

In that case you are going to have to estimate g and use asin, but the principles are
the same. total angle from vertical requires the sqrt(xx+yy) term, components in
x and y directions are individually got from asin (x) where x is scaled to be of
magnitude exactly 1.0 when the device is x-axis vertical. Any error and asin can
overflow or give poor results near horizontal - basically don't trust it for large
angles (greater than 45 from vertical).

I just had a look at the datasheet for the ADXL202/210.
Two things to note:

  1. It's obsolete.
  2. It's a digital device

But the ADXL202 also has analog outputs, plus the digital outputs are actually PWM so they could be low pass filtered to provide analog signals. Thus, it is imperative to know how the OP wired the sensor to the Arduino.

But the ADXL202 also has analog outputs,

Ah yes, you're right.;
But they don't output in gs either.
They need an offset and scaling operation.
Maybe that accounts for the "ridiculous"ness.

tiltx=analogRead(5);
    :
  Serial.print(asin(tiltx/9.81));

analogRead() does not return a floating point number all adjusted to be meaningful for the particular sensor that is connected. It returns an INTEGER between 0 and 1023, depending on where in the range of 0-5V the input voltage lies. Typically you would apply some scaling to get the actual voltage (vX = (5.0*analogRead(5)/1000.0), but it depends on signedness/etc), and then some additional math to get that into a value that is meaningful to the thing being sensed (and then, if necessary, convert to radians as required by the trig functions.)

While I appreciate the desire to properly condition newbies, it didn't really require code tags, complete code, or actual output to see that this is currently very wrong at a basic level...

it didn't really require code tags, complete code, or actual output to see that this is currently very wrong at a basic level...

Reply #22.