Skip to content

Instantly share code, notes, and snippets.

@ccamacho
Last active July 5, 2016 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ccamacho/e2498fab9a5ec92317881703c74d5e80 to your computer and use it in GitHub Desktop.
Save ccamacho/e2498fab9a5ec92317881703c74d5e80 to your computer and use it in GitHub Desktop.
How to use the ADXL345 with a Raspberry Pi 3
Shoping list:
Solder: https://www.amazon.es/gp/product/B005I4QCB4 13.40
Solder support: https://www.amazon.es/gp/product/B001BMSBD4 6.30
Solder wire: https://www.amazon.es/gp/product/B000LFTN1G 5.72
Flux: https://www.amazon.es/gp/product/B00CIOVF8W 5.93
JUmper kit: https://www.amazon.es/gp/product/B0144HG2RE 6.99
Raspberry Pi 3: https://www.amazon.es/gp/product/B01CD5VC92 41.95
Raspberyy Pi 3 case: https://www.amazon.es/gp/product/B00W7S1BFG 5.47
GPIO board: https://www.amazon.es/gp/product/B0144HFO0A 12.98
ADXL345: https://www.amazon.es/gp/product/B0151FIBZO 6.99
Total for this project ->105.73
Install:
sudo apt-get install python-smbus
sudo apt-get install i2c-tools
Enable kernel module:
run:
sudo raspi-config
Then enable I2C kernel module in AdvancedOptions -> I2C -> Whould you like the ARM.... -> whould you like it enabled by default.. -> Then reeeebot!
COnnections:
GND - GND
3V - 3V3
SDA - SDA
SCL - SCL
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:
Test.. sudo i2cdetect -y 1
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...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment