Saturday, January 9, 2016

Using ADXL335 Accelerometer with Arduino

Here's a very brief note about an analog accelerometer which I tried recently. It is ADXL335 which is a 3-axis accelerometer and can be very easily used with any microcontroller having three analog interfaces. I used an Arduino Uno board for this purpose. For this work, what I exactly used is a module which contains a ADXL335 chip with correct support circuit components. This module just have five output pins, which are VCC, GND, X, Y and Z.

Here's how we connect ADXL335 module with an Arduino Uno board. Connect the GND pin of ADXL335 with GND of Arduino, VCC pin of ADXL335 with 3.3v pin of Arduino and finally connect the X, Y and Z pins of the ADXL335 with the A0, A1 and A2 pins of the Arduino board. Then we are good to go. We can connect the Arduino board as usual and read the analog pins 0, 1 and 2 using the analogRead() function to get the raw output data of X, Y and Z pins from the accelerometer.

Since the raw data readings from the analog pins provide values within a particular range according to the orientation of the accelerometer, we can get an idea of how it is oriented if we know the maximum and minimum values it can show. Additionally, there are some ways to calculate the exact angle as a degree value using these raw data. Following is such a code I used to see the orientation of the accelerometer in degrees. When you run it, serial monitor will show the orientation as degree values for the three axis.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//Analog read pins
const int xPin = 0;
const int yPin = 1;
const int zPin = 2;

//The minimum and maximum values that came from
//the accelerometer while standing still
//You very well may need to change these
int minVal = 265;
int maxVal = 402;


//to hold the caculated values
double x;
double y;
double z;


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


void loop(){

  //read the analog values from the accelerometer
  int xRead = analogRead(xPin);
  int yRead = analogRead(yPin);
  int zRead = analogRead(zPin);

  //convert read values to degrees -90 to 90 - Needed for atan2
  int xAng = map(xRead, minVal, maxVal, -90, 90);
  int yAng = map(yRead, minVal, maxVal, -90, 90);
  int zAng = map(zRead, minVal, maxVal, -90, 90);

  //Caculate 360deg values like so: atan2(-yAng, -zAng)
  //atan2 outputs the value of -π to π (radians)
  //We are then converting the radians to degrees
  x = RAD_TO_DEG * (atan2(-yAng, -zAng) + PI);
  y = RAD_TO_DEG * (atan2(-xAng, -zAng) + PI);
  z = RAD_TO_DEG * (atan2(-yAng, -xAng) + PI);

  //Output the caculations
  Serial.print("x: ");
  Serial.print(x);
  Serial.print(" | y: ");
  Serial.print(y);
  Serial.print(" | z: ");
  Serial.println(z);

  delay(100);//just here to slow down the serial output - Easier to read
}


Further details of the ADXL335 accelerometer and the description of the above code can be found in the original place from where I learned it. Those places are given as references in the following section.

References:

[1] The original source of the above code I have used.
http://bildr.org/2011/04/sensing-orientation-with-the-adxl335-arduino/

[2] A good place to understand what is an accelerometer, Gyro and how to select a suitable one for a project.
https://www.sparkfun.com/pages/accel_gyro_guide


No comments:

Post a Comment