cancel
Showing results for 
Search instead for 
Did you mean: 

I2C communication between STM32L4R5 and esp32

ejeee
Associate

I tried creating I2C connection between my stm32 board and esp32. I assume it is not working because of my code, due to the wiring being so simple in I2C communication, the pull up resistor is connected externally. Do you guys see any mistake in my code that could make my I2C to not work?  I cannot figure out why it would not work.

14 REPLIES 14
tantheman
Associate

Have a look at my post. it may be helpful.

ESP32 AND STM32 I2C INTERFACE 

@tantheman did you mean to give a link?

https://community.st.com/t5/others-stm32-mcus-related/esp32-and-stm32-i2c-interface/td-p/854859 

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.

yes meant to give the link but was facing some errors .... being new to the site 

MM..1
Chief III

Seems in Arduino you miss order , right is

bool Wire.begin(uint8_t addr, int sdaPin, int sclPin, uint32_t frequency);
Bermingham
Associate

It looks like the confusion here mostly comes from how the STM32 HAL handles 7-bit I2C addresses. A few key points:

  1. Address Shifting – STM32 HAL expects the 7-bit slave address shifted left by 1. The LSB is reserved for the R/W bit. So if your ESP32 slave address is 0x08, you should pass 0x08 << 1 to the HAL functions.

  2. Pull-up Resistors – Make sure both SDA and SCL lines have pull-up resistors. Without them, communication can be unstable, especially at higher speeds.

  3. Check Master vs Slave – Confirm which device is the master and which is the slave. Test each side with a known-working device first before connecting STM32 ↔ ESP32.

  4. Debugging – If possible, use an oscilloscope or logic analyzer to check the I2C lines. Look for proper clock and data transitions. Noise or missing ACKs are common culprits.

  5. HAL vs Arduino Wire – Note that STM32 HAL requires the address shifted left, while Arduino Wire library uses unshifted 7-bit addresses. That difference often causes confusion when connecting STM32 to Arduino/ESP devices.

  6. Start Simple – Use a minimal setup first: one master, one slave, simple read/write commands, then scale up to multiple devices.

Following these steps usually resolves most STM32 ↔ ESP32 I2C issues.