Accelerometer ADXL335 Fast Test and Angle Test

10616 (2)

Hardware

  1. 3-Axis Accelerometer ADXL335
  2. CIKU
  3. USB Mini B Cable (2.0)
  4. LCD Keypad Shield
  5. Jumper Wire

Software

Please refer to “Getting Started with CIKU” tutorial.

Suggested Reading

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.

Capture

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).

Wiring Supply to ADXL335

10616

Testing ADXL335 Z-axis Response Using Multimeter

Capture

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.

Testing ADXL335 Response Using CIKU

10616 (1)105874173 

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

DSC_0001

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.

[code lang=”c” highlight=””]
#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 xRead=0, yRead=0, zRead=0;
int ave = 10;

for(int i=0; i<ave ; i++)
{
xRead += analogRead(A0);
yRead += analogRead(A1);
zRead += analogRead(A2);
}

xRead = xRead/ave;
yRead = yRead/ave;
zRead = zRead/ave;

lcd_setCursor(0,0);
lcd_printString("X=");
lcd_printNumber(xRead,10);
lcd_write(‘ ‘);

lcd_printString("Y=");
lcd_printNumber(yRead,10);

lcd_setCursor(0,2);
lcd_printString("Z=");
lcd_printNumber(zRead,10);
}
[/code]

Digital Form to Voltage Form

DSC_0001

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.

[code lang=”c” highlight=””]
#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 xRead=0, yRead=0 ,zRead=0;
float xReadVolt=0, yReadVolt=0, zReadVolt=0;

int ave = 10;
for(int i=0; i<ave ; i++)
{
xRead += analogRead(A0);
yRead += analogRead(A1);
zRead += analogRead(A2);
}

xRead = xRead/ave;
yRead = yRead/ave;
zRead = zRead/ave;

xReadVolt = (float)xRead * (VREF/1023.0);
yReadVolt = (float)yRead * (VREF/1023.0);
zReadVolt = (float)zRead * (VREF/1023.0);

lcd_setCursor(0,0);
lcd_printString("X=");
lcd_printFloat(xReadVolt,2);
lcd_write(‘ ‘);

lcd_printString("Y=");
lcd_printFloat(yReadVolt,2);

lcd_setCursor(0,2);
lcd_printString("Z=");
lcd_printFloat(zReadVolt,2);
}
[/code]

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

DSC_0001

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.

[code lang=”c” highlight=””]
int xMinVal = 265;
int xMaxVal = 398;

int yMinVal = 259;
int yMaxVal = 393;

int zMinVal = 281;
int zMaxVal = 413;
[/code]

The sketch:

[code lang=”c” highlight=””]

#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 xRead=0, yRead=0, zRead=0;
int xAng=0, yAng=0, zAng=0;

int ave = 10;
for(int i=0; i<ave ; i++)
{
xRead += analogRead(A0);
yRead += analogRead(A1);
zRead += analogRead(A2);
}

xRead = xRead/ave;
yRead = yRead/ave;
zRead = zRead/ave;

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);
}

[/code]

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

DSC_0001

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.

[code lang=”c” highlight=””]
#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 RAD_TO_DEG = 57.2957795;
double PI = 3.14159265359;

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

int ave = 10;
for(int i=0; i<ave ; i++)
{
xRead += analogRead(A0);
yRead += analogRead(A1);
zRead += analogRead(A2);
}

xRead = xRead/ave;
yRead = yRead/ave;
zRead = zRead/ave;

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);
}
[/code]

Buy

One thought on “Accelerometer ADXL335 Fast Test and Angle Test

Leave a Reply

Your email address will not be published. Required fields are marked *