Here are the charts featuring the latest data I collected from the accelerometer
The good news is that the accelerometer is now working, and I only pulled out a few hairs in the process. Now that this accelerometer is working I have a few additional learnings and resources to share with anyone working on hooking up an accelerometer. I hope these can help you get up and running without any hair pulling:
- Make sure that you have soldered the pins to your accelerometer breakout board before starting to test.
- Use the AREF pin on the Arduino to set the reference voltage to 3v and improve the sensor readings.
- Use a running average of all readings or some other stabilization algorithm to help reduce noise from accelerometer readings.
- Check out the code samples below for different ways to test your new accelerometer.
- Whichever axis is in vertical position will have a different sensor reading due to gravity, even when resting.
- The sensor for each axis is only able to alternate resistance by +-15%.
Code Sample 1 – As Simple as You Can Get
int xAxis = 0;
int yAxis = 1;
int zAxis = 2;
int zInput = 0;
int yInput = 0;
int xInput = 0;
void setup () {
Serial.begin(9600);
}
void loop () {
xInput = analogRead(xAxis);
delay (10);
yInput = analogRead(yAxis);
delay (10);
zInput = analogRead(zAxis);
delay (10);
Serial.print("Inpu (xyz): ");
Serial.print(xInput);
Serial.print(", ");
Serial.print(yInput);
Serial.print(", ");
Serial.print(zInput);
Serial.println(".");
}
Code Sample 2 – Capture Base Readings and Then Report Difference from Base
This sample was developed by Andy Davidson, and taken from the Arduino message boards.
/* ADXL335test6
Test of ADXL335 accelerometer
Andy Davidson
*/
const boolean debugging = true; // whether to print debugging to serial output
const boolean showBuffer = false; // whether to dump details of ring buffer at each
read
const int xPin = 0; // analog: X axis output from accelerometer
const int yPin = 1; // analog: Y axis output from accelerometer
const int zPin = 2; // analog: Z axis output from accelerometer
const int led = 13; // just to blink a heartbeat while running
const int totalAxes = 3; // for XYZ arrays: 0=x, 1=y, 2=z
const int baseSamples = 1000; // number of samples to average for establishing
zero g base
const int bufferSize = 16; // number of samples for buffer of data for running
average
const int loopBlink = 100; // number of trips through main loop to blink led
// array of pin numbers for each axis, so the constants above can be chnaged with
impunity
const int pin [totalAxes] = {
xPin, yPin, zPin};
// base value for each axis - zero g offset (at rest when sketch starts)
int base [totalAxes];
// ring buffer for running average of data, one for each axis, each with
samples
int buffer [totalAxes] [bufferSize];
// index into ring buffer of next slot to use, for each axis
int next [totalAxes] = {
0,0,0};
// current values from each axis of accelerometer
int curVal [totalAxes];
// count of trips through main loop, modulo blink rate
int loops = 0;
void setup() {
long sum [totalAxes]= { // accumulator for calculating base value of each axis
0,0,0 };
Serial.begin (9600);
Serial.println ("***");
// initialize all pins
pinMode (led, OUTPUT);
for (int axis=0; axis
pinMode (pin [axis], INPUT); // not necessary for analog, really
// read all axes a bunch of times and average the data to establish zero g offset
// chip should be at rest during this time
for (int i=0; i
for (int axis=0; axis
sum [axis] += analogRead (pin [axis]);
for (int axis=0; axis
base [axis] = round (sum [axis] / baseSamples);
// and display them
Serial.print ("*** base: ");
for (int axis=0; axis
Serial.print (base [axis]);
Serial.print ("\t");
}
Serial.println ();
Serial.println ("***");
// initialize the ring buffer with these values so the averaging starts off right
for (int axis=0; axis
for (int i=0; i
buffer [axis] [i] = base [axis];
// light up the led and wait til the user is ready to start (sends anything on serial)
// so that the base values don't immediately shoot off the top of the serial window
digitalWrite (led, HIGH);
while (!Serial.available())
/* wait for */ ;
digitalWrite (led, LOW);
}
void loop() {
//increment the loop counter and blink the led periodically
loops = (loops + 1) % loopBlink;
digitalWrite (led, loops == 0);
// get new data from each axis by calling a routine that returns
// the running average, instead of calling analogRead directly
for (int axis=0; axis
curVal [axis] = getVal (axis, showBuffer);
if (debugging) {
Serial.print (curVal [axis]);
Serial.print ("\t");
}
}
if (debugging)
Serial.println ();
// here we will do all of the real work with curVals
}
int getVal (int axis, boolean show) {
// returns the current value on , averaged across the previous
reads
// print details if is true
long sum; // to hold the total for aaveraging all values in the buffer
// read the data into the next slot in the buffer and stall for a short time
// to make sure the ADC can cleanly finish multiplexing to another pin
buffer [axis] [next [axis]] = analogRead (pin [axis]);
delay (10); // probably not necessary given the stuff below
// display the buffer if requested
if (show) {
for (int i=0; i
if (i == next [axis]) Serial.print ("*");
Serial.print (buffer [axis] [i]);
Serial.print (" ");
}
Serial.println ();
}
// bump up the index of the next available slot, wrapping around
next [axis] = (next [axis] + 1) % bufferSize;
// add up all the values and return the average,
// taking into account the offset for zero g base
sum = 0;
for (int i=0; i
sum += buffer [axis] [i];
return (round (sum / bufferSize) - base [axis]);
}
1 comment:
That code from the forums is way more complicated than you need for what you are doing. Better off to write your own.
Post a Comment