Out of the box, the ADXL345 is an I2C 3-axis accelerometer with user selectable sensitivity and 10-13 bit resolution (depending on sensitivity). And… It is pretty decent all around, but were not really going to spend much time talking about boring stuff today. Today, we are talking about getting the special features of the ADXL345 up and running. (tap, double tap, free fall, activity and inactivity)
These special detection features have the ability to trigger 2 interrupt pins, making the pin go HIGH the moment one of the events is sensed. You can select what events to watch for, and what interupt pin to trigger when it is sensed (I had issues with INT2, it would not reset). You can take these interrupt pins and connect them to your Arduino to trigger an interrupt. An interrupt, when sensed on the Arduino, pauses the system instantly, even if it is in the middle of something, and responds to that interrupt how ever you have it setup before returning to the normal code. Like a police officer coming down the street with its lights on… Even if you have a green light, you stop and let them go. But using interrupts is a little beyond the scope of this article, so we are going to just keep checking for these actions in code and respond to them when we see them.
Hooking it up
The ADXL345 supports both SPI and I2C, but we are just going to talk I2C here. I2C is a 2-wire serial connection, so you just need to connect the SDA (Data) and SCL (Clock) lines to your Arduino for communication. On your Arduino (everything but the mega) SDA is on analog pin 4, and SCL is on analog pin 5. On an arduino mega, SDA is digital 20, and SCL is digital 21.
Again, because this project is made up of many files, I am using bildr’s fancy bildrCode wrapper. All this example code will do for you now is output the name of the triggered action in the serial terminal. But I left the code in a state that should be pretty obvious how to extend it.
The special features require a lot of setup to get them working, but because this is bildr, we are going to start you off with working code, everything all setup, and spend a little bit explaining why certain things are how they are. To simplify everything in code, we will be using this Arduino library. But I have made a few small changes to it to make this a little easier to do, and to make it work a little better with other libraries, so the new library files are included in the code below.
Once the ADXL345 folder is in there, you can just go ahead and open ADXL345_Example.pde in your Arduino software, and upload it.
The parameters that are set in the code should get you going, but you will most likely find that your project requires something different. Read through the setup code, there are a lot of things that can be changed, from the force needed to activate “activity”, the time before “inactivity” is triggered and the maximum length / minimum intensity of a tap. You can also completely turn off actions. I found that it was hard to trigger tap without triggering action, and double tap most often triggered tap as well. If you are up for it, the documentation on this chip is quite good, and explains a lot of the values in much greater detail (PDF).
Default Library Folder Location
On your Mac:: In (home directory)/Documents/Arduino/libraries
On your PC:: My Documents -> Arduino -> libraries
On your Linux box:: (home directory)/sketchbook/libraries
If you click the download button to the right of “Arduino” you can download the whole thing as a zip, so you dont need to copy all the files.
Im sure you could come up with 101 ways to expand on this, but incase you need some help, here we go: What about a tap dance sensitive floor that caused lights to change with the rhythm of the dance? Shoes that shock you if you haven’t walked in 5 min. Something as simple as a ball that you throw up and only lights up on its way down… Anything you could think of that needs or could incorporate (in)activity, free-fall, or taps, you could find a way to use this to make it happen.
We need your help
bildr is in need of people interested in helping write any sort of blog post for bildr. If you think you would like to help bildr by writing something, supplying code or schematic, or just have an idea for an article you think should be written, please contact us at email@example.com or let us know in the forum.