STM32 I2C Scanner HAL Code Example

Previous Tutorial Previous Tutorial Tutorial 45 Next Tutorial Next Tutorial
STM32 I2C Scanner Example
STM32 Course Home Page 🏠


STM32 I2C Scanner HAL Example Code Tutorial


In this short tutorial, we’ll be creating an STM32 I2C Scanner example project. To check for the connected slave devices on the I2C bus and report their addresses over UART to our PC.

The I2C Scanner example is a very common Arduino sketch example to scan the I2C bus for available devices and get their addresses (if found). That can be useful if you don’t know the address of any I2C device or just not sure about it. Just run this example and get the addresses.

It can also tell if an I2C device is actually working or not. Maybe the device you’re trying to communicate with is actually damaged and no longer works. Then, it can be handy to run this example and make sure everything is OK.

STM32 I2C Scanner Example

For this example project, you’ll need to configure one UART peripheral and one I2C interface in master mode. The UART will be used to send the address readings to the PC (with USB-TTL), and the I2C master will do the address scanning as we’ll see next.

I2C Scanner Project Configurations

Those are the CubeMX configurations I’ve used for UART1 & I2C1 peripherals

STM32 I2C Scanner HAL Code Example Sketch STM32 I2C Scanner Example Code HAL

I2C Scanner Project Code Listing

 * File: I2C_Scanner
 * Created on: Mar 28, 2020
 * Ver: 1.0
 * Author:     Khaled Magdy
 * -------------------------------------------
 * For More Information, Tutorials, etc.
 * Visit Website:

#include "main.h"
#include "stdio.h"

I2C_HandleTypeDef hi2c1;
UART_HandleTypeDef huart1;

uint8_t Buffer[25] = {0};
uint8_t Space[] = " - ";
uint8_t StartMSG[] = "Starting I2C Scanning: \r\n";
uint8_t EndMSG[] = "Done! \r\n\r\n";

void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
static void MX_USART1_UART_Init(void);

int main(void)
    uint8_t i = 0, ret;



    /*-[ I2C Bus Scanning ]-*/
    HAL_UART_Transmit(&huart1, StartMSG, sizeof(StartMSG), 10000);
    for(i=1; i<128; i++)
    	ret = HAL_I2C_IsDeviceReady(&hi2c1, (uint16_t)(i<<1), 3, 5);
    	if (ret != HAL_OK) /* No ACK Received At That Address */
    	    HAL_UART_Transmit(&huart1, Space, sizeof(Space), 10000);
    	else if(ret == HAL_OK)
    	    sprintf(Buffer, "0x%X", i);
    	    HAL_UART_Transmit(&huart1, Buffer, sizeof(Buffer), 10000);
    HAL_UART_Transmit(&huart1, EndMSG, sizeof(EndMSG), 10000);
    /*--[ Scanning Done ]--*/

    while (1)

Download STM32 I2C Scanner Project Folder

Test #1 Setup & Results

For the first test, I’ve connected an MPU6050 IMU sensor to the SCL & SDA lines of I2C1 as you can see in the image down below. The I2C address for this slave device is 0x68 as stated in the datasheet. We can change only one bit of that address so there could be 2 MPU6050 sensors on the same bus at maximum. Anyway, we should expect to see that address after running the example on the “Tera Term” serial terminal on my PC.

This is the result

STM32 I2C Scanner Example Result 1

Test #2 Setup & Results

In the second test, I’ve just added another I2C module to the bus lines which is the I2C_LCD interface IO expander (PCF8574T). This module has an I2C slave address of 0x27 as a default address if no solder bridge is touched! You can still play around with those bridges to change the device address to allow many I2C_LCD devices on the same bus. Which we’ll be doing in the future in another tutorial.

But anyway, we should now expect to see another device at the 0x27 address as well as the MPU6050 at 0x68. And this is what happened exactly as you can see on the terminal screen.

STM32 I2C Scanner Example Result 2

Did you find this helpful? If yes, please consider supporting this work and sharing these tutorials!


Stay tuned for the upcoming tutorials and don’t forget to SHARE these tutorials. And consider SUPPORTING this work to keep publishing free content just like this!



  STM32 Course Home Page 🏠
Previous Tutorial Previous Tutorial Tutorial 45 Next Tutorial Next Tutorial
Share This Page With Your Network!

Khaled Magdy

I'm an embedded systems engineer doing both Software & Hardware. I'm an EE guy who studied Computer Engineering, But I'm also passionate about Computer Science. I love reading, writing, creating projects and Technical training. A reader by day a writer by night, it's my lifestyle. You can view my profile or follow me via contacts.

You may also like...

Leave a Reply

%d bloggers like this: