how to convert to radians or degree

I wonder if someone knows how to convert the reading from adxl345 to degrees or radians.

I use the arduino 1.0 program, and used this code https://www.loveelectronics.co.uk/Tutorials/12/adxl345-accelerometer-arduino-tutorial

I get the raw and scaled digits but want to convert one of these to degrees, I actually want to see the slope angle of the laser pointer in which i attach the adxl345 to.

/*
ADXL345_Example.pde - Example sketch for integration with an ADXL345 triple axis accelerometer.
*/

// Include the Wire library so we can start using I2C.
#include <Wire.h>
// Include the Love Electronics ADXL345 library so we can use the accelerometer.
#include <ADXL345.h>

// Declare a global instance of the accelerometer.
ADXL345 accel;

// Set up a pin we are going to use to indicate our status using an LED.
int statusPin = 2; // I'm using digital pin 2.

void setup()
{
  // Begin by setting up the Serial Port so we can output our results.
  Serial.begin(9600);
  // Start the I2C Wire library so we can use I2C to talk to the accelerometer.
  Wire.begin();
  
  // Ready an LED to indicate our status.
  pinMode(statusPin, OUTPUT);
  
  // Create an instance of the accelerometer on the default address (0x1D)
  accel = ADXL345();
  
  // Check that the accelerometer is infact connected.
  if(accel.EnsureConnected())
  {
    Serial.println("Connected to ADXL345.");
    digitalWrite(statusPin, HIGH); // If we are connected, light our status LED.
  }
  else 
  {
    Serial.println("Could not connect to ADXL345.");
    digitalWrite(statusPin, LOW); // If we are not connected, turn our LED off.
  }
  
  // Set the range of the accelerometer to a maximum of 2G.
  accel.SetRange(2, true);
  // Tell the accelerometer to start taking measurements.
  accel.EnableMeasurements();
}

void loop()
{
  if(accel.IsConnected) // If we are connected to the accelerometer.
  {
    // Read the raw data from the accelerometer.
    AccelerometerRaw raw = accel.ReadRawAxis();
    //This data can be accessed like so:
    int xAxisRawData = raw.XAxis;
    
    // Read the *scaled* data from the accelerometer (this does it's own read from the accelerometer
    // so you don't have to ReadRawAxis before you use this method).
    // This useful method gives you the value in G thanks to the Love Electronics library.
    AccelerometerScaled scaled = accel.ReadScaledAxis();
    // This data can be accessed like so:
    float xAxisGs = scaled.XAxis;
    
    // We output our received data.
    Output(raw, scaled);
  }
}

// Output the data down the serial port.
void Output(AccelerometerRaw raw, AccelerometerScaled scaled)
{
   // Tell us about the raw values coming from the accelerometer.
   Serial.print("Raw:\t");
   Serial.print(raw.XAxis);
   Serial.print("   ");   
   Serial.print(raw.YAxis);
   Serial.print("   ");   
   Serial.print(raw.ZAxis);
   
   // Tell us about the this data, but scale it into useful units (G).
   Serial.print("   \tScaled:\t");
   Serial.print(scaled.XAxis);
   Serial.print("G   ");   
   Serial.print(scaled.YAxis);
   Serial.print("G   ");   
   Serial.print(scaled.ZAxis);
   Serial.println("G");
}

1 Radian = 57.2957795 degrees. To avoid calculating decimals with arduino, use something like the following:

deg = rad * 57296 / 1000

rad = deg * 1000 / 57296

You may find some advantage in using these conversions:

radians = (degrees * 71) / 4068

degrees = (radians * 4068) / 71

In case you wonder how good approximations like 71/4068 can be found you may want to look up the theory of continued fractions.

Except that I don't think that those answers solve the original problem.
If I understand correctly, the accelerometer will output a value of acceleration. Acceleration due to gravity is 9.8m/s/s, and as you tilt perpendicular sensors, one will read 9.8sin(theta) and the other will read 9.8cos(theta)
See any highschool physics text (or the net) on the parallel and perpendicular components of gravity on an "inclined plane."
To convert accelerometer data to an incline, scale the A-D reading so that you get m/s/s out of it, and then use the inverse trig functions (asin() or acos()) to get an angle (beware degrees vs radians return value!)
If you are tilted in the z direction as well, I think there are corrections you can do based on the readings of a third axis of accelerometer data, but that gets more complicated, and dealing with a single axis is a good start.

it seems I was unclear, what I really want is to write a CODE receiving these values (see picture) so that it ??will be printed in radians or degrees, how to convert these mathematically I can manage to do it by myself :),

the only axis that are of my interest is the x axis

do I have to include Math.h?

how do I scale the A-D to m/s/s, and where should i use the inverse trig functions ?

I've never worked with an accelerometer and I'm struggling to understand the problem. Since you seem to be wanting a result in angular units, I assume you're interested in the angle between the device's x-axis and horizontal - is that correct?

What is the relationship between (static) device positioning and the reported values?

#include <math.h>
   :
// Ax = G sin(theta)
// Ax/G = sin(theta)
// asin(Ax/G) = theta
    AccelerometerScaled scaled = accel.ReadScaledAxis();
    float angle = asin(scaled.XAxis/9.8);

