Raspberry Pi Data Logger for the ADXL345 Accelerometer

The ADXL345 accelerometer is a handy 3-axis sensor for detecting acceleration in g’s in three dimensions.  The tutorial below shows how it can be used with a smart phone to log data to a CSV file.


Connect and Set Up the Sensor

I have written another tutorial for setting up the sensor.  It does require some soldering.  Check out the tutorial here.

Pair a Phone with BlueDot

I wanted to be able to start taking data when I was ready, so I decided to use the RPis internal BlueTooth to make it happen.  Luckily, BlueDot exists and it is very easy to integrate into a program.  Setting up BlueDot requires a smart device (I used my phone) and the Raspberry Pi.  I’d recommend looking at Docs, which are great.  I’ve summarized it below.

  1. On your phone, download the BlueDot app.
  2. In the RPi’s terminal enter the following code
    sudo apt install python3-dbus
    sudo pip3 install bluedot
  3. Pair the Raspberry Pi with the phone.  This involves the phone and the RPi.


    1. Open Settings
    2. Select Bluetooth and turn it on.
    3. Make your phone discoverable.


    1. Click the Bluetooth icon on the top right of the screen.
    2. Click Bluetooth – Make Discoverable
    3. Click Bluetooth – Add Device
    4. Your phone will appear on the list.  Select it and click Pair.

    Your phone may prompt you to enter the PIN that is on the RPi screen.  Once you enter it, you are paired!

    When you open the BlueDot App, you should see this screen.

To make sure it works you can make a python program called BlueTest.py.

from bluedot import BlueDot
bd = BlueDot()
print("You Pressed the Blue Dot")

Run the program on the RPi.  Open BlueDot and select the RPi.  Press the BlueDot.  When its pressed, you get a printed line of text.

Its very easy!

Program the Raspberry Pi

I went through several iteration, and that may be reflected in my code below.  In short, I wanted something that would take measurements for 60 seconds, while operating headless.  I also wanted to know when it was on.  So here is how the program works.

  1. Plug in the RPi.
  2. A green light flashes three times.
  3. Pair the RPi to my phone.
  4. Open and press the Blue Dot.
  5. Take data for 60 seconds. (Time, X, Y, Z).  Green light stays on during this time.
  6. Stop data collection.
  7. Red light turns on for five seconds.

Here is the device

from gpiozero import LED
from bluedot import BlueDot
from adxl345 import ADXL345
import datetime
import adxl345
import time
import csv
import os

accel = adxl345.ADXL345()
accel.setRange(adxl345.RANGE_4G) #sets the maximum to 4G

led1 = LED(18)
led2 = LED(17)

def blink(): #blinks a light 3 times
  for i in xrange(3):

def millisec(): #counts time in milliseconds
  nowTime = datetime.datetime.now()
  ms = nowTime-startTime
  ms = int(ms.total_seconds()*1000)

def gravity(): #gets the readings and the number of ms elapsed, puts them in a csv file
  timestamp = time.strftime("%H:%M:%S")
  startTime = datetime.datetime.now() #time the experiment starts
  for i in xrange(600): #selected to make 600 readings at 10ms intervals
    axes = accel.getAxes(True)
    nowTime = datetime.datetime.now()
    ms = int((nowTime-startTime).total_seconds()*1000)
    x = axes['x']
    y = axes['y']
    z = axes['z']
    with open('/home/pi/'+timestamp+'templog.csv','a') as csvfile:
      csvwriter = csv.writer(csvfile,delimiter=',',
      quotechar='|', quoting=csv.QUOTE_MINIMAL)

blink() #blink LED1 3 times
bd = BlueDot()

Experiment and Results

I was very pleased with the results.  It took very little time to make a measurement.  Here are a couple examples.

For a long pendulum

For a short pendulum

Comparing the Pendulums

When the length of the pendulum decreases, so does its Period, and the frequency increases.  You can see that when we overlay the net acceleration for both pendulums.

Leave a Reply

Your email address will not be published. Required fields are marked *