Q: How to implement I2C communication on STM32 avoiding common pitfalls?
Answer
STM32 I2C issues usually stem from: wrong pin configuration (open-drain required) missing pull-up resistors (2.2-10 kOhm) or clock stretching timing conflicts. Use Standard Mode (100 kHz) or Fast Mode (400 kHz). The HAL I2C API is complex – use polling for simple single-byte transfers DMA for multi-byte. Always check HAL_I2C_Mem_Write() return status. For bus errors implement timeout and bus reset: toggle SCL 9 times to recover stuck slaves. Enable I2C clock stretching detection via I2C_CR1_ANFOFF. For repeated start conditions use HAL_I2C_Master_Transmit_IT() followed by HAL_I2C_Master_Receive_IT().
Filed under: FAQ
