Docs

Micro-controllers, wireless transmission and database

Head

Blue Pill I2C Scanner using STM32CubeIDE

Prerequisites

This project assumes you have already installed STM32CubeIDE. You need to have previously done a basic blink sketch with blue-pill using STM32CubeIDE. I have made a complete video from installing STM32CubeIDE to LED blink program. You can watch it by clicking this link. https://www.youtube.com/watch?v=kXg467nVd_A

STM32CubeIDE Settings

Click RCC → High Speed Clock (HSE) to Crystal/Ceramic Resonator

Click connectivity --> Click I2C1

For I2C select I2C

Click connectivity → Click USB

(Tick) Device (FS)

Click Middleware → Click USB_DEVICE

For Class for FS IP → select Communication Device Class (virtual Port Com)

Click Clock Configuration tab → HCLK (MHz) to 72

Additional code on top of STM32CubeIDE generated code

/* USER CODE BEGIN Includes */
#include "usbd_cdc_if.h"
#include "string.h"
/* USER CODE END Includes */

  /* USER CODE BEGIN WHILE */
  while (1)
  {
      int found = 0;
      char charData[20]; // Data holder
      for(uint16_t i2c=1; i2c<128; i2c++)
      {
          if (HAL_I2C_IsDeviceReady(&hi2c1, i2c<<1, 5, 10) == HAL_OK)
          {
          found = 1;
          sprintf(charData, "%s : ", "i2cdevices.org");
              CDC_Transmit_FS((uint8_t *) charData, strlen(charData));
              HAL_Delay(500);
              sprintf(charData, "%x and ", i2c);
              CDC_Transmit_FS((uint8_t *) charData, strlen(charData));
              HAL_Delay(500);
              sprintf(charData, "%s : ", "STM32 Address");
              CDC_Transmit_FS((uint8_t *) charData, strlen(charData));
              HAL_Delay(500);
              sprintf(charData, "%x\n", i2c<<1);
              CDC_Transmit_FS((uint8_t *) charData, strlen(charData));
              HAL_Delay(500);
           }
           HAL_Delay(5);
       }
       if (!found)
       {
            sprintf(charData, "%s", "No devices found!\n");
            CDC_Transmit_FS((uint8_t *) charData, strlen(charData));
            HAL_Delay(500);
       }
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */