Code examples for ADXL345 accelerometer?

Hi' I've recently purchased the ADXL345 accelerometer. But I can't find any code examples of it being used with the arduino, or with anything else for that matter. Please let me know if you have used it or know of an open project where I can see a wiring scheme and/or some Arduino code. Thanks!

Erm, the data sheet ;)

Looks like you can use it with either SPI or I2C, I2C is probably easiest. Read up about the wire library. Once you know how to address an I2C slave, look at the register map on page 14 of the data sheet. Then you can pull whatever information you want out of the chip. :)

Looks like you can use it with either SPI or I2C, I2C is probably easiest.

That sounded a bit Chinese, but let's see if I got it straight. I need to use the Wire library in order to communicate with the sensor, and using I2C umm protocol? would be the easiest option (still have to figure out the slave thing). After I can communicate I just take the values from the registers I need using the table on page 14. Correct?

Correct, or you can write to the registers that need to be written to (looks like a lot of thresholds in the datasheet :slight_smile: )

Basically both the arduino and the adxl345 are both on the same BUS, the arduino is the master and the adxl345 is the slave.

  1. The arduino activates the BUS, all the slaves listen in

2.The arduino writes a slave address and whether it wants to read or write to the BUS

3.All the slaves listen for the address, compare it to their own address and if it’s not equal they stop listening, this means that now only the slave you want is listening to the arduino. The slave then looks at whether you want to read or write and gets into the correct mode

4.The arduino then writes a register address to the BUS, the selected slave reads it and notes down the register address.

5.If the arduino wanted to write it would send the byte and the slave would write it into the noted register, if the arduino wanted to read the slave would send the data from the noted register to the arduino.

edit:Fix’d for being basically unreadable

Woo one sentence record

Classic advice from editor/writing teacher to writers:

The period is your friend!

Ok, read about the Wire library and also read the datasheet. The address of the device is 0xA6 for reading and 0xA7 for writing. Now all I have to do is wire the thing correctly and hit run? I'm kinda stuck at that phase.

On page 10 of the datasheet there's a simple wiring scheme. I'm connecting the CS pin to the arduino 3V out, and the SDO to ground (to get the addresses they mention). Now where do I stick the chip SDA and SDCL is beyond me. The Wire library page says that the arduino SDA is analog in 4, and clock is analog 5. But I'm not sure are they used that way when arduino is the master or the slave. I'm asking lots of questions before the actual plugging because I don't wanna fry the 30$ chip :D

Ha, yeah I know that feeling

You are correct about CS and SDO, also remember you have to connect Vdd and Vss to 3v (isn't it 3.3V?) and the GND pins to GND (page 19 has the power circuit).

It may seem weird that the pins are analog 4 and 5 but that is just where atmel decided to put the hardware for the I2C. SDA and SCL are just one the pins alternate functions.

Thanx a lot for the help! I'll try it as soon as I get back home (in a week :() and I hope I'll get the other chip issues figured out till then, if not, will revive this post :D

Hello, and welcome back to the adxl345 accelerometer saga.

I've plugged the chip according to the datasheet, the schematic of the breakout board and the Wire library reference.

The wiring looks like this:

I discovered very soon that this wiring shorts the arduino! After some experimenting I've narrowed down the shorting wires to this:

Now I'm assuming one of two things: A. The breakout board (or the chip) is defective B. My wiring is awful wrong

Please advise

Hi MeLight, did you check the sparkfun-board SEN-09364 They present the source code with it, take a look into the file ADXL345.c.

(I still do not have neither ADXL nor Arduino, so no knowledge of my own) Good luck! Georg

I've finally managed to do it, and wrote a matching application for Processing. Here're the details adxl345 + arduino + Processing