How to Use the ADXL345 on Raspberry Pi
Intro: 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
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!
15 Comments
sumittile 5 years ago
And did not find that line in blacklist
But still I am getting 53 at I2c detect command
I am getting error in file adxl345.py
At line 11,47 and 55 which are adxl345=ADXL345() ,
self.setbandwidthRate(BW_RATE_100HZ)
and
bus.write_byte_data(set.address, BW_RATE, rate_flag)
please help me
jes198 6 years ago
File "example.py", line 105, in
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
can you please help me out
helloins 6 years ago
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 8 years ago
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 7 years ago
Hey! Did you figured it out?
JelleB6 7 years ago
my z axis is always stuck at 2.044 G help please
DswW 7 years ago
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 7 years ago
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 7 years ago
Okay, tq yar
AviV1 8 years ago
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.
Vijayenthiran 8 years ago
How do I connect multiple ADXL 345 to raspberry pi and read the values?
AviV1 8 years ago
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 8 years ago
AviV1 8 years ago
AviV1 8 years ago
Hope you guys like this as it is my first guide!