element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
STEM Academy
  • Learn
  • Learning Center
  • STEM Academy
  • More
  • Cancel
STEM Academy
Blog A Raspberry Pi Data Logger for the ADXL345 Accelerometer
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join STEM Academy to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: mikedavis
  • Date Created: 30 Jul 2018 5:44 PM Date Created
  • Views 4178 views
  • Likes 5 likes
  • Comments 3 comments
  • bluetooth
  • bluedot
  • stem
  • python
  • education
  • accelerometer
  • raspberry pi
  • physics
Related
Recommended

A Raspberry Pi Data Logger for the ADXL345 Accelerometer

mikedavis
mikedavis
30 Jul 2018

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.

Materials

  • Raspberry Pi
  • Male to Male Jumper Cables
  • RPi Breakout Kit (includes breadboard and cables)
  • ADXL345 Accelerometer

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.

 

image

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
  1. Pair the Raspberry Pi with the phone.  This involves the phone and the RPi.

    Phone:

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

    RPi:

    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.

 

image

 

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

 

 

from bluedot import BlueDot
bd=BlueDot()
bd.wait_for_press()
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:

 

image

 

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

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

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

def blink():
 for i in xrange(3):
   led1.on()
   time.sleep(1)
   led1.off()
   time.sleep(1)

def millisec():
  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()
  for i in xrange(600):
    led1.on()
    axes = accel.getAxes(True)
    nowTime = datetime.datetime.now()
    ms = int((nowTime-startTime).total_seconds()*1000)
    x = axes['x']
    y = axes['y']
    z = axes['z']
    print(ms,x,y,z)
    time.sleep(0.1)
    led1.off()
    with open('/home/pi/'+timestamp+'templog.csv','a') as csvfile:
      csvwriter = csv.writer(csvfile,delimiter=',',
      quotechar='|', quoting=csv.QUOTE_MINIMAL)
      csvwriter.writerow([ms,x,y,z])

blink() #blink LED1 3 times
bd = BlueDot()
bd.wait_for_press()
gravity()
led2.on()
time.sleep(5)
led2.off()

 

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

 

image

 

For a short pendulum

image

 

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.

image

  • Sign in to reply

Top Comments

  • DAB
    DAB over 7 years ago +1
    Could you provide a picture of your test pendulum so I can see how you mounted the accelerometer? DAB
  • 14rhb
    14rhb over 7 years ago +1
    Mike, The Bluedot app looks really useful and simple (or at least it appears to be from your interesting blog) so thanks for drawing to my attention. Rod
  • genebren
    genebren over 7 years ago +1
    Interesting project. This would make an interesting school physics project to show real-time data in experiments. Keep up the good work! Gene
  • genebren
    genebren over 7 years ago

    Interesting project.  This would make an interesting school physics project to show real-time data in experiments.

    Keep up the good work!

    Gene

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • 14rhb
    14rhb over 7 years ago

    Mike,

     

    The Bluedot app looks really useful and simple (or at least it appears to be from your interesting blog) so thanks for drawing to my attention.

     

    Rod

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • DAB
    DAB over 7 years ago

    Could you provide a picture of your test pendulum so I can see how you mounted the accelerometer?

     

    DAB

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube