2026-02-07 1:36 AM
Im using i2c on stm32 nucleo and the slave is a cheap imu (mpu6500). Sometimes the bus just gets stuck and doesnt work. And the weird thing is if i plug everything out, flash code again and run it it will not work. Until it randomly works again until the next random fail. I tried with various setups, with pull ups from stm32, with external 5k pull ups.
Initially i was running bare metal code but i thought something is worng with the code and switched to HAL to do simple read or write. Everything is the same, randomly works and doesn't work.
Did anyone experience and solve something similar?
2026-02-07 6:09 AM
Yes, many people have experienced something similar. You can search for those threads if you want. In order to diagnose your case, I recommend providing more details on your case. What STM32 are you using? Can you show your code? Do you have the ability to look at SDA/SCL traces on the line?
There are some race conditions on some stm32 devices for the i2c peripheral that need to be handled correctly. One way of doing this is disabling interrupts while doing i2c operations.
2026-02-07 6:11 AM
Use a oscilloscopio to check the I2C signal, internal pullups are way to high impedance to be of use for I2C, even the proposed 5K pullups are to high for high speed (400khz).
2026-02-07 6:41 AM
Thanks for the response. I am using Nucleo F446RE code is stripped to the simples possible in order to eliminate the possibility of this happening due to code mistake (since i started first bare-metal style). Now i stripped it to simples HAL code. Initialized PB8 and PB9 in Cube GUI and just called:
HAL_StatusTypeDef ret = HAL_I2C_IsDeviceReady(&hi2c1, (0b1101000<<1) + 0, 1, 100);
if (ret == HAL_OK)
{
printf("The device is ready \n");
}
else
{
printf("The device is not ready \n");
}
The same happens as on my bare metal driver. It works until it randomly stops or until I plug it out. From that point it can not start again even after power off, flashing again etc. Until it randomly works
2026-02-07 6:42 AM
Don't have oscilloscope unfortunately, I'm using logic analyzer but still can't figure this out. I'm using 100kHz for now. I see the MPU6500 sensor I'm using has internal pull-ups. So I tried with and without external pull-ups of 5k i mentioned.
2026-02-07 8:42 AM
What does the logic analyzer show when the issue occurs?