Accelerometers measures the acceleration it experiences due to freefall. This means that an accelerometer will experience 1g (9.8m/s) of acceleration while it is at rest. The axis that experience this acceleration depends on the orientation and positioning of the accelerometer. Because of this 1g of acceleration we must subtract off a bias value that will give us a more useful value. The arduino will convert the analogue voltage to a value from 0-1023, where 512 corresponds to 1g of acceleration. Thus 512 is subtracted from the read in value to normalize the acceleration value.
In this circuit, we have connected each of three outputs of the accelerometer to the analogue inputs of the arudino. Each output corresponds to its respective axis and will be converted into a digital number (0-1023). Also, the accelerometer is wired up to a reference voltage (5V) and a common ground. In this code example, we read in the analogue values from each analogue axis-output and subtract a bias value that normalizes the acceleration data. We then perform a simple delta operation that calculates the total change in acceleration and print out all the data using the serial monitor.
Code: Select all
//use Ardunio to read in ADXL330 accelerometer data
//and use the serial monitor for real-time data analysis
#define xPin 0
#define yPin 1
#define zPin 2
int delta; // for calculation
int x,y,z = 0;
int bias = 512; // normalizing constant
////////////////////////////////////////////////////
// read in analog values for each axis and normalize
void accelRead()
{
x = analogRead(xPin) - bias;
y = analogRead(yPin) - bias;
z = analogRead(zPin) - bias;
}
////////////////////////////////////////////////////
// print out all the accelerometer data
void accelPrint()
{
Serial.print("x = "); Serial.print(x);
Serial.print("\ty = "); Serial.print(y);
Serial.print("\tz = "); Serial.print(z);
Serial.print("\tdelta = "); Serial.print(delta);
Serial.println();
}
void setup()
{
Serial.begin(9600); // set baud rate for serial monitor
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
pinMode(zPin, INPUT);
}
void loop()
{
delta = x + y + z; // add up all the accelerations
accelRead();
delta -= x + y +z; // change in total acceleration
accelPrint();
delay(50);
}