Verified by our editorial teamSimulated in Proteus 9.0 SP2

ADXL3xx Accelerometer using an Arduino

Summary of ADXL3xx Accelerometer using an Arduino This tutorial explains how to read an Analog Devices ADXL3xx series accelerometer (e.g., ADXL320, ADXL330) using an Arduino and communicate the data to a computer. The device outputs analog voltage signals for each axis, which are read via the Arduino's `analogRead()` function. The circuit connects the sensor directly […]

Avatar of Ibrar Ayyub
By Ibrar Ayyub
Published August 2, 2013
At a glance
CategoryMetering – Instrument Projects
DifficultyIntermediate
IncludesCode, Diagrams, Datasheets
UpdatedNovember 24, 2026
Reading timeabout 9 min
On this page

Summary of ADXL3xx Accelerometer using an Arduino


This tutorial explains how to read an Analog Devices ADXL3xx series accelerometer (e.g., ADXL320, ADXL330) using an Arduino and communicate the data to a computer. The device outputs analog voltage signals for each axis, which are read via the Arduino's `analogRead()` function. The circuit connects the sensor directly to the Arduino using specific analog pins for power, ground, self-test, and X, Y, Z-axis data.

Parts used in the ADXL3xx Accelerometer Project:

  • Arduino Board
  • ADXL3xx Accelerometer (e.g., ADXL320, ADXL321, ADXL322, ADXL330)
  • Analog input pins on Arduino
  • Digital output pins on Arduino
10 min read

An ADXL3xx accelerometer tells an Arduino how fast it is speeding up or slowing down on three axes, and, when it is held still, which way gravity points. That is enough to build a tilt sensor, a shake detector or a simple motion logger. This page explains how the sensor talks to the Arduino, how to wire it without damaging it, a sketch that turns the readings into g and a tilt angle, and when another sensor is a better choice.

HOW THIS PAGE WAS CHECKED

Datasheet verified by us. The zero-g level (about half the supply), the sensitivity per g and the supply range were checked against the ADXL335 datasheet. Page numbers are quoted where each figure is used, and the datasheets are linked in the resources box below.

Simulated in Proteus 9.0 SP2, run by us. We built the circuit and ran the sketch ourselves in Proteus 9.0 SP2 Professional (Build 40482). The screenshots and the files we used are on this page. The sketch was run on a simulated ATmega328P at 16 MHz with 3.3 V on AREF. Proteus has no ADXL335 model, so fixed voltages stood in for the sensor outputs (1.65 V on X and Y, 1.98 V on Z). The serial monitor printed g: 0.00, 0.00, 1.00 tilt: 0.0 deg, as expected for a board lying flat. This checks the code and the arithmetic; the sensor itself was not simulated. See the screenshots and download the files.

IN SHORT
  • The ADXL3xx family gives one analog voltage per axis (X, Y, Z), so a plain analogRead() is all you need.
  • At rest the sensor reads 0 g on the flat axes and 1 g on the axis pointing up or down, which is how you measure tilt.
  • The bare ADXL335 chip runs on 1.8 to 3.6 V (datasheet p. 3). Do not feed it 5 V unless your breakout has a regulator.
  • For a digital interface or a gyroscope as well, look at the ADXL345 or the MPU-6050 (see the comparison below).
3 axes
X, Y and Z
±3 g
range of the ADXL335
~300 mV/g
sensitivity at 3 V (270 to 330)
Analog
3 outputs, no library
Circuit drawing of an ADXL3xx accelerometer breakout board connected to an Arduino
Circuit drawing, made with Fritzing.

Our Proteus simulation: schematic, screenshots and files

We ran the sketch from this page in Proteus 9.0 SP2 Professional with 3.3 V on AREF. Proteus has no ADXL335 3-axis analog accelerometer model, so three fixed voltages stood in for the X, Y and Z outputs of a board lying flat (1.65 V, 1.65 V and 1.98 V). These are our own screenshots. Click any screenshot to enlarge it.

Proteus 9.0 SP2 schematic of the ADXL335 test with the virtual terminal open
Our Proteus schematic: the ATmega328P, the voltage sources standing in for the sensor, and the virtual terminal.
Virtual terminal printing g: 0.00, 0.00, 1.00 tilt: 0.0 deg
Serial output at 9600 baud: 0, 0 and 1 g, tilt 0 degrees, as expected for a board lying flat.
💾 Download our simulation files (adxl335-accelerometer-proteus-files.zip)
Zip with the compiled .hex, the Arduino sketch, the Proteus project (.pdsprj) and a README. Open the .pdsprj in Proteus 9.0 SP2 or newer, check the Program File path points to the .hex, and press Play.

