Bukit Mertajam, Penang

# Accelerometer ADXL335 Fast Test and Angle Test

Hardware

Software

Please refer to “Getting Started with CIKU” tutorial.

Measuring Tilt Angle with Gyro and Accelerometer

Introduction

Accelerometers measure acceleration (measure in g), often caused by motion. ADXL335 will output an analog (voltage) reading correspond to the acceleration acting to it. When they are standing still, the only acceleration the accelerometer senses is due to gravity (1g) pulling down on it.

If a stationary, single-axis accelerometer is oriented so that its measuring axis is horizontal, its output will be 0 g, and it will continue to be 0 g if mounted in an automobile traveling at a constant velocity (no acceleration) on a level road. When the driver presses on the brake or gas pedal, the accelerometer will register positive or negative acceleration (acceleration or deceleration).

Testing ADXL335 Z-axis Response Using Multimeter

When they are standing still facing upward, the only acceleration the accelerometer senses is due to gravity (1g) pulling down on it. This Cytron ADXL335 breakout board represent the 1g output as a voltage which is approximately 2.02V. When they are standing still facing downward, the only acceleration the accelerometer senses is due to gravity (-1g) pulling down on it. This Cytron ADXL335 breakout board represent the -1g output as a voltage which is approximately 1.37V. You can probe the z-axis pin using multimeter to see its output voltage.

Stack together CIKU and LCD keypad shield together. Then using mini jumper connect the following:

• ADXL335 5V and GND pin  to CIKU 5V and GND pin
• ADXL335 x-pin to CIKU A0 pin
• ADXL335 y-pin to CIKU A1 pin
• ADXL335 z-pin to CIKU A2 pin.

The ADC, Analog to Digital Converter

The ADXL335 x,y and z pin output a voltage correspond to g-force acting on it. The value is in analog (voltage) form which then being converted to digital form. This digital value is then being shown on LCD. Upload the following sketch and you will see the output of x,y and z pin in digital form. Try to play with the ADXL335 and see the changing in its output value. Do capture/take note on each axis maximum value and minimum value because you will need it later.

Note: The maximum value represent the 1g and the minimum value represent the -1g.

```#include "Arduino.h"
#include "LiquidCrystal.h"
#include <math.h>

void setup()
{
pinMode(LED, OUTPUT); //onboard LED as output
pinMode(SW, INPUT); //onboard SW as input
lcd_4bit(8, 9, 4, 5, 6, 7); // RS, E, D4, D5, D6, D7
lcd_begin(16, 2);
}
void loop()
{
int ave = 10;

for(int i=0; i<ave ; i++)
{
}

lcd_setCursor(0,0);
lcd_printString("X=");
lcd_write(' ');

lcd_printString("Y=");

lcd_setCursor(0,2);
lcd_printString("Z=");
}
```

Digital Form to Voltage Form

The digital form can be converted to the voltage form by using the formula of voltageValue = digitalValue x (VREF/1023). Upload the following sketch and you will see the output of x,y and z pin in voltage form. This voltage value are the voltage output at the x,y and z pin of ADXL335. Its the same as you probing a multimeter to each of x,y and z pin manually. Try to play with the ADXL335 and see the changing in its output value.

```#include "Arduino.h"</span>
#include "LiquidCrystal.h"
#include <math.h>

void setup()
{
pinMode(LED, OUTPUT); //onboard LED as output
pinMode(SW, INPUT); //onboard SW as input

lcd_4bit(8, 9, 4, 5, 6, 7); // RS, E, D4, D5, D6, D7
lcd_begin(16, 2);
}

void loop()
{
float VREF = 5.0;

int ave = 10;
for(int i=0; i<ave ; i++)
{
}

lcd_setCursor(0,0);
lcd_printString("X=");
lcd_write(' ');

lcd_printString("Y=");

lcd_setCursor(0,2);
lcd_printString("Z=");
}
```

Digital Form to Angle Form (90 degree to -90 degree)

The digital form can be converted to the angle form by using the map() funtion. Upload the following sketch and you will see the output of x,y and z pin in anlge form. This angle values are just representing the g value. 1g will be represented by 90 degree while -1g will be represented by -90degree. Try to play with the ADXL335 and see the changing in its output value.

