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

Breadboard

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).

Advertisements

26 thoughts on “Single and double tap detection with the ADXL345 accelerometer

  1. Thanks for letting me know. Apparently Dropbox has killed public links. I have added new links to the files through Mega.

  2. Hi

    Great tutorial! One small comment though: the link to your sketch does not work anymore. Do you still have the code?

    Regards,
    David

  3. Hi Sami,

    I never looked into that, but if you look in the data sheet there should be a register that allows you to select 1g, 8g or 19g. You would write to the register as you would any other (see the code for how to use the Wire library).

    Regards,

    Barrett

  4. good job , thanks alot , just one easy question . How can I change the working of ADXL345 from 1g to 8g or 16 g , because my sensors in 2g the z-axis does not work .

  5. Hello pawel,

    It has been a very long time since I have worked on this project, but I recall that I would have this problem if there was a loose connection between the Arduino and ADXL345, or between the ADXL345 and the battery/power supply. Either the ADXL345 is not updating the X-Y-Z registers correctly (which is unlikely if the code is running correctly), or a wire is loose and the Wire.h function is timing out because a connection is broken (which returns a -1. Much more likely). Try checking your wires, especially the power supply.

    Let me know if you have any further questions or figure anything out.

    Regards,

    Barrett

  6. same story, I’ve got returning “-1 -1 -1″ in serial monitor, made connection same as on your pictures but adxl345 isn’t work, no taps are detected, have you any what could be wrong if connection is same as your shown on your pictures?

  7. Oliver,

    Yes, I do think an accelerometer could be used, but you may want to combine it with a gyroscope for more accurate and precise movement measurements. If you are intending to build a head-tracking system, you will definitely need both an accelerometer and gyroscope as well as some type of software filter to integrate the sensors’ data (such as a Kalman or complementary filter).

  8. Hi Geetanjali,

    “-1 -1 -1” is what the code returns if it fails to retrieve data from the ADXL345. This could be due to two things. The first is that the Arduino is trying to read from a register on the ADXL345 that is empty. This could be because the ADXL345 was not configured correctly and is therefore not updating its x, y, z data registers. The other possibility is that you have a broken connection between the Arduino and the ADXL345. A symptom of this would be the data updating very slowly (because of timeouts). My code worked perfectly for me, so, besides what I have mentioned, I do not know what your problem could be. Are you sure you’re using an ADXL345 and not another similar variant? If you can provide me with more information I may be able to be of more assistance.

  9. Hello Miles,

    I do not currently, but I would be interested to hear a proposition. Please contact me at my email (banderies [at] gmail [dot] com) if you are still interested. I check this website as much as possible, but email is much more convenient (and I will likely respond much more quickly to email).

  10. Hello Sir,
    The above code is not working with Arduino Uno board.
    I did single tap and double tap of Accelerometer i.e. ADXL345 but at the output , the data is-1 -1 -1 continuosly.
    If I am changing its orientation than also data is not changing and the output is -1 -1 -1.
    Please help me and thanks for sharing code.
    Regards,
    Geetanjali

  11. Hi. Can you write some code so the arduino can read raw values from the accelerometer?

  12. i dont think you need library for an adc bro. check out example at ardunio site for analog read

  13. Do you think there is a library for converting analog into digital for arduino uno and this type of accelerometer?

  14. what i want to detect is the movement of the patient’s head. my 1st idea is to try to have the initial position of the head. and then if any movement that occur more than the threshold, it will trigger an alarm. but i have no idea how to detect the initial position. so i decided to try the interrupt function. your single tap actually detecting how much of G?

  15. Oliver,

    It’s possible, but depending on exactly what type of movement you wish to detect, you may want to try a different method. Are you just trying to detect the start of a movement, or would you like to determine how much movement has occurred (i.e. angle of head rotation)?

  16. just to get an opinion.do you think tap function can be use to detect movement? im going to detect head movement for a paralyze person. because as what i have read from the datasheet. the treshold can be set.

  17. I don’t quite understand your question. I am using breadboard jumper wires. If you’re asking whether or not you can use ONLY I2C and NOT the interrupts, then yes, you can. As I mention in the video, I only used the interrupts to learn how they worked. You can read the interrupt registers in the ADXL345 via I2C to achieve the exact same operation.

  18. Would this be possible with breadboard jumper wire? Or do you have to use the I2C cables and the interrupt wires?

  19. Let me make sure I understand you correctly. You would like me to write a short piece of code that will do exactly what I did in the video, except without the use of my library? If so, are you interested in using interrupts, or just reading the tap-detection registers? Unless you want to practice using interrupts, or really need them, I would suggest just reading the tap registers as it is much easier. Plus, you mention you are interested in understanding I2C protocol, which is completely unrelated to using interrupts.
    I’d be happy to write you some code, although I’m back in school so it may take me awhile to get it to you. Before I do though, have you made any progress? If you’ve already written something that kind of works, I may be able to help you debug it. There is a certain sequence you must request information from the ADXL345 in order to use the tap-detection feature. I think I wrote comments regarding this sequence in the code, but I’m not sure.
    Just let me know if you’ve written anything, and whether you’d like to use interrupts or just read the tap registers, and I can get started.

  20. Hello there, great tutorial!
    I got and ADXL 345 breakout board and I would like to do the same set up like you. However I would like to write the whole code without using any libraries so I can understand how the I2C works. Is it possible for you to rewrite the code for the arduino into a new one without the library?
    That would be awsome becuase I can’t really figure out how to do it by my own. I know how to read values from the accelerometer thanks to this guy and his tutorial http://codeyoung.blogspot.se/2009/11/adxl345-accelerometer-breakout-board.html
    Please help me and thank you for taking your time!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s