3,079

7

2

## Introduction: ACCELOROMETER WITH Adxl 345

/* 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

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

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 }

### Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.