The ADXL335 sensor is a three-axis accelerometer: it measures acceleration in the X, Y, and Z axes simultaneously. It is an “analog” sensor, meaning that it reports data back to you through analog input lines.

The sensor is very easy to wire; 5 volts to VCC, ground to GND, and feed each of the outputs to one of the analog input lines on the Arduino. If you only want to use it to measure acceleration in one or two axes, you may simply only wire the ones you want to read.

Image of ADXL335 sensor. It is a small PCB with five pins.

Arduino code to read and Serial.print three successive analog values

This code assumes you’ve plugged the ADXL335’s XOUT into A0, the YOUT into A1, and the ZOUT into A2. When you run this code, the return values will be formatted like:

256 369 301
257 369 310

This sort of formatting can be read by the Arduino IDE’s “Serial plotter” to automatically plot the incoming data on a simple graph.

// set up pin assignments
const int XPIN = A0;
const int YPIN = A1;
const int ZPIN = A2;

void setup() {
  pinMode(XPIN, INPUT);
  pinMode(YPIN, INPUT);
  pinMode(ZPIN, INPUT);
  Serial.begin(9600);
}

void loop() {
  // to save a bit of typing, the Serial.print statements can embed the analogRead in themselves: 
  Serial.print(analogRead(XPIN));
  Serial.print(" ");    // spaces between values to separate them
  Serial.print(analogRead(YPIN));
  Serial.print(" ");
  Serial.println(analogRead(ZPIN));     // line break only needed after the final value
  delay(50);
}