cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L053 I2C problems with LIS3DH sensor

JJavi
Associate II

Hi,

I'm trying to recover the data from the LIS3DH sensor and I found this tutorial " https://www.youtube.com/watch?v=A1CIOUZdeR4". In the tutorial the person uses function " if(HAL_I2C_IsDeviceReady(&hi2c1,SLAVEI2C_ADD,10,100)==HAL_OK) " to see if the sensor is ready for use. However when I make the connections and run the program, I don'tsee anything, I do not know if you can give me some advice. 

I connected the sensor as follows:

Sensor STM32

SDA -> SDA/D14

SCL -> SCL/D15

SDO -> 3.3v

GND -> GND

Vcc -> 3.3v

#include "main.h"
 
I2C_HandleTypeDef hi2c1;
 
#define SLAVEI2C_ADD 0x19
 
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
 
int main(void)
{
 
  HAL_Init();
 
  SystemClock_Config();
 
  MX_GPIO_Init();
  MX_I2C1_Init();
 
	
HAL_Delay(1000);
		if(HAL_I2C_IsDeviceReady(&hi2c1,SLAVEI2C_ADD,10,100)==HAL_OK)
{
	HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_5);
}
 
  while (1)
  {
 
  }
 
}

0690X000006DavhQAC.png

I want to recover the aceleration but i can't do a simple step..... Someone could help me???

10 REPLIES 10
ASING.91
Associate II

well all the I2C pins are in PORTB so use the port B pins, go to page number 46 of datasheet for Alternate functions of portB.

My apologies ! I guess I misjudged. you have selected right pins

ASING.91
Associate II

__HAL_RCC_GPIOB_CLK_ENABLE();

  mx_gpio_init();

  __HAL_RCC_I2C1_CLK_ENABLE();

  mx_i2c1_init();

have you enabled the clocks already? as above

No, i didn't enable. I will try it!!!

I've tried, but the LED never lights up, as if the sensor was never ready. 

ASING.91
Associate II

did you enable the clock for the port A on which LED resides. If it doesn't work kindly show the GPIO and I2C initialisation functions. Also just in case I hope you have externally pulled up the SDA and SCL of LIS3DH using 10k resistors and connected CS also to VCC.

I enabled the pullup option of the STM32 card, also I have set CS to Vcc, I am checking carefully what happens when using HAL_I2C_IsDeviceReady. And I have found that I can't establish a connection in the defined time and therefore is in a HAL_Error state. 

ASING.91
Associate II

All GPIO pins have weak internal Pullup and pulldown resistors. So the external is needed. Also as you say that there is timing issue kindly synchronise the frequency while initiating the i2c at i2c.init.timing.

JJavi
Associate II

Hi,

Thanks for all your answers, i solved my problem.