Do take note to change this maximum and minimum value part with your ADC value that you take note on ADC section.

```int xMinVal = 265;
int xMaxVal = 398;

int yMinVal = 259;
int yMaxVal = 393;

int zMinVal = 281;
int zMaxVal = 413;
```

The sketch:

```#include "Arduino.h"
#include "LiquidCrystal.h"
#include <math.h>

void setup()
{
pinMode(LED, OUTPUT); //onboard LED as output
pinMode(SW, INPUT); //onboard SW as input

lcd_4bit(8, 9, 4, 5, 6, 7); // RS, E, D4, D5, D6, D7
lcd_begin(16, 2);
}

void loop()
{
float VREF = 5.0;

int xMinVal = 265;
int xMaxVal = 398;

int yMinVal = 259;
int yMaxVal = 393;

int zMinVal = 281;
int zMaxVal = 413;

int xAng=0, yAng=0, zAng=0;

int ave = 10;
for(int i=0; i<ave ; i++)
{
}

xAng = map(xRead, xMinVal, xMaxVal, -90, 90);
yAng = map(yRead, yMinVal, yMaxVal, -90, 90);
zAng = map(zRead, zMinVal, zMaxVal, -90, 90);

lcd_setCursor(0,0);
lcd_printString("X=");
if(xAng<0)
{
lcd_write('-');
xAng = xAng*(-1);
}
else lcd_write('+');
lcd_printNumber(xAng,10);
lcd_write(' ');

lcd_setCursor(6,0);
lcd_printString("Y=");
if(yAng<0)
{
lcd_write('-');
yAng = yAng*(-1);
}
else lcd_write('+');
lcd_printNumber(yAng,10);

lcd_setCursor(0,2);
lcd_printString("Z=");
if(zAng<0)
{
lcd_write('-');
zAng = zAng*(-1);
}
else lcd_write('+');
lcd_printNumber(zAng,10);
}

```

Angle Form (90 degree to -90 degree) to Angle Form (0 degree to 359 degree)

The angle form (90 degree to -90 degree) can be converted to the angle form (0 degree to 359 degree) by using arc tangent 2 function in math.h library. Upload the following sketch and you will see the output of x,y and z pin in angle form (0 degree to 359 degree). Try to play with the ADXL335 and see the changing in its output value.

Do take note to change this maximum and minimum value part with your ADC value that you take note on ADC section.

```#include "Arduino.h"
#include "LiquidCrystal.h"
#include <math.h>

void setup()
{
pinMode(LED, OUTPUT); //onboard LED as output
pinMode(SW, INPUT); //onboard SW as input
lcd_4bit(8, 9, 4, 5, 6, 7); // RS, E, D4, D5, D6, D7
lcd_begin(16, 2);
}

void loop()
{
float VREF = 5.0;
int xMinVal = 265; int yMinVal = 259;int zMinVal = 281;
int xMaxVal = 398; int yMaxVal = 393;int zMaxVal = 413;
double PI = 3.14159265359;

int xAng=0, yAng=0, zAng=0;
double x=0, y=0, z=0;

int ave = 10;
for(int i=0; i<ave ; i++)
{
}

xAng = map(xRead, xMinVal, xMaxVal, -90, 90);
yAng = map(yRead, yMinVal, yMaxVal, -90, 90);
zAng = map(zRead, zMinVal, zMaxVal, -90, 90);

x = RAD_TO_DEG * (atan2(-yAng, -zAng) + PI);
y = RAD_TO_DEG * (atan2(-xAng, -zAng) + PI);
z = RAD_TO_DEG * (atan2(-yAng, -xAng) + PI);

lcd_setCursor(0,0);
lcd_printString("X=");
lcd_printNumber(x,10);
lcd_write(' ');

lcd_setCursor(6,0);
lcd_printString("Y=");
lcd_printNumber(y,10);

lcd_setCursor(0,2);
lcd_printString("Z=");
lcd_printNumber(z,10);
}
```