Hardware Reference
In-Depth Information
Regardless of the accelerometer you're using,
you'll find that the angle readings aren't always
accurate, and that they can be quite noisy. The
calculations explained above assume there are no forces
acting on the accelerometer besides gravity, but that is
seldom the case. As you move the accelerometer through
space, your movement accelerates and decelerates,
adding more force along all three axes to the calculation.
Generally, accelerometer data is combined with data from
gyrometers to help adjust for these forces.
If you're using the accelerometer on the LSM303DLH
compass from the earlier project, you're in luck. The
Arduino library for that accelerometer does the pitch and
roll calculations for you, and simply returns the results as
pitch and roll. The sketch below reads them and returns
the values as angles from -90 degrees to 90 degrees, just
like the analog accelerometer sketch above.
X
Listen to It (I2C)
This
sketch
reads the LSM303DLH accelerome-
ter's X and Y axes and sends the results
out serially. Like the previous project, it
uses the LSM303DLH library, so make
sure you have it installed.
/*
I2C accelerometer
Context: Arduino
Reads an ST Microelectronics LSM303DLH compass and prints
the X and Y axes accelerometer output.
*/
// include the necessary libraries:
#include <LSM303DLH.h>
#include <Wire.h>
NOTE: This sketch uses the accelerometer
on the LSM303DLH digital compass.
// initialize the compass:
LSM303DLH compass;
void setup() {
// initialize serial and Wire, and enable the compass:
Serial.begin(9600);
Wire.begin();
compass.enable();
// calibrate for the first five seconds after startup:
while (millis() < 5000) {
compass.calibrate();
}
}
void loop() {
// read the compass and print the accelerometer
// X and Y readings:
compass.read();
Serial.print(compass.pitch()); // X axis angle
Serial.print(",");
Serial.println(compass.roll()); // Y axis angle
delay(100);
}
 
Search WWH ::




Custom Search