Overview
In this guide, we will explore the process of interfacing the ADXL345 3-axis Accelerometer with the Raspberry Pi Pico using MicroPython code. The Raspberry Pi Pico is a cost-effective, flexible, and powerful microcontroller that pairs well with the ADXL345, a high-resolution and low-power digital accelerometer. Together, these components can be used to develop a wide range of fascinating projects, such as motion-based control systems, orientation sensors, or even creative game controllers.
Throughout this guide, we will offer you detailed instructions on how to connect the Raspberry Pi Pico and the ADXL345 accelerometer. We will also guide you through the process of writing the necessary code to acquire and process the accelerometer data. By the end of this tutorial, you will have gained a solid understanding of both the Raspberry Pi Pico and the ADXL345 accelerometer, as well as the skills needed to create your own custom projects using these components.
You may take a look at the MPU6050 and ADXL335 in case you need some other motion sensors.
Components Required
We need the following components for this guide.
| S.N. | Components | Quantity | Purchase Link |
|---|---|---|---|
| 1 | Raspberry Pi Pico | 1 | Amazon | AliExpress |
| 2 | ADXL345 3-Axis Accelerometer | 1 | Amazon | AliExpress |
| 3 | Breadboard | 1 | Amazon | AliExpress |
| 4 | Connecting Wires | 1 | Amazon | AliExpress |
ADXL345 Digital Acccelerometer
The ADXL345 is a 3-axis accelerometer developed by Analog Devices. This sensor is designed to accurately measure acceleration in the X, Y, and Z axes, making it suitable for various applications. It is particularly popular in the fields of robotics, gaming, virtual reality, wearables, and IoT devices, among others.
This accelerometer is capable of detecting both static acceleration, such as the force of gravity, and dynamic acceleration resulting from motion or vibrations. The ADXL345 has adjustable sensitivity, enabling it to cater to a range of applications with different acceleration requirements.
The ADXL345 communicates with microcontrollers using either I2C or SPI interfaces, which makes it compatible with a wide range of development platforms, such as Arduino, Raspberry Pi Pico, and others. Thanks to its low power consumption and small form factor, the ADXL345 is ideal for battery-powered and space-constrained applications.
Specifications of ADXL345
The ADXL345 is a triple-axis accelerometer with the following key specifications:
- Axes: 3-axis (X, Y, Z)
- Sensitivity: User-selectable, ±2g, ±4g, ±8g, or ±16g
- Resolution: 10-bit or 13-bit, user-selectable
- Output data rate (ODR): 0.10 Hz to 3200 Hz
- Communication interfaces: I2C (up to 400 kHz) and SPI (4-wire and 3-wire, up to 5 MHz)
- Supply voltage range: 2.0 V to 3.6 V
- Current consumption: 23 µA to 130 µA, depending on output data rate and measurement mode
- Operating temperature range: -40°C to +85°C
- Package: 3 mm × 5 mm × 1 mm LGA
Pinout of ADXL345
The ADXL345 3-axis Digital Accelerometer Module has 8 pins.
- GND: Ground connection for the module.
- VCC: Power supply input, typically 3.3V, but it can accept a voltage range from 2.0V to 3.6V
- CS (Chip Select): Used for SPI communication. When pulled low, it enables SPI communication; when pulled high, it switches to I2C communication.
- INT1 (Interrupt 1): This pin can be configured to output interrupt signals for certain events like activity/inactivity detection, single/double-tap detection, or free-fall detection.
- INT2 (Interrupt 2): This pin can be configured to output interrupt signals for certain events like activity/inactivity detection, single/double-tap detection, or free-fall detection.
- SDO (Serial Data Out): Master In Slave Out (MISO) line for SPI communication.
- SDA (Serial Data): Serial Data (I2C)/Serial Data Input (SPI 4-Wire)/Serial Data Input and Output (SPI 3-Wire).
- SCL (Serial Clock): Serial Communications Clock. SCL is the clock for I2C, and SCLK is the clock for SPI.
Interfacing ADXL345 Accelerometer with Raspberry Pi Pico
To use the ADXL345 accelerometer with a Raspberry Pi Pico using MicroPython, you’ll need to connect the module to the Pico and write a MicroPython script to communicate with the sensor. Here’s a simple example using I2C communication. First connect as per circuit diagram.
Connect the ADXL345 to the Raspberry Pi Pico as follows:
- VCC to 3.3V (Pin 36)
- GND to GND (Pin 38)
- SDA to SDA (GP20, Pin 26)
- SCL to SCL (GP21, Pin 27)
- CS to 3.3V (This is to use I2C mode)
- SD0 to GND (This sets the I2C address to 0x53)
You can use a breadboard and jumper wires for connection.
MicroPython Code to Read Acceleration Values
Here’s a sample MicroPython code to read the Acceleration Raw Values in the x, y, and z-axis by interfacing the ADXL345 & Raspberry by Pico.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
from machine import Pin, I2C import time import ustruct # Constants ADXL345_ADDRESS = 0x53 ADXL345_POWER_CTL = 0x2D ADXL345_DATA_FORMAT = 0x31 ADXL345_DATAX0 = 0x32 # Initialize I2C i2c = I2C(0, sda=Pin(8), scl=Pin(9), freq=400000) # Initialize ADXL345 def init_adxl345(): i2c.writeto_mem(ADXL345_ADDRESS, ADXL345_POWER_CTL, bytearray([0x08])) # Set bit 3 to 1 to enable measurement mode i2c.writeto_mem(ADXL345_ADDRESS, ADXL345_DATA_FORMAT, bytearray([0x0B])) # Set data format to full resolution, +/- 16g # Read acceleration data def read_accel_data(): data = i2c.readfrom_mem(ADXL345_ADDRESS, ADXL345_DATAX0, 6) x, y, z = ustruct.unpack('<3h', data) return x, y, z # Main loop init_adxl345() while True: x, y, z = read_accel_data() print("X: {}, Y: {}, Z: {}".format(x, y, z)) time.sleep(0.1) |
This code initializes the ADXL345, configures it for full-resolution, ±16g measurements, and then reads and prints the acceleration data for the X, Y, and Z axes every 0.1 second. You can adjust the code to use a different sensitivity setting or read the data at a different rate, depending on your application’s requirements.
MicroPython Code to Read Rotational Angle Values
To measure the magnitude, pitch, and roll using the ADXL345 accelerometer, you can modify the MicroPython code. Here’s the updated code to calculate the magnitude, pitch, and roll and print them along with the acceleration data:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
from machine import Pin, I2C import time import ustruct import math # Constants ADXL345_ADDRESS = 0x53 ADXL345_POWER_CTL = 0x2D ADXL345_DATA_FORMAT = 0x31 ADXL345_DATAX0 = 0x32 # Initialize I2C i2c = I2C(0, sda=Pin(8), scl=Pin(9), freq=400000) # Initialize ADXL345 def init_adxl345(): i2c.writeto_mem(ADXL345_ADDRESS, ADXL345_POWER_CTL, bytearray([0x08])) # Set bit 3 to 1 to enable measurement mode i2c.writeto_mem(ADXL345_ADDRESS, ADXL345_DATA_FORMAT, bytearray([0x0B])) # Set data format to full resolution, +/- 16g # Read acceleration data def read_accel_data(): data = i2c.readfrom_mem(ADXL345_ADDRESS, ADXL345_DATAX0, 6) x, y, z = ustruct.unpack('<3h', data) return x, y, z # Calculate the magnitude of acceleration def calc_accel_magnitude(x, y, z): return math.sqrt(x**2 + y**2 + z**2) # Calculate roll angle in degrees def calc_roll(x, y, z): return math.atan2(y, math.sqrt(x**2 + z**2)) * (180 / math.pi) # Calculate pitch angle in degrees def calc_pitch(x, y, z): return math.atan2(-x, math.sqrt(y**2 + z**2)) * (180 / math.pi) # Main loop init_adxl345() while True: x, y, z = read_accel_data() magnitude = calc_accel_magnitude(x, y, z) roll = calc_roll(x, y, z) pitch = calc_pitch(x, y, z) print("X: {}, Y: {}, Z: {}, Magnitude: {:.2f}, Roll: {:.2f}, Pitch: {:.2f}".format(x, y, z, magnitude, roll, pitch)) time.sleep(0.1) |
Upload the code and check the results.
This updated code includes thress new functions, calc_roll , calc_roll, calc_accel_magnitude, to compute the pitch, roll, and magnitude of the acceleration vector. The pitch and roll are calculated using the atan2 function from the math library and are expressed in degrees. The magnitude is calculated as the square root of the sum of the squared acceleration components.
The main loop reads the accelerometer data, calculates the magnitude, pitch, and roll, and then prints the results every second.













6 Comments
How can you get acceleration in m/s^2 with these values?
I am having this error.
any solution ?
I am having same error, were you able to resolve it?
I am getting same error, were you able to resolve it?
I believe the SDA and SCL pins are wrong in the wiring description; should be pins GP8 and GP9
Ich habe nun die Falsche verdrahtungsangabe korregiert und mich nach den Pin Belegung in der Programmierung gerichtet. Jetzt geht es. Es reicht nicht im Programm die Pins auf die Fehlerhaften Pinbelegung um zu stellen, da das Programm diese als Schlecht bezeichnet. Lötet den I2C Bus auf Pin 8 und Pin 9, dann sollte es auch bei euch Laufen.