UP | HOME

YAGNI
You are(n't)? gonna need it…

Build an I2C scanner for ESP32

About

This post will explain how to build the I2C Tools Example included in the ESP-IDF framework.

This tool set includes a \(I^2C\) scanner, which is useful to discover the components attached to an \(I^2C\) bus and to discover their corresponding addresses.

The I2C Tools Example is implemented as a CLI like interface using the ESP32 Console component.

Build the I2C Tools Example

Building the tool set is as simple as building any other ESP-IDF example. First copy the i2c_tools folder included in the framework to a writable location:

cp -r $IDF_PATH/examples/peripherals/i2c/i2c_tools ~
cd ~/i2c_tools

Then set the target, build, flash and monitor the project:

idf set-target esp32c3
idf build flash monitor

A successful setup will launch the CLI with the following prompt:

Type 'help' to get the list of commands.
Use UP/DOWN arrows to navigate through command history.
Press TAB when typing command name to auto-complete.
I (319) main_task: Returned from app_main()
i2c-tools>

Run the scanner

Before scanning the \(I^2C\) bus, we have to configure the GPIOs used for the data and clock lines. The following configuration is using GPIOs 18 and 19 for the SCL and SDA lines respectively:

i2c-tools> i2cconfig --scl=18 --sda=19

Now it should be possible to scan the bus by running i2cdetect:

i2c-tools> i2cdetect

The following output shows that we have two different \(I^2C\) components connected to the bus, one with the address 0x48 and another one with the address 0x68.

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 

Exit the monitor with Ctrl+].

See also

  1. The I2C Tools Example
  2. The ESP-IDF I2C documentation

Date: 2025-07-04