Hardware Reference
In-Depth Information
From there, you have to calculate the portions along all
three axes at once. It turns out that:
acceleration = voltage / 0.3;
Once you've got the acceleration for each axis, you can
apply the earlier trigonometric formulas to get the pitch
and roll.
roll = arctan (x-axis / √(y-axis 2 + z-axis 2 ))
and:
The following sketch reads an analog accelerometer's X
and Y axes, calculates pitch and roll as an angle from -90
degrees to 90 degrees, and sends the results out serially.
If you want to use this sketch with an analog accelerom-
eter other than the Adafruit ADXL335 module, rearrange
the pins to match your accelerometer. If you're using a 5V
accelerometer, change the voltage calculation as well.
X
pitch = arctan (y-axis / √(x-axis 2 + z-axis 2 ))
In order to know the acceleration on each axis, though, you
have to convert the reading you get from the analogRead()
command. It's not too difficult. You already know that the
accelerometer's range is from 0 volts to 3.3 volts (because
the accelerometer operates on 3.3 volts), and that it gets
converted to a range from 0 to 1023. So, you can say that:
voltage = analogRead(axis) * 3.3 / 1024;
Each axis has a zero acceleration point at half its range,
or 1.65 volts. So subtract that from your reading, and any
negative reading means tilt in the opposite direction.
From the accelerometer's data sheet, you can find out that
the sensitivity of the accelerometer is 300 millivolts per g,
where 1g is the amount of the acceleration due to gravity.
When any axis is perpendicular to the ground, it experienc-
es 1g of force, and should read about 300 mV, or 0.3V. So,
the acceleration on each axis is the voltage reading divided
by the sensitivity, or:
Listen to It (Analog)
The
setup() method initializes serial com-
munications, and configures the
analog-to-digital converter to take its
reference from the external analog
reference pin.
/*
Accelerometer reader
Context: Arduino
Reads 2 axes of an accelerometer, calculates pitch and roll,
and sends the values out the serial port
*/
NOTE: Use this sketch with the Analog
Devices ADXL320 accelerometer.
void setup() {
// initialize serial communication:
Serial.begin(9600);
// tell the microcontroller to read the external
// analog reference voltage:
analogReference(EXTERNAL);
}
 
Search WWH ::




Custom Search