This mini-project covers the functionality of an ADXL345 Triple-Axis Accelerometer on an Arduino UNO board and uses Processing 3 to visualize the 3D animation control. The x, y, and z values obtained by the accelerometer will be transformed into a 3D object animation where it mimics the movements of an accelerometer. The Arduino UNO communication protocol used is the Inter-integrated Circuit (I2C) protocol.

Hardware Components

Software Components

Application Discussion

What is an ADXL345 Accelerometer and how does it work?

The ADXL345 is a small, thin, low power, 3-axis MEMS accelerometer with high resolution (13-bit) measurement at up to +/-16 g. Digital output data is formatted as 16-bit two’s complement and is accessible through either an SPI (3- or 4-wire) or I2C digital interface.

Accelerometers are devices that measure acceleration, which is the rate of change of the velocity of an object. They measure in meters per second squared (m/s2) or in G-forces (g). A single G-force here on planet Earth is equivalent to 9.8 m/s2, but this will vary due to elevation and gravitational pull variations. Accelerometers are useful for sensing vibrations in systems or for orientation applications.

They are electromechanical devices that sense either static (gravity) or dynamic (vibrations and movement) forces of acceleration.

Accelerometer axes of measurement
Photo Credit: SparkFun

Most of the accelerometers used today measure 3 axes since the cost of development for them decreases.

In general, accelerometers contain capacitive plates internally. Some of these are fixed, while others are attached to minuscule springs that move internally as acceleration forces act upon the sensor. As these plates move in relation to each other, the capacitance between them changes. The changes in capacitance determine the acceleration.

Other accelerometers can be centered around piezoelectric materials. These tiny crystal structures output electrical charge when placed under mechanical stress (e.g. acceleration).

Image result for piezoelectric accelerometer gif
Photo Credit: Wikimedia

                                          An internal structure of a piezoelectric accelerometer

How To Connect to an Accelerometer (Communication Protocols)

Accelerometers will communicate over an analog, digital, or pulse-width modulated connection interface.

  • Accelerometers with an analog interface show accelerations through varying voltage levels. These values generally fluctuate between ground and the supply voltage level. An ADC on a microcontroller can then be used to read this value. These are generally less expensive than digital accelerometers. This communication interface is more susceptible to noise.
  • Accelerometers with a digital interface can either communicate over SPI or I2C communication protocols. These tend to have more functionality and be less susceptible to noise than analog accelerometers.
  • Accelerometers that output data over pulse-width modulation (PWM) output square waves with a known period, but a duty cycle that varies with changes in acceleration. [1]

    How To Power An Accelerometer?

Some triple-axis accelerometers have a logic level converter in their internal circuitry. You’ll know if it has an addition 3.3V pin beside the VCC pin. I used an accelerometer with no internal logic level converter so I have to use a separate one if I intend to use SPI protocol.

NOTE: If you plan on using the ADXL345 in I2C mode, you don’t need a Logic Level Converter since there are pull-up resistors to power up Arduino pins to 3.3V.

    Technical Details (ADXL345)

  • 2.0 – 3.6 VDC Supply Voltage
  • Ultra Low Power: 40uA in measurement mode, 0.1uA in standby@ 2.5V
  • Tap/Double Tap Detection
  • Free-Fall Detection
  • SPI and I2C interfaces

The last three details listed above are the additional features of the ADXL345 Triple-Axis Accelerometer. Tap sensing detects single and double taps in any direction. Freefall sensing detects if the device is falling. These functions can be mapped individually to either of two interrupt output pins. An integrated memory management system with a 32-level first in, first out (FIFO) buffer can be used to store data to minimize host processor activity and lower overall system power consumption.


  • Handsets
  • Medical Instrumentation
  • Gaming and Pointing Devices
  • Industrial Instrumentation
  • Personal Navigation Devices
  • Hard Disk Drive (HDD) protection

To further research about the functionality of an ADXL345 Triple-Axis Accelerometer, here is the link to the datasheet: ADXL345 datasheet

What is Processing 3?

Image result for processing 3

Processing was initially created to serve as a software sketchbook and to teach programming fundamentals within a visual context, Processing has also evolved into a development tool for professionals. The Processing software is free, open source, and runs on the Mac, Windows, and GNU/Linux platforms. The Processing community has written more than a hundred libraries to facilitate computer vision, data visualization, music composition, networking, 3D file exporting, and programming electronics.

Processing is geared toward creating visual, interactive media, so the first programs start with drawing. Students new to programming find it incredibly satisfying to make something appear on their screen within moments of using the software. This motivating curriculum has proved successful for leading design, art, and architecture students into programming and for engaging the wider student body in general computer science classes. [5]

In this project, Processing 3 was used to visualize a 3D object that will be controlled by the accelerometer using Java language.

Set-up the Hardware

Before we can use the accelerometer, we have to calibrate it first to determine the stable reference force for both directions in each of the three axes.

    Calibration Method

  1. Assemble the hardware components as shown above. Don’t place the accelerometer on the breadboard yet. You can also place the sensor on the breadboard but you will need a flat surface to stick the breadboard on it. Note that it won’t be as accurate if you place the sensor on a block of wood.
  2. Mount the sensor on a small box or a block of wood and place it on a firm, flat surface.

    Photo Credit: Adafruit
  3. Copy and upload the Arduino code below for the accelerometer calibration. Open the serial monitor and wait for the prompt.
  4. Position the block so that the arrow of the axis in the sensor is pointing upward (positive axis) or pointing downward (negative axis).
  5. Type any character on the serial monitor and hit return or press enter. The sketch will take a measurement of that axis and print the results.
  6. Repeat the steps 4 to 5 for the rest of the 5 sides. Your sample result for all six sides will look like this.
  7. Record the data for acceleration minimums and maximums in this way. 
  8. To solve for the offset and gain, use the following equations as stated in the Application Note from Analog Devices (page 8: equations 17 and 18).
alt text

This is my sample table from the data obtained in the serial monitor above.

You will place the new values of the offset and gain in the DEFINED VARIABLES part of the code. It was initially set to 0 (for offset) and 1 (for gain).

Placing the new offset and gain values, the DEFINED VARIABLES will look like this.

You have to uncomment this part of the code so that you will see the new calibrated calues for x, y, and z.

Now, if you re-do steps 3 to 5, the new calibrated values will look like this. The 0 0 1 on the first output below indicates that the z-axis has been calibrated to 1 g or 1 gravitational force. All x, y, z values must have a calibrated value of 1 before continuing to the next steps.

    Hardware Assembly and Software

  1. Assemble the hardware components as shown above (either on the breadboard or still on the block of wood).
  2. Copy the Arduino code below and click the Upload button. You can verify if the accelerometer is working by clicking the serial monitor and observing if there are variations in the x, y, z
  3. Close the serial monitor so that the Processing 3 visuals will run.
  4. Copy the processing 3 code below and click the run button. You can now move your sensor and the 3D animation will mimic it.

NOTE: Make sure that the serial monitor is closed before clicking run on the Processing 3. make sure that the Processing 3 animation window is closed before opening the serial monitor.


    Libraries used (Arduino)

SparkFun_ADXL345.h Library by SparkFun

This library by SparkFun has all the ADXL345 features and functionalities defined in the Library. These are shown in pp 20-22 of the ADXL345 datasheet, which are the INTERRUPTS, DATA_READY, SINGLE_TAP, DOUBLE_TAP, FREE_FALL, and many more.

  Wire.h by Arduino

This Library allows you to communicate with I2C/TWI devices. On the Arduino boards with the R3 layout (1.0 pinout), the SDA (data line) and SCL (clock line) are on the pin headers to close to the AREF pin. This library is already in the default libraries on the Arduino sketch tab. [2]

    Libraries used (Processing 3)


OpenGL (Open Graphics Library) is a cross-platform graphics interface for 3D and 2D graphics. This library allows Processing programs to utilize the speed of an OpenGL accelerated graphics card. This expands the potential for drawing more to the screen and creating larger windows.  [3]


The Serial library reads and writes data to and from external devices one byte at a time. It allows two computers to send and receive data. This library has the flexibility to communicate with custom microcontroller devices and to use them as the input or output to Processing programs. The serial port is a nine pin I/O port that exists on many PCs and can be emulated through USB. [4]

     Arduino code (sensor calibration)

The code itself is already understandable with the aid of the comments beside the code functions.

Arduino code (ADXL345 to Arduino to Serial port) by Live Fast – Code Young

Processing 3 code (3D Animation Control) by Live Fast – Code Young


There you have it! A fun project in using the ADXL345 accelerometer in controlling 3D objects. This project is usually used in gaming control and navigation devices. You now know the basics of gaming and navigation by using this sensor. You can also try experimenting it by adding a servo so that it will also mimic the sensor along the 3D animation on your computer screen.







Tags : 3d3d animationaccelerationaccelerometeradxl345arduinocalibrationgaingravityi2coffsetprocessingprocessing 3sparkfun_adxl345.hspitriple-axis accelerometerwire.h

Leave a Response