Sensor

๐Ÿ“ ADXL345 Accelerometer (GYโ€‘291 Module)

๐Ÿ“Œ Description

The ADXL345 is a digital 3โ€‘axis accelerometer that measures acceleration along the X, Y, and Z axes. It communicates via I2C, making it easy to connect to microcontrollers. This sensor can detect tilt, motion, and orientation.

๐ŸŽฏ Use

In our projects, the ADXL345 is used to:

  • Detect robot tilt or orientation.
  • Measure movement or vibration.
  • Enable gesture control or motionโ€‘based triggers.

๐Ÿ‘‰ Think of the ADXL345 as the robotโ€™s balance sensor โ€” it tells the robot how it is moving in space.

Module parameters

Pin name Description
GND GND (Power Input Negative)
VCC VCC (Power Input Cathode)
SDA Data transfer pin
SCL Communication clock pin
  • Supply voltage: 3.3V – 5V
  • Connection: PH2.0 4P terminal wire
  • Installation method: screw fixing

๐Ÿ–ฅ๏ธ Code Explanation

import machine
import adxl345
import time

# Configure I2C communication (pins 22 = SCL, 21 = SDA)
i2c_extend = machine.SoftI2C(scl = machine.Pin(22), sda = machine.Pin(21), freq = 100000)

# Initialize ADXL345 sensor
xsensor = adxl345.ADXL345(i2c_extend)

while True:
    print('X: ', end="")
    print(xsensor.readX(), end="")   # Read X-axis acceleration
    print('Y: ', end="")
    print(xsensor.readY(), end="")   # Read Y-axis acceleration
    print('Z: ', end="")
    print(xsensor.readZ())           # Read Z-axis acceleration
    time.sleep_ms(200)               # Wait 0.2 seconds

Step by Step

  1. i2c_extend = machine.SoftI2C(...) โ†’ Sets up I2C communication using pins 22 (SCL) and 21 (SDA).
  2. xsensor = adxl345.ADXL345(i2c_extend) โ†’ Initializes the ADXL345 sensor.
  3. while True: โ†’ Loop that repeats forever.
  4. xsensor.readX() โ†’ Reads acceleration on the X axis.
  5. xsensor.readY() โ†’ Reads acceleration on the Y axis.
  6. xsensor.readZ() โ†’ Reads acceleration on the Z axis.
  7. print(...) โ†’ Displays the values for each axis.
  8. time.sleep_ms(200) โ†’ Adds a short delay between readings.

๐Ÿ‘‰ Together, this program prints the acceleration values for all three axes every 0.2 seconds.


โœ… Conclusion of the Test

  • If the printed values change when the sensor is tilted or moved, the program is working correctly.
  • This test shows how the robot can sense motion and orientation in three dimensions.
  • Later, the ADXL345 could be used for gesture recognition, fall detection, or balancing robots.
On this page