2025-01-26 08:52 AM - last edited on 2025-01-29 08:58 AM by SofLit
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.
2025-01-29 08:29 AM
Hello,
there seems to me two potentially problems. At first, there should be connected pull up resistor to both I2C pins (SDA and SCL). The second, you are shifting slave address 0x08 in:
HAL_I2C_Master_Receive(&hi2c2, 0x08 << 1, &receivedByte, 1, 100)
I would say, there should be:
HAL_I2C_Master_Receive(&hi2c2, 0x08, &receivedByte, 1, 100)
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.
2025-01-29 12:32 PM
@Hl_st wrote:Hello,
there seems to me two potentially problems. At first, there should be connected pull up resistor to both I2C pins (SDA and SCL). The second, you are shifting slave address 0x08 in:
HAL_I2C_Master_Receive(&hi2c2, 0x08 << 1, &receivedByte, 1, 100)I would say, there should be:
HAL_I2C_Master_Receive(&hi2c2, 0x08, &receivedByte, 1, 100)
Being that the slave address is 0x08 on the ESP32, then the OP is correct in shifting the slave address 0x08 left by 1 as indicated in the comments ST provides.
/**
* @brief Transmits in master mode an amount of data in blocking mode.
* @PAram hi2c Pointer to a I2C_HandleTypeDef structure that contains
* the configuration information for the specified I2C.
* @PAram DevAddress Target device address: The device 7 bits address value
* in datasheet must be shifted to the left before calling the interface
* @PAram pData Pointer to data buffer
* @PAram Size Amount of data to be sent
* @PAram Timeout Timeout duration
* @retval HAL status
*/
HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData,
uint16_t Size, uint32_t Timeout)
{
uint32_t tickstart;
uint32_t xfermode;