Showing results for 
Search instead for 
Did you mean: 

STM32F407VG I2C MEMS Sensor Value Reading - Bare Metal Programming

Associate II


I have a discovery board with stm32f407vg-t6.There is a mems sensor on it and I am trying to read sensor values via I2C. However, my code is not working. I also upload my code.

I checked my code 4 times and couldn't find any mistake. I even messed up the code a little bit to be sure all the parameters were set correctly.

I connected physically the pins on the board like this below:

  • PA5->PB6
  • PA6->GND
  • PA7->PB7

Please help me to find the problem in this code. 

ST Employee

Hello @electrovanga , 

You can configure the I2C from STM32CubeMx then you can generate the code and manage it, it's easier.

Also, could you please check the  STM32F4-Discovery_FW package.

It offers the BSP appropriate drivers and the Mems accelerometer component that can help you to develop your application based on F4Cube FW : 

  • STM32Cube_FW_F4_V1.27.0\Drivers\BSP\Components\lis3dsh\lis302dl.c and .h
  • STM32Cube_FW_F4_V1.27.0\Drivers\BSP\STM32F4-Discovery\stm32f4_discovery_accelerometer.c and .h

You can try also to read data from sensor via HAL drivers.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Hi @Foued_KH,

I am learning bare metal programming with STM32. I will not use HALlibrary. I will also use bare metal programming after I learnt it in my job. Therefore, It is so critical me to undertstand the issue here. Could you help me, please?

Associate II

@Foued_KH ,

Any help from your side, please? I feel like my question just answered to be answered only. I hope ST's attidute is not like this way and more supporting.

This is primarily a user-to-user forum, if you expect ST and other engineers to parachute in you're probably going to be disappointed.

Part of Bare Metal is to Read The Manual and Figure It Out. The reference manuals do describe the HW adequately. Part of the curve here is learning how to read and interpret the manual, and how to observe what's happening inside the peripherals.

One of the routes to understand how something works, is to take something that does work (HAL / Cube Examples), and contrast it to how yours does or doesn't work. You can't on one hand dismiss that as a valid course of action because you don't want to do it, and on the other hand expect people to pick through your code to figure out why it's not working the way you want it too.

You can take parts of the code and swap them out, bisect, figure out if it's an initialization thing.

I will note that doing dozens of RMW (Load/Store) actions on a peripheral register is not efficient, if that's the purpose of using bare metal over other paths.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Pavel A.
Evangelist II

@electrovanga If you want urgent help with your project, you can look here.

Associate II

Dear So Smart @Pavel A.,

Thanks for your answer. First of all, I didn't write it is urgent. Second, if you read my post, you can easily see that I am learning Bare metal programming to improve myself and do some hobby stuff. I am stucked at this point for 3 weeks already and searching on the web. If you want to help, please help. Otherwise, don't write unnecessary comment here like how I have to do now because of you. The time is wasted.


@Tesla DeLorean,

You're right. I agree about what you say. I am stucked here and can't go further since 3 weeks. I need a guidance about fix that part or that part. This is missing and so on. Otherwise, I can't learn if I don't get feedback. 

If the ST support is like this than I go with Texas Instrument and try it. I am learning and even in this phase for non urgent stuff if the community is not supportive, I can't imagine when I use this in professional environment. I don't wonder why my friends are using TI boards now.

ST Employee

Hello @electrovanga , 

Could you please read the section 27.3 I2C functional description in the Reference Manual :

For I2C read you should : 

1. Enable I2C and GPIO clocks ( RCC_AHBxENR & RCC_APBxENR )

2. Configure the I2C pins (GPIOx registers )

3. manage the I2C periph ( I2C_CR1 )

4. I2C clock ( I2C_CR2 )

5. I2C write ( I2C_DR ) + clear ADDR bit 

6. Wait for RxNE to set ( I2C_SR1) + clear ACK bit 

6. I2C read ( I2C_DR ) 



To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Hi Fouded,

Thanks for your answer. I have no issue with my I2C function. However, I can't find the issue.

Some fellows said that I can't use gyro sensor on discovery board with I2C as it is connected to the SPI. Is this correct? I have rectengular mems sensor on board.