Simple accelerometer using ADXl345 and raspberrypi
COMPONENTS REQUIRED:
- Raspberrypi 3 or 4 with pre installed raspbian os on it.
- adxl345 3 axis acceleration sensor.
- Breadboard.
- 4 Male to female jumper wires.
- Power cable for the raspberrypi.
ADXL 345 :
The ADXL345 is a low-power, 3-axis MEMS accelerometer modules with both I2C and SPI interfaces. The Adafruit Breakout boards for these modules feature on-board 3.3v voltage regulation and level shifting which makes them simple to interface with 5v microcontrollers such as the Arduino.
The ADXL345 features 4 sensitivity ranges from +/- 2G to +/- 16G. And it supports output data rates ranging from 10Hz to 3200Hz.
Circuit diagram:
- Wire the GND pin of the Accelerometer to Physical Pin 6 (GND) on the Raspberry Pi.
- Wire the VCC pin of the Accelerometer to Physical Pin 1 (3v3) on the Raspberry Pi.
- Wire the SDA pin of the Accelerometer to Physical Pin 3 (SDA) on the Raspberry Pi.
- Wire the SCL pin of the Accelerometer to Physical Pin 5 (SCL) on the Raspberry Pi.
Preparing Rpi to Interface with ADXL345:
- First insure that everything is up to date on your rpi for that use the following command
sudo apt-get update && sudo apt- get upgrade
2 .Next step is to enabling i2c on rpi using rpi configuration tool
sudo raspi-config
3. On this screen, you will see “5 Interfacing Options” menu. You can navigate the raspi-config tools menus by using the arrow keys. Use the ENTER key to select particular options. Now within the interfacing options menu go ahead and select “ I2C“. When asked if you would like to enable the ARM I2C interface, select “<YES>“.
4.After this reboot once your rpi using command
sudo reboot
5.After the rebooting of our rpi we have to install packages for interfacing with acceleration sensor for that use command
sudo apt-get install python3-dev python3-pip python3-smbus i2c-tools -y
6. Then lets see whether our Raspberry Pi can see our ADXL345 Accelerometer. We can do that by running the following command.If you see number that is 53 in between this then your rpi is connected succesfully with adxl345.
sudo i2cdetect -y 1
Output is like this
pi@raspberrypi:~ $ sudo i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: — — — — — — — — — — — — —
10: — — — — — — — — — — — — — — — —
20: — — — — — — — — — — — — — — — —
30: — — — — — — — — — — — — — — — —
40: — — — — — — — — — — — — — — — —
50: — — — 53 — — — — — — — — — — — —
60: — — — — — — — — — — — — — — — —
70: — — — — — — — —
Coding or Python script part:
- First install adafruit adxl34x python library to talk with ADXL345 for this use command
sudo pip3 install adafruit-circuitpython-ADXL34x
2. Then open the text editor using command (you can use any text editor )
sudo nano
3.Then write following python script in that text editor and save using ctrl+x then y with name accelerometer.py
import time
import board
import busio
import adafruit_adxl34x i2c = busio.I2C(board.SCL, board.SDA) accelerometer = adafruit_adxl34x.ADXL345(i2c)
while True:
print(“%f %f %f”%accelerometer.acceleration)
time.sleep(1)
4.Lets run the script using command
sudo python3 accelerometer.py
5.You will see output like this
0.000000 -1.059118 10.434276
0.000000 -1.059118 10.355822
0.000000 -1.059118 10.395049
0.000000 -1.059118 10.395049
0.000000 -1.059118 10.355822
0.000000 -1.019892 10.395049
0.000000 -1.098345 10.355822
So this are orientation of x,y,z direction respectively.
This is a simple accelerometer project using ADXL345 and Rpi. Hope this will help you .In next projects we will see more about ADXL345.
Follow me on instagram :
https://www.instagram.com/tscdevloper/