Note that the default range of "2" won't allow for the 9.8 range of local gravity...

/*

*/
#include <math.h>
// Include the Wire library so we can start using I2C.
#include <Wire.h>
// Include the Love Electronics ADXL345 library so we can use the accelerometer.
#include <C:\Users\Huss\Desktop\arduino prog\koden\ADXL345_Example\ADXL345.h>

// Declare a global instance of the accelerometer.
ADXL345 accel;

// Set up a pin we are going to use to indicate our status using an LED.
int statusPin = 2; // I'm using digital pin 2.

void setup()
{
  // Begin by setting up the Serial Port so we can output our results.
  Serial.begin(9600);
  // Start the I2C Wire library so we can use I2C to talk to the accelerometer.
  Wire.begin();
  
  // Ready an LED to indicate our status.
  pinMode(statusPin, OUTPUT);
  
  // Create an instance of the accelerometer on the default address (0x1D)
  accel = ADXL345();
  
  // Check that the accelerometer is infact connected.
  if(accel.EnsureConnected())
  {
    Serial.println("Connected to ADXL345.");
    digitalWrite(statusPin, HIGH); // If we are connected, light our status LED.
  }
  else 
  {
    Serial.println("Could not connect to ADXL345.");
    digitalWrite(statusPin, LOW); // If we are not connected, turn our LED off.
  }
  
  // Set the range of the accelerometer to a maximum of 2G.
  accel.SetRange(2, true);
  // Tell the accelerometer to start taking measurements.
  accel.EnableMeasurements();
}

void loop()
{
  if(accel.IsConnected) // If we are connected to the accelerometer.
  {
    // Read the raw data from the accelerometer.
    AccelerometerRaw raw = accel.ReadRawAxis();
    //This data can be accessed like so:
    int xAxisRawData = raw.XAxis;
    
    // Read the *scaled* data from the accelerometer (this does it's own read from the accelerometer
    // so you don't have to ReadRawAxis before you use this method).
    // This useful method gives you the value in G thanks to the Love Electronics library.
    AccelerometerScaled scaled = accel.ReadScaledAxis();
   // Ax = G sin(theta)
// Ax/G = sin(theta)
// asin(Ax/G) = theta

    float angle = asin(scaled.XAxis/9.8);
   
 // We output our received data.
    Output(raw, scaled);
    
  }
}

// Output the data down the serial port.
void Output(AccelerometerRaw raw, AccelerometerScaled scaled)
{
   // Tell us about the raw values coming from the accelerometer.
   Serial.print("Raw:\t");
   Serial.print(raw.XAxis);
   Serial.print("   ");   
   Serial.print(raw.YAxis);
   Serial.print("   ");   
   Serial.print(raw.ZAxis);
   
   // Tell us about the this data, but scale it into useful units (G).
   Serial.print("   \tScaled:\t");
   Serial.print(scaled.XAxis);
   Serial.print("G   ");   
   Serial.print(scaled.YAxis);
   Serial.print("G   ");   
   Serial.print(scaled.ZAxis);
   Serial.println("G");
   Serial.print(asin(scaled.XAxis/9.8));
   Serial.print("angle   ");   
   
 }

is this the way you want me to implement the code into my program? Now when the g for the x axis are 0 the print for this Serial.print(asin(scaled.XAxis/9.8)); is also zero. But when I tilt the sensor let say 90 degrees the g = 1 but the angel is 0.1 but it should be 90 :slight_smile: what have I done wrong?

I am only interested in the slope ix axis that is in the horizontal plane i think

What is the reading at 90° and 0° maybe you could just map() it

Perhaps the "scaled" returns a value that is already scaled by 9.8m (returns "g's" instead of m/s^2)
That would be consistent with returning "1" at a 90 degree tilt.
In that case you can leave out the 9.8 and just do asin(scaled.xaxis);
I wasn't planning on reading ALL the datasheets for you...

But the thing is, I removed 9.82 but still it is wrong

But the thing is, I removed 9.82 but still it is wrong

Which leaves us with the question to you of what "wrong" is (what result do you get), and how that differs from "right" (the value you would like to get)?

What are the scaled readings at 0°, 45°, and 90°?

Three people have asked for values that might enable them to help you and you have not answered other than to say 'it's not working'...

here you have two pictures that explain what I mean with wrong.

Serial.print((asin(scaled.XAxis)*180)/3.14);

I wrote this instead now I get angel in degree I think but why is it not 0 and 90 degrees that I get when horizontal and vertical ?

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);
}

Is anybody can help me because I have ridiculous result in serial monitor.

Regards,
JOE

Three things:

  1. Your code doesn't compile
  2. You're missing code tags.
  3. Define (or show) "ridiculous"

This is my full code:

// 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;
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);
}

If there anything missing or wrong. Please correct them out. Thanks.

Ok, so that's 1), now how about 2) & 3) ?

Sorry friend, I have no idea what do you mean by I'm ''missing code tags''? and ''ridiculous'' isn't the code in my programme it was my word :slight_smile:

can you please give me some example of code tags to get me more clear.
JOE