loading

// Watch video here: https://www.youtube.com/watch?v=omZ538TElCU

/* Accelerometer connection pins (I2C) to Arduino are shown below:

Arduino Accelerometer ADXL345 A5 SCL A4 SDA 3.3V CS 3.3V VCC GND GND */

#include #include

ADXL345 adxl; //variable adxl is an instance of the ADXL345 library

int x,y,z; int rawX, rawY, rawZ; float X, Y, Z; float rollrad, pitchrad; float rolldeg, pitchdeg;

void setup(){ Serial.begin(9600); adxl.powerOn(); }

void loop(){ adxl.readAccel(&x, &y, &z); //read the accelerometer values and store them in variables x,y,z // Output (x,y,z) on horizontal plane should be approximately (0,0,255) // the following 3 lines is for an offset rawX=x+360; rawY=y+170; rawZ=z+10; X = rawX/521.00; // used for angle calculations Y = rawY/521.00; // used for angle calculations Z = rawZ/521.00; // used for angle calculations rollrad = atan(Y/sqrt(X*X+Z*Z)); // calculated angle in radians pitchrad = atan(X/sqrt(Y*Y+Z*Z)); // calculated angle in radians rolldeg = 180*(atan(Y/sqrt(X*X+Z*Z)))/PI; // calculated angle in degrees pitchdeg = 180*(atan(X/sqrt(Y*Y+Z*Z)))/PI; // calculated angle in degrees // print out values: Serial.print("x: "); Serial.print(x); // raw data without offset Serial.print(" y: "); Serial.print(y); // raw data without offset Serial.print(" z: "); Serial.print(z); // raw data without offset Serial.print(" rawX = "); Serial.print(rawX); // raw data with offset Serial.print(" rawY = "); Serial.print(rawY); // raw data with offset Serial.print(" rawZ = "); Serial.print(rawZ); // raw data with offset Serial.print(" X = "); Serial.print(X); // raw data with offset and divided by 256 Serial.print(" Y = "); Serial.print(Y); // raw data with offset and divided by 256 Serial.print(" Z = "); Serial.print(Z); // raw data with offset and divided by 256

Serial.print("\t Angle according to x axis (Roll(deg)) = "); Serial.print(rolldeg); // calculated angle in degrees Serial.print("\t Angle according to y axis (Pitch(deg)) = "); Serial.println(pitchdeg); // calculated angle in degrees // Serial.print(" Roll(rad) = "); Serial.print(rollrad); // calculated angle in radians // Serial.print(" Pitch(rad) = "); Serial.print(pitchrad); // calculated angle in radians }

Step 1:

<p>Where does one use such a device</p>
<p>Cool project</p>

About This Instructable

1,352views

7favorites

License:

More by shadow501:ACCELOROMETER WITH adxl 345 
Add instructable to: