Last active
October 25, 2019 02:41
-
-
Save tasasaki-github/c6b0aab240190a6e0635b4bd8e2e6d8b to your computer and use it in GitHub Desktop.
change the first comments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Simple demo of the ADXL345 accelerometer library. Will output the X, Y, Z | |
# axis acceleration values and its FFT values as csv files. | |
# Author: Tatsuya Sasaki | |
# License: Public Domain | |
# Import the ADXL345 module. | |
import Adafruit_ADXL345 | |
import csv | |
import pandas as pd | |
import time | |
import sys | |
import math | |
import scipy | |
from datetime import datetime | |
from scipy.fftpack import fft | |
SAMPLES = 1024 | |
# Max resolution value ( 3.9mg / LSB ) | |
RESOLUTION = 3.9 | |
accel = Adafruit_ADXL345.ADXL345(address=0x53, busnum=2) | |
accel.set_range(Adafruit_ADXL345.ADXL345_RANGE_16_G) | |
accel.set_data_rate(Adafruit_ADXL345.ADXL345_DATARATE_3200_HZ) | |
l = [None] * SAMPLES | |
# start measurement | |
start_time = datetime.now() | |
for i in xrange(SAMPLES): | |
l[i] = accel.read() | |
end_time = datetime.now() | |
# end of measurement | |
time_diff = end_time - start_time | |
duration_sec = time_diff.seconds + (time_diff.microseconds / float(10**6)) | |
sys.stdout.write('Number of samples: ') | |
sys.stdout.write(str(SAMPLES)) | |
sys.stdout.write('measurement takes ') | |
sys.stdout.write(str(duration_sec)) | |
sys.stdout.write(' sec') | |
sps = SAMPLES / duration_sec | |
# raw data output | |
df = pd.DataFrame(l) | |
df.columns = ['x', 'y', 'z'] | |
df *= RESOLUTION | |
df['time'] = pd.Series(scipy.array(range(SAMPLES))*duration_sec/SAMPLES) | |
df = df[['time', 'x', 'y', 'z']] | |
df.to_csv("rawoutput.csv") | |
# FFT output | |
start_time = datetime.now() | |
d = {'x': fft(df['x']), 'y': fft(df['y']), 'z':fft(df['z'])} | |
df_fft = pd.DataFrame(data=d) | |
end_time = datetime.now() | |
df_fft['frequency'] = pd.Series(scipy.array(range(SAMPLES))*sps/SAMPLES) | |
df_fft = df_fft[['frequency', 'x', 'y', 'z']] | |
df_fft.to_csv("fftoutput.csv") | |
time_diff = end_time - start_time | |
duration_sec = time_diff.seconds + (time_diff.microseconds / float(10**6)) | |
sys.stdout.write("FFT takes: ") | |
sys.stdout.write(str(duration_sec)) | |
sys.stdout.write(" sec") | |
sys.stdout.write("Sampling frequency: ") | |
sys.stdout.write(str(sps)) | |
sys.stdout.write(" Hz") | |
Your bus number might not be 2 but 1 (/dev/i2c-1), try to change the line 23 like the following:
accel = Adafruit_ADXL345.ADXL345(address=0x53, busnum=1)
That was so simple! Thank you very much man!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi! Thank you very much for this code.
I am doing a project based on this, but I am new in Python.
I ran the code as it is (I already downloaded the adafruit.adxl345 library), but I get the following error:
Traceback (most recent call last):
File "/home/pi/accelerometer2.py", line 24, in
accel = Adafruit_ADXL345.ADXL345(address=0x53, busnum=2)
File "/usr/local/lib/python3.5/dist-packages/Adafruit_ADXL345/ADXL345.py", line 63, in init
self._device = i2c.get_i2c_device(address, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/Adafruit_GPIO/I2C.py", line 64, in get_i2c_device
return Device(address, busnum, i2c_interface, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/Adafruit_GPIO/I2C.py", line 97, in init
self._bus = Adafruit_PureIO.smbus.SMBus(busnum)
File "/usr/local/lib/python3.5/dist-packages/Adafruit_PureIO/smbus.py", line 105, in init
self.open(bus)
File "/usr/local/lib/python3.5/dist-packages/Adafruit_PureIO/smbus.py", line 130, in open
self._device = open('/dev/i2c-{0}'.format(bus), 'r+b', buffering=0)
FileNotFoundError: [Errno 2] No such file or directory: '/dev/i2c-2'
Would you know how to solve the problem?
Thanks in advance!