The Adafruit ADXL345 Accelerometer and the Raspberry Pi

Lately, I have become interested in doing some more science experiments with the Raspberry Pi.  I decided to start with physics, and some basic pendulum experiments.

Materials

Instructions

Connect the Sensor to the Raspberry Pi

The connections are pretty straightforward.  First, you will need to solder the pins onto the sensor.  There are seven pins, and you will use 4 of them.  Once soldered, make these connections:

Sensor RPi
3v 3v – Pin 1
SDA SDA – Pin 3
SCL SCL – Pin 5
GND Ground – Pin 6

Next we will work on the Pi to get it to recognize the sensor.

Preparing the Pi

You will need to change a couple files on the Pi so it can work with the sensor.

sudo nano /etc/modules

In that file add the following lines

i2c-bcm2708
i2c-dev

Then you need to open another file, and remove I2C from the blacklist.  If you already have I2C enabled for the Pi, you might not have to do this, but it is good to check.

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

Comment out this line

blacklist i2c-bcm2708

it will look like

#blacklist i2c-bcm2708

Reboot the Pi to make the changes.

Install the software

In the command line, type:

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

The Pi will go through some installation.  After that you can check to see that your sensor is connected.

sudo i2cdetect -y 1

If everything works, you should get something like this:

If you see all dashes, something is probably not connected right.  You will want to go back and check it again.

Get a Measurement

To get a measurement, you will need to download some software from pimoroni.  Make sure you are in home directory and use the command below.

sudo git clone https://github.com/pimoroni/adxl345-python.git

This creates a folder called adxl345-python.  In that folder there are a few files, one of which is an example file.

cd adxl345-python
sudo python example.py

That is pretty much it.  Everything else is just getting a program set up to collect the data you want, in the way you want it.  Have fun!

Leave a Reply

Your email address will not be published. Required fields are marked *