Using The ADXL335 Accelerometer With Arduino

Posted on Wed 20 October 2010 in posts

Setting up the DXL335 triple axis accelerometer is pretty straight forward. First I soldered 6 straight breakaway headers into the ADXL335 in order to easily plug the breakout board directly into the analog pins in the Arduino.

Once soldered I plugged the accelerometer in to the following pins:

Breakout Pin Analog Pin
Self-Test 0
Z-Axis 1
Y-Axis 2
X-Axis 3
Ground 4
VCC 5

Once the accelerometer was in place it was time to test.

    /*
    This short program reads data from the  ADXL335 axis accelerometer
    */
    const int groundpin = 18;             // analog input pin 4 -- ground
    const int powerpin = 19;              // analog input pin 5 -- voltage
    const int xpin = A3;                  // x-axis of the accelerometer
    const int ypin = A2;                  // y-axis
    const int zpin = A1;                  // z-axis

    const String XHEADER = "X: ";
    const String YHEADER = "Y: ";
    const String ZHEADER = "Z: ";
    const String TAB = "\t";

    void setup()
    {
        // initialize the serial communications:
        Serial.begin(9600);
        // Power and ground the necessary pins. Providing power to both
        // the analog and digital pins allow me to just use the breakout
        // board and not have to use the normal 5V and GND pins
        pinMode(groundpin, OUTPUT);
        pinMode(powerpin, OUTPUT);
        digitalWrite(groundpin, LOW);
        digitalWrite(powerpin, HIGH);
    }

    void loop()
    {
        // print values that are recieved from the sensors and put a tab between
        // the values
        Serial.print(XHEADER + analogRead(xpin) + TAB);
        Serial.print(YHEADER + analogRead(ypin) + TAB);
        Serial.print(ZHEADER + analogRead(zpin));
        Serial.println();
        delay(200);
    }

Using this code I was able to get the following output from the board when it was set on a flat surface:

X: 500 Y: 512 Z: 594

These values will represent the baseline for detecting movement.