Introduction: How to Use the ADXL345 on Raspberry Pi

Picture of How to Use the ADXL345 on Raspberry Pi

I have found that there is not much out there to actually find out how to use the ADXL345 on the web, but first, what is an ADXL345, well it is a 3-axis 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.

What you'll need:

1x Raspberry Pi2 Model B

1x PSU

1x Micro SD card 8GB recommended

1x ADXL345 accelerometer you can get one form Adafruit or Pimoroni (WARNING your sensor may look different to mine so don't worry if it's not the same in appearance.)

1x Breadboard

12x Male to Female jumper wires

That's all for the components, now go to the next step for the wiring...

Step 1: Setting Up the Raspberry Pi for the ADXL 345

Picture of Setting Up the Raspberry Pi for the ADXL 345

Wiring up the accelerometer is pretty easy, there are only 4 connections:

Use the GPIO diagram above to help with the wiring

GND - GND

3V - 3V3

SDA - SDA

SCL - SCL

Now comes the hard part

The ADXL345 supports both I2C and SPI connections, I used I2C, which requires some configuration on the Pi:

Add the I2C modules to the Pi's configuration:

sudo nano /etc/modules

Then add the following lines:

i2c-bcm2708
i2c-dev

Remove I2C from the blacklist:

sudo nano /etc/modprobe.d/raspi-blacklist.conf

Change this:

blacklist i2c-bcm2708

To this:

#blacklist i2c-bcm2708

Reboot to make the changes:

sudo reboot

Now go to the next step to install some software...

Step 2: Installing the Stuff

You will need to install smbus:

sudo apt-get install python-smbus i2c-tools git-core

Now test the ADXL345 is found on the I2C bus by running:

sudo i2cdetect -y 1

you should not get any errors and see a device at address 53

Now download from

https://github.com/pimoroni/adxl345-python

the file.

Run the example code to test if the sensor is working by writing tis in the terminal:

cd adxl345-python

Then write:

sudo python example.py

If you get 0.000G for all axis then something probably isn't set up correctly.

Writing your own python program

The program below imports the module, instantiates an ADXL345 object and reads values from the accelerometer as g-forces.

#import the adxl345 module
import adxl345

#create ADXL345 object accel = adxl345.ADXL345()

#get axes as g axes = accel.getAxes(True) # to get axes as ms^2 use #axes = accel.getAxes(False)

#put the axes into variables x = axes['x'] y = axes['y'] z = axes['z']

#print axes print x print y print z

Change the program for fun!

The default range is 2g which means that the maximum G the ADXL345 can measure is 2.048, but at a high degree of sensitivity.

You can change the sensitivity of the ADXL345 by using the .setRange() method of the class.

This is it for the tutorial. If you prefer a video tutorial then go to the next step...

Step 3: The SUNFOUNDER Method

This is done by Sunfounder, it is a bit different but hopefully it will help.

Firstly download their file from:

http://www.sunfounder.com/index.php?c=downloads

From there download the User manual(Super Kit for Pi) and the Code and Frizting(Super kit for Pi)

then follow the youtube video:

and follow the instructions, while doing this I advise you use the instruction booklet which you had downloaded for some additional support.

Hope you have fun like I did!

Comments

helloins (author)2017-10-15

Traceback (most recent call last):

File "example.py", line 105, in <module>

adxl345 = ADXL345()

File "/home/pi/adxl345-python/adxl345.py", line 47, in __init__

self.setBandwidthRate(BW_RATE_100HZ)

File "/home/pi/adxl345-python/adxl345.py", line 55, in setBandwidthRate

bus.write_byte_data(self.address, BW_RATE, rate_flag)

IOError: [Errno 121] Remote I/O error

SalmaF1 (author)2016-03-17

I tried this tuto but i have error while running the "example.py" :

File "example.py", line 5, in <module>

adxl345 = ADXL345()

File "/home/pi/adxl345-python/adxl345.py", line 47, in __init__

self.setBandwidthRate(BW_RATE_100HZ)

File "/home/pi/adxl345-python/adxl345.py", line 55, in setBandwidthRate

bus.write_byte_data(self.address, BW_RATE, rate_flag)

IOError: [Errno 110] Connection timed out

KaranP48 (author)SalmaF12016-11-09

Hey! Did you figured it out?

JelleB6 (author)2016-06-02

my z axis is always stuck at 2.044 G help please

DswW (author)2016-05-08

How to display the shape while you turn? like display the shape when you turn in Matlab? any way to show it in shape instead of just the reading of the 3 axis xyz in NOOBS?

AviV1 (author)DswW2016-05-09

Sorry I won't be able to help you on that, there probably is way, I haven't used Matlab. However I would advise that you go to the Raspberry pi forum. there are thousands of people there who will be able to help! I would advise that you make an account so you can participate in the community. Link:

https://www.raspberrypi.org/forums/viewforum.php?f...

:)

DswW (author)AviV12016-05-09

Okay, tq yar

AviV1 (author)2016-03-18

if this still does not work, like it did for me, try step 3, which is an alternative way to do it, a bit more fiddely but still works the same.

AviV1 made it! (author)2016-03-18

Hmm, I think there are two possible solutions to this. Firstly,
if you go to menu, then to Preferences, you will find an icon called ‘Raspberry
pi configuration’. Click on it and go to
interfaces on the opened up window. Check that the SPI and I2C are enabled. If not,
enable them and apply changes. This will then tell you to reboot the system. Once
you have rebooted, do step 2 again, just ignoring any installing. Check that
you get a response when you type this in the terminal:

sudo i2cdetect –y 1.

Vijayenthiran (author)2015-11-16

How do I connect multiple ADXL 345 to raspberry pi and read the values?

AviV1 (author)Vijayenthiran2015-11-17

Looking at the raspberry pi's GPIO pin layout, I don't think so, but I will research more into thins and tell how to do it if possible. :)

Vijayenthiran (author)AviV12015-11-17

Hey, I just connected two ADXL in parallel and one ADXL SD0 pin to 3.3V and it worked :)

AviV1 (author)Vijayenthiran2015-11-17

Oh great! Can you add a picture of that? (To do that click the I made it icon and upload the dual ADXL pic)

AviV1 (author)2015-08-28

Hope you guys like this as it is my first guide!

About This Instructable

21,312views

20favorites

License:

More by AviV1:how to use the ADXL345 on Raspberry pi
Add instructable to: