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");
}
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>
// 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 what have I done wrong?
I am only interested in the slope ix axis that is in the horizontal plane i think
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
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)?
// 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 = ");