2026-01-16 11:59 AM - last edited on 2026-01-17 1:36 AM by Andrew Neil
Hello,
I am unable to setup I2C communication between 2 x STM32F103 blue pill boards. I have pasted code and the hardware setup below. Would appreciate help trying to pinpoint the problem.
Hardware setup:
Software setup:
I2C Configuration:
Run Procedure:
Firmware setup:
Master:
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_I2C1_Init();
while (1)
{
HAL_StatusTypeDef status;
//send address byte, wait for acknowledge from slave, send 1 data byte
status = HAL_I2C_Master_Transmit(&hi2c1, slave_addr, &fast_cmd, 1, 200);
//checks if there is a NACK, bus error, etc and turns led ON
if (status != HAL_OK) {
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);
} else {
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
}
HAL_Delay(5000); // Wait 5 seconds
//send address byte, wait for acknowledge from slave, send 1 data byte
status = HAL_I2C_Master_Transmit(&hi2c1, slave_addr, &slow_cmd, 1, 200);
//checks if there is a NACK, bus error, etc and turns led ON
if (status != HAL_OK) {
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);
} else {
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
}
HAL_Delay(5000); // Wait 5 seconds
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
Slave:
I2C_HandleTypeDef hi2c1;
uint8_t rx_buffer = 0; // Buffer to receive I2C data
volatile uint32_t blink_speed = 50; // Initial blink speed (ms)
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_I2C1_Init();
/* Enable I2C slave listening */
HAL_I2C_EnableListen_IT(&hi2c1);
while (1)
{
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
HAL_Delay(blink_speed);
}
}
void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode)
{
if (hi2c->Instance == I2C1 &&
TransferDirection == I2C_DIRECTION_TRANSMIT)
{
HAL_I2C_Slave_Receive_IT(hi2c, &rx_buffer, 1);
}
}
void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c)
{
if (rx_buffer == 0xFF)
{
blink_speed = 100;
}
else if (rx_buffer == 0x00)
{
blink_speed = 1000;
}
HAL_I2C_EnableListen_IT(hi2c);
}
void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c)
{
HAL_I2C_EnableListen_IT(hi2c);
}
Edited to apply source code formatting - please see How to insert source code for future reference.
2026-01-16 12:51 PM - edited 2026-01-16 1:06 PM
Hi,
> IDE results in the following message: "Target no device found...
This happens , if there is not a STM cpu , but some cpu by GD or CKS or other.
Probably you use the st-link V2 "sticks" , = clones of st-link .
>Also, the only way to connect to the STM is by holding the reset button
Set connect to "software reset" , as V2 sticks have no working hard reset.
So dont expect this mix of unknown cpu's working "fine" , or much help here from STM for your cpu mix from other manufacturers.
Maybe try on the forum or help they provide...Knowledge of Chinese could be helpful.
Or look at comments of the product, before buying ; is there any "dont work with STM..IDE " or "is not genuine STM" ?
Then dont buy, look for another with good comment "works with STM..IDE " , maybe this will be ok.
Or buy some boards with genuine STM chips, like cheap nucleo boards. ymmv.
2026-01-17 1:45 AM - edited 2026-01-17 2:34 AM
Note that Blue Pill is not an ST product, and likely contains a clone/fake STM32.
As @AScha.3 says, this is most likely why STM32CubeIDE refuses to connect.
Regarding the ST-Link, see also: How to recognize a genuine ST-LINK/V2 versus a cloned one;
In particular, this comment.
You could save yourself a load of grief by using genuine ST NUCLEO-F103RB boards; then you could take advantage of the ready-to-go examples - which include connecting 2 boards via I2C!
As to your I2C experiment, you are making the common mistake of trying to do both ends at once.