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…How to use the ADXL345 on Raspberry pi

Step 1: 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…How to use the ADXL345 on Raspberry pi schematic

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!

 

For more detail: How to use the ADXL345 on Raspberry pi




© 2015 Powered By Engineering Projects Team, Raspberry Pi Projects

Scroll to top