What is it?
An accelerometer is a device that measures acceleration ("g-force"). I bought one from @Pimoronin an Adafuit ADXL345 (a triple axis accelerometer) which costs around £16. Pimoroni provide a really useful python module to get started with the ADXL345. Martin O'Hanlon has a good set of instructions to set up the component and this is based on it as a beginners guide.
1. Getting Started
Firstly it needs connecting up, this is the part which I found the most tricky, once all the pins are soldered in, then connect the cables. There are four connections : GND - GND, 3V - 3V3, SDA - SDA, SCL - SCL. And this is the part where I was stuck! However below are the pin numbers that I used on a B+ model., which means the GND is further down. The pin numbers refer to the physical pin not the GPIO number,
Pin 39: GND - GND,
Pin 1: 3V - 3V3,
Pin 3: SDA - SDA,
Pin 5: SCL - SCL.
2. Preparing the Pi
The ADXL345 uses the I2C bus, which requires you to change and configure a few lines of code on your Raspberry Pi:
Load the LX Terminal and type:
sudo nano /etc/modules
Add the following lines to the end:
Save the code and then remove I2C from the blacklist, comment it out using a hash # , open the following file:
sudo nano /etc/modprobe.d/raspi-blacklist.conf
when it loads change the
blacklist i2c-bcm2708 so it reads #blacklist i2c-bcm2708
Reboot to make the changes, type, sudo reboot
3. Install the Software
Next up install the python modules for the I2C, ensure the Pi is connected to the Internet and load the LX Terminal, type the following:
sudo apt-get install python-smbus i2c-tools git-core
then type sudo reboot
4. Testing the ADXL345
It is possible to test that the I2C is working properly and that the software is installed. The I2C should pick up the accelerometer on address 53.
In the LX Terminal Type:
sudo i2cdetect -y 1
5. Python Module
The ADXL345 requires a Python module to support the programs you will code. This can be downloaded at GitHub but it is below. Save the file into the Raspberry Pi home folder Home/Pi and ensure that it is saved in the same folder where your program will be saved. (Thanks to Jonathan Williamson)
6. Test Programs
Below are two example programs to try, the first measures the change in the X. Y. Z in Gs and the second as the change in acceleration.