STM32 I2C Scanner HAL Code Example

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

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.

[toc]


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

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!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Author
Khaled Magdy
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?

Leave a Comment