What this proves: the code, the scaling arithmetic and the wiring. What it does not: the real sensor, its regulator, noise or drift. Only the flat case was run.

How the sensor works

Inside the chip is a tiny mass on springs, etched into silicon. When the chip accelerates, the mass lags behind, and the change in distance changes a capacitance. On-chip electronics turn that into a voltage. The output is ratiometric: at 0 g it sits at about half the supply voltage, and it rises or falls in proportion to the acceleration. On an ADXL335 powered from 3.3 V, that means roughly 1.65 V at 0 g, and with a sensitivity of about 330 mV per g (it scales with the supply) about 0.66 V at −3 g and about 2.64 V at +3 g.

An accelerometer cannot tell gravity from movement. A board lying flat reads 1 g on the Z axis even though nothing is moving. Tilt it and gravity splits between the axes: at 45 degrees, X and Z each read about 0.71 g (1 g times the cosine of 45 degrees). That is why the same sensor works as a tilt meter when it is held still, and as a shake or impact detector when it moves.

The family covers several parts, such as the ADXL320, ADXL321, ADXL322 and ADXL330, and the ADXL335 is the one most breakout boards use today. They differ in range and supply voltage, so read the datasheet of the exact part you have.

What you need

Arduino Uno and USB cable
ADXL3xx breakout board
Breadboard and jumper wires

Two kinds of breakout board are common. SparkFun-style boards have no regulator. The Adafruit ADXL335 breakout also works, but it has a 3.3 V regulator and different pins, so it is wired differently, as described below. For background on what an accelerometer is, see Wikipedia.

Wiring

There are two ways to wire this, depending on the board you have.

ADXL335 breakout VCCGNDX-OutY-OutZ-OutST (leave open) Arduino Uno 5VGNDA0A1A23.3V to AREF Wire the Arduino 3.3V pin to AREF and call analogReference(EXTERNAL) in setup(). Without that call in the sketch, do not connect 3.3V to AREF.

Option A: breakout with a 3.3 V regulator (most current boards)

Breakout pin Arduino pin
VCC 5V
GND GND
X-Out A0
Y-Out A1
Z-Out A2
(optional, better accuracy) 3.3V to AREF

Because the outputs swing between 0 and about 3.3 V, pointing the Arduino’s analog reference at 3.3 V uses the full ADC range instead of only two thirds of it.

Option B: the SparkFun-style board

The sensor draws almost no current, so this option powers it straight from the Arduino’s analog pins, used as digital outputs. That saves a breadboard rail, and the breakout plugs directly onto six adjacent pins:

Arduino pin Breakout function
A0 Self-test
A1 Z axis output
A2 Y axis output
A3 X axis output
A4 Ground (set LOW)
A5 Power (set HIGH)
Schematic of an ADXL3xx breakout board connected to Arduino analog pins A0 to A5
Schematic for the A0 to A5 wiring.

Three details that stop this build working

Check the supply voltage of your exact part. A bare ADXL335 is rated for 1.8 to 3.6 V (datasheet p. 3), so the 5 V that an Arduino pin gives out is too high for it. Option B is only safe on a breakout designed for it, or on a part whose datasheet allows 5 V. If you are not sure, use Option A with a regulated breakout.
AREF and analogReference(EXTERNAL) go together. If you wire 3.3 V to AREF, the sketch must call analogReference(EXTERNAL) before any analogRead(). Forgetting it shorts the Arduino’s internal reference to the 3.3 V pin and can damage the board.
The readings drift between boards. Sensitivity is only specified between 270 and 330 mV per g, and the 0 g point is not exactly half the supply. For good angles, calibrate your own board (see below).

The code

The official Arduino example prints the three raw readings. Our sketch for Option A goes further: it averages ten readings per axis to reduce noise, converts them to g, and works out a tilt angle from gravity.

// duino4projects.com
// ADXL335 breakout, outputs on A0 (X), A1 (Y), A2 (Z)
// 3.3V pin wired to AREF, so the ADC full scale is 3.3 V
const int X_PIN = A0;
const int Y_PIN = A1;
const int Z_PIN = A2;

// Calibration: raw reading at 0 g, and ADC counts per g.
// 0 g is about 1.65 V, which is about 512 counts.
// About 0.33 V per g at a 3.3 V supply and 3.3 V reference is about 102 counts per g.
float zeroG[3]  = {512, 512, 512};
float perG      = 102.0;

int readAxis(int pin) {
  analogRead(pin);              // throw away the first reading
  delay(1);
  long sum = 0;
  for (int i = 0; i < 10; i++) sum += analogRead(pin);
  return sum / 10;
}

