Try   HackMD

To interface the ADXL345 three-axis accelerometer with an STM32 microcontroller, you can use either I2C or SPI (the sensor supports both). Below is a complete guide using I2C with STM32 HAL, which is the most common and beginner-friendly setup.

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

1. ADXL345 Overview

  • 3-axis accelerometer
  • Digital output via I2C or SPI
  • Resolution: 10-bit (±2g to ±16g range)
  • Address: 0x53 (when ALT address pin = LOW)

2. Hardware Connections (I2C Mode)

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Add 4.7kΩ pull-up resistors to SDA and SCL lines if needed.

3. STM32CubeMX Setup

  1. Enable I2C1
  2. Assign pins (e.g., PB6 = SCL, PB7 = SDA)
  3. Set I2C clock speed (100 kHz or 400 kHz)
  4. Generate code with HAL drivers

4. ADXL345 Register Summary

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

5. STM32 I2C Code Example (HAL)
a) Read Device ID

c

uint8_t devID;
HAL_I2C_Mem_Read(&hi2c1, 0xA6, 0x00, 1, &devID, 1, HAL_MAX_DELAY);
// 0xA6 = 0x53 << 1 (write address for I2C)

b) Initialize Sensor

c

// Set POWER_CTL (0x2D) to measurement mode (0x08)
HAL_I2C_Mem_Write(&hi2c1, 0xA6, 0x2D, 1, (uint8_t[]){0x08}, 1, HAL_MAX_DELAY);

// Set DATA_FORMAT (0x31) to ±2g and full resolution (0x08)
HAL_I2C_Mem_Write(&hi2c1, 0xA6, 0x31, 1, (uint8_t[]){0x08}, 1, HAL_MAX_DELAY);

c) Read Acceleration Data

c

uint8_t data[6];
int16_t ax, ay, az;

HAL_I2C_Mem_Read(&hi2c1, 0xA6, 0x32, 1, data, 6, HAL_MAX_DELAY);

ax = (int16_t)(data[1] << 8 | data[0]);
ay = (int16_t)(data[3] << 8 | data[2]);
az = (int16_t)(data[5] << 8 | data[4]);

float accel_x = ax * 0.0039; // assuming ±2g, 4mg/LSB
float accel_y = ay * 0.0039;
float accel_z = az * 0.0039;

6. Notes on Scaling

  • Scale factor: 4 mg/LSB at ±2g range
  • To convert to m/s²: multiply by 9.81
c

float accel_ms2 = accel_x * 9.81; // m/s²

7. Best Practices

  • Add delays after writing to configuration registers.
  • Use DMA or interrupts for real-time applications.
  • Verify DEVID returns 0xE5 to confirm communication.