Arduino: Reading the ADXL 3xx Accelerometer
Reading the ADXL 3xx is pretty straightforward — give it power, run lines from the x,y,z pins to three analog pins, and do analogRead()s on the pins.
What’s a bit trickier is dealing with the sensor values. Resting flat on my desk, the x, y, and z values all vary on each read. It’s only by one or two steps, but it’s still noisy and could introduce a lot of jitter into your code. One way of eliminating/reducing noise is to take a running average of sensor readings, then use those to make your decisions.
Here’s an example Arduino sketch that reads the sensors, computes a running average, then displays the previous average, average, and current raw value for each axis.
A couple of related notes:
- The power you give the ADXL needs to be the same as the reference voltage on your Arduino. If you power the ADXL directly from the Arduino, then everything is fine. If you power it from another source, you might need to use the external power reference features on the Arduino.
- While you put power to the ST pin, the ADXL will force all three outputs to the middle value. This is useful for calibrating the ADXL by reading what it things its middle values are for each axis. However, it’s not something the average person will probably care much about, I find it useful as a “is it really working” test while I’m debugging a circuit.
[tags]adxl,arduino[/tags]