void setup() {
  analogReference(EXTERNAL);    // must come before any analogRead()
  Serial.begin(9600);
}

void loop() {
  float gx = (readAxis(X_PIN) - zeroG[0]) / perG;
  float gy = (readAxis(Y_PIN) - zeroG[1]) / perG;
  float gz = (readAxis(Z_PIN) - zeroG[2]) / perG;

  // tilt of the X axis away from flat, using gravity only
  float tilt = atan2(gx, gz) * 180.0 / PI;

  Serial.print("g: ");
  Serial.print(gx, 2); Serial.print(", ");
  Serial.print(gy, 2); Serial.print(", ");
  Serial.print(gz, 2);
  Serial.print("   tilt: ");
  Serial.print(tilt, 1);
  Serial.println(" deg");
  delay(200);
}

Open the Serial Monitor at 9600 baud. Lay the board flat and still: X and Y should be near 0 and Z near 1. To calibrate, note the raw readings with each axis pointing up and then down. Half the sum is that axis’s 0 g value, and half the difference is its counts per g. Put those numbers in the two variables at the top. If you leave AREF unwired, remove the analogReference line. The ADC then uses 5 V as its reference, so on a 3.3 V regulated board 0 g reads about 337 counts (1.65 V) and one g is about 68 counts, so set zeroG and perG to those values and calibrate.

If it does not work

What you see Likely cause What to do
All three values stuck near 0 or 1023 No power or ground to the breakout Check VCC and GND, and that the board is regulated or powered as in Option B
Values jump around Noise or a loose breadboard contact Keep the averaging, shorten wires, add a 0.1 microfarad capacitor close to the sensor
Z reads about 0.8 g or 1.2 g when flat Uncalibrated sensitivity Run the calibration above
Angle is wrong when you move the board Movement adds to gravity Read the angle only when the board is still, or add a gyroscope
Readings flat-line at the maximum Acceleration above the sensor’s range The ADXL335 clips at about 3 g, use a wider-range part

Which accelerometer suits you?

Sensor Interface Good for Watch out for
ADXL335 (this page) 3 analog outputs Learning, tilt, simple shake detection, no library Uses three analog pins, needs calibration, only ±3 g
ADXL345 Digital, I2C or SPI Selectable ranges (up to ±16 g), two pins on I2C Needs a library and a 3.3 V-safe module
MPU-6050 Digital, I2C Accelerometer plus gyroscope, better for angles while moving More code, and angles drift without filtering

Our view: use the ADXL335 to learn how an analog sensor, the ADC and ratiometric output fit together. For a project that has to track angles while it moves, start with an MPU-6050 instead. We cover it in our MPU-6050 tutorial.

Project variants: the same accelerometer family

Related projects: motion and tilt

Quick answers

Why does the sensor read 1 g when it is not moving?
It measures gravity too. A board lying flat reads about 1 g on its Z axis.
Can I connect an ADXL335 straight to 5 V?
Not the bare chip, which is rated 1.8 to 3.6 V. A breakout with a 3.3 V regulator can take 5 V.
What is the self-test pin for?
It makes the sensor move its internal mass electrically so you can check the outputs respond. Leave it unconnected in normal use.
How do I get a tilt angle?
Hold the board still, convert two axes to g, and take atan2 of them, as in the sketch above.

Check your own build

Lay the board flat and still, and note the three readings. Then stand it on each edge in turn: the axis pointing up should read about +1 g and the axis pointing down about −1 g. If any of these is far off, calibrate that axis as described above.

Project resources and downloads

Datasheets and documents for the parts in this build. Page numbers quoted on this page refer to these files.

Share this project
X
LinkedIn
Reddit
WhatsApp

Quick Solutions to Questions related to ADXL3xx Accelerometer Project:

  • How does the ADXL3xx output acceleration data?
    The ADXL3xx outputs the acceleration on each axis as an analog voltage between 0 and 5 volts.
  • Which function is used to read the analog voltage?
    You need the analogRead() function to read the acceleration data.
  • Can other breakout boards be used besides Sparkfun?
    Yes, the Adafruit accelerometer breakout board also works, though it must be wired differently.
  • How much amperage does the accelerometer use?
    The accelerometer uses very little amperage, allowing it to run directly off the Arduino's digital output pins.
  • What pins are used for the self-test feature?
    One of the analog input pins is used as a digital I/O pin for the self-test pin.
  • Which pins are assigned to the X, Y, and Z axes?
    Analog 3 is the x-axis, Analog 2 is the y-axis, and Analog 1 is the z-axis.
  • Where can you find more circuit examples?
    More circuit examples can be found on the Fritzing project page.
  • Who created the original example code?
    The example code was created by David A. Mellis.
Scroll to Top