cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L476RG with ADXL345 axis return 511

SRabi.3
Associate

Hi everyone, I am beginner for STM32 and C. I am trying to use ADXL345 with STM32L476RG board. I watched video and I am trying to use same code but my y axis returns 511. When I move the adxl345, x and z values go to 511 and then go to 0 and 1.

Here is my code

#include "main.h"
#include <stdio.h>
I2C_HandleTypeDef hi2c1;
UART_HandleTypeDef huart2;
#define ADXL345_address 0x53<<1
char buffer[42];
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_I2C1_Init(void);
uint8_t recData[6];
int32_t x,y,z;
float xg,yg,zg;
void ADXL345_write (uint8_t reg,uint8_t value)
{
	uint8_t data[2];
	data[0] = reg;
	data[1] = value;
	HAL_I2C_Master_Transmit(&hi2c1,ADXL345_address,data,2,10);
}
 
void ADXL345_read (uint8_t reg,uint8_t numberOfBytes)
{
	HAL_I2C_Mem_Read(&hi2c1,ADXL345_address,reg,1,recData,numberOfBytes,100);
}
void ADXL345_init(void)
{
	ADXL345_read(0x00,1);
 
	ADXL345_write(0x2d,0);
 
	ADXL345_write(0x2d,0x08);
 
	ADXL345_write(0x31,0x01);
 
}
int main(void)
{
  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  MX_USART2_UART_Init();
  MX_I2C1_Init();
  ADXL345_init ();
  while (1)
  {
		ADXL345_read(0x32,6);
		x = (recData[1]<<8) | recData[0] ;
		y = (recData[3]<<8) | recData[2] ;
		z = (recData[5]<<8) | recData[4] ;
		xg = (float)x*0.0078;
		yg = (float)y*0.0078;
		zg = (float)z*0.0078;
		HAL_Delay(10);
  }
}
 

Live Ex screen ;

xg = 0.0311999992 ;

yg = 511.149597 ; // it say value = 65525 in float 511

zg = 1.092521001 ;

when I move the sensor

xg and zg 's value change to 510, 511 .

In the video, he gets between 0 and 2 values for all axis.

My connections :

STM32L476RG ADXL345

3.3V. VCC

GND GND

SDA (I2C1 PB9) SDA

SCL (I2C1 PB8) SCL

0 REPLIES 0