Skip to main content
Visitor II
February 13, 2019
Question

STM32L053 I2C problems with LIS3DH sensor

  • February 13, 2019
  • 6 replies
  • 3544 views

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???

    This topic has been closed for replies.

    6 replies

    Visitor II
    February 14, 2019

    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.

    Visitor II
    February 14, 2019

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

    Visitor II
    February 14, 2019

    __HAL_RCC_GPIOB_CLK_ENABLE();

      mx_gpio_init();

      __HAL_RCC_I2C1_CLK_ENABLE();

      mx_i2c1_init();

    have you enabled the clocks already? as above

    JJaviAuthor
    Visitor II
    February 14, 2019

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

    Visitor II
    February 15, 2019

    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.

    JJaviAuthor
    Visitor II
    February 15, 2019

    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. 

    Visitor II
    February 15, 2019

    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.

    JJaviAuthor
    Visitor II
    February 17, 2019

    Hi,

    Thanks for all your answers, i solved my problem.

    Visitor II
    August 2, 2024

    Hi JJavi, 

    Im working on a project involving a stm32l433cct6 microchip with a lis3dh over i2c. I believe i have similar problem as you. Could you please post your solution on this page if thats ok, that would help me alot. 

     

    Thanks!

    Visitor II
    February 22, 2023

    I am new to STM32 programming as well. But I believe you need to shift your device address to the left by one bit for the function to add RW bit.