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.
- 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.
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.
- On your phone, download the BlueDot app.
- In the RPi's terminal enter the following code
sudo apt install python3-dbussudo pip3 install bluedot
- 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.
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.
- Plug in the RPi.
- A green light flashes three times.
- Pair the RPi to my phone.
- Open and press the Blue Dot.
- Take data for 60 seconds. (Time, X, Y, Z). Green light stays on during this time.
- Stop data collection.
- 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.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
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.