Single and double tap detection with the ADXL345 accelerometer

I’ve had an Arduino Uno for almost a year now, but seldom had the time to work with it, and when I did have time, I had too little experience to build anything interesting. Recently however, I’ve been spending a little more time with it and have become proficient enough in the language to construct fun and interesting systems. This project uses the ADXL345 3-axis accelerometer to detect single and double taps, and blink a dual color (red and green) LED depending on whether an sigle or double tap has been detected.

I chose this project to learn how to use two things: Inter-integrated circuit protocol (I2C) and interrupts on the Arduino (see I2C and Interrupts respectively).

The Ariduino has two designated I2C bus pins (data and clock) and the Arduino IDE includes an I2C library (Wire.h, which includes other protocols as well). Also, pins 2 and 3 on the Arduino Uno can be configured as hardware interrupts. The I2C device I use is the ADXL345 3-axis accelerometer (mine is similar to this, but has SPI interface pins as well as I2C pins). Get the ADXL345 datasheet here.

The hardware:

Arduino Uno Rev. 3

ADXL345 3-axis Accelerometer


9v power supply cable

I2C wires (5v, SCL, SDA, 0v)

Interrupt wires

Jumper wires and two-color LED

The Build:

Ground the LED through a 150ohm resistor (the middle pin is ground)

Connect the LED to pins 4 and 5 on the Arduino

Power the breadboard rails with the Arduino and attach the power supply cable

Attach the I2C wires. Connect the power and ground wires to the breadboard power rails and the SDA (data) and SCL (clock) lines to the designated SDA and SCL pins on the Arduino

Attach interrupt wires to pins 2 and 3 on the Arduino

Attach the ADXL345

Finished with 9v power supply connected

The Code:

Because of the large number of functions on the ADXL345, writing a library was necessary to keep everything looking clean. The library is not very well commented, but as the functions are extremely simple it should be easy to follow (if you’ve read the datasheet).

The library can be found here.

The sketch is well commented so it should be relatively easy to follow, but if you have questions about the sketch or library feel free to ask in the comments below.

The sketch can be found here (click to view, right-click and “Save as” to download).