2016年2月19日 星期五

在Raspberry 上面使用ADXL345



Step 1: Setting up the Raspberry pi for the ADXL 345

ADXL 345 pin out 圖

 RPI GPIO 圖

The ADXL345 supports both I2C and SPI connections, I used I2C, which requires some configuration on the Pi:
使用I2C 來連線.....

Use the GPIO diagram above to help with the wiring
GND - GND 
3V - 3V3
SDA - SDA
SCL - SCL
check 一下系統有沒有把I2C關掉

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/fbdev-blacklist.conf
Change this:
blacklist i2c-bcm2708
To this:
#blacklist i2c-bcm2708
Reboot to make the changes:
sudo reboot

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

看的出來address 0x53的地方有device....

如果沒有看到0x53...代表你線接錯了...或是i2c關掉了

Now download from
不太會使用gitcore.....  所以我從網頁右上角有個 Download Zip..... 按下... 然後用ftp 傳到RPI...


cd adxl345-python-master
Then write:
sudo python example.py
If you get 0.000G for all axis then something probably isn't set up correctly.

看一下  example.py
from adxl345 import ADXL345

adxl345 = ADXL345()

axes = adxl345.getAxes(True)
print "ADXL345 on address 0x%x:" % (adxl345.address)
print "   x = %.3fG" % ( axes['x'] )
print "   y = %.3fG" % ( axes['y'] )
print "   z = %.3fG" % ( axes['z'] )

解釋如下 :



#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.

這邊還有另外一個sample code

Firstly download their file from:
From there download the User manual(Super Kit for Pi) and the Code and Frizting(Super kit for Pi)

會看到兩個code ...


也是可以參考一下......  

Reference : http://www.instructables.com/id/how-to-use-the-ADXL345-on-Raspberry-pi/

沒有留言:

張貼留言