Skip to content
systemcoding.dev
Embedded Linux

Scan an I2C Bus for Devices With i2cdetect

Enable /dev/i2c-N, list adapters with i2cdetect -l, scan a bus with i2cdetect -y and read the address grid, including reserved ranges and missing devices.

6 min read
Summarize in AI
Flat illustration of a single-board computer with its SoC, memory, header and ports, with the Linux, NXP and Raspberry Pi logos.

You wired a sensor to a board, the datasheet says it answers at some address, and the first question is whether the board can see it at all. On Linux the answer comes from i2cdetect, a tool in the i2c-tools package that probes each address on a bus and draws a grid of the ones that respond. It takes a minute to run and settles whether the problem is wiring, power, address or software. This post uses i2c-tools 4.3 on Linux 6.12 and walks through enabling the bus, listing adapters, scanning one, reading the grid and handling the cases where the scan finds nothing or finds something unexpected. The bus protocol itself is in the I2C specification from NXP.

What you are scanning

I2C is a two-wire bus with a controller that addresses devices by a 7-bit address, giving values from 0x00 to 0x7F. Each device on the bus responds to its own address with an acknowledge bit. A scan sends a probe to each address and records whether anyone acknowledged. Some addresses are reserved by the specification, such as 0x00 to 0x07 and 0x78 to 0x7F, so tools skip them by default.

On Linux every I2C controller, called an adapter, appears as a bus number. With the i2c-dev driver loaded, each adapter also exposes a character device /dev/i2c-N that user space can talk to, which is what i2cdetect uses.

Make the device nodes appear

If /dev/i2c-* does not exist, the kernel either lacks the controller driver or the user-space interface. The two kernel options are the adapter driver for your SoC, and CONFIG_I2C_CHARDEV, which provides /dev/i2c-N. If the second is built as a module, load it:

Shell
modprobe i2c-dev
ls /dev/i2c-*
Text
/dev/i2c-1  /dev/i2c-2

On boards with a device tree, the bus must also be enabled there with status = "okay" on the controller node, and the pins must be muxed to the I2C function. A missing node is the commonest reason a bus does not appear.

List the adapters

i2cdetect -l prints every adapter the kernel knows about, so you can match a bus number to hardware:

Shell
i2cdetect -l
Text
i2c-1	i2c       	bcm2835 (i2c@7e804000)          	I2C adapter
i2c-2	i2c       	i2c-bcm2708                     	I2C adapter

The numbers and names depend on the board. The first column is the bus number to pass to the next command, and the third column names the controller. On a board with several buses, read the schematic or the device tree to know which header pins connect to which controller, and do not assume that bus 1 is the one you wired.

Scan a bus

Pass the bus number. The -y option suppresses the interactive warning that probing can disturb some devices:

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

Each cell is an address: the row label gives the high digit and the column the low digit. A two-digit hex number means a device acknowledged. A dash means no answer. UU means the address is in use by a kernel driver, so the tool did not probe it. In this example two devices answered: 0x3c, a typical OLED display address, and 0x48, the default address of a TMP102 temperature sensor or an ADS1115, depending on what is wired.

Read the result against the datasheet's address table. Many parts have address pins that select among several values, such as 0x48 to 0x4B for the TMP102, so a device at 0x49 is not an error if the pin is tied high.

Probe methods and risk

By default the tool picks a probe method per address, a quick write for most ranges and a read for a few ranges where a write could cause harm. The risk is real: a quick-write on some EEPROMs can start a write cycle, and a read on some devices can change an internal pointer. The man page warns about this. If you are not sure, restrict the scan to the range where your device lives, and choose the method:

Shell
i2cdetect -y -r -a 1 0x48 0x4f
Text
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- -- 

-r uses a single-byte read for every probe, -q a quick write, and -a includes the reserved addresses. The two numbers at the end give the first and last address to scan. Prefer a narrow range on a bus with unknown or fragile devices.

UU: the kernel already has it

If an address shows UU, a driver has claimed it. That is good news if you meant it, for example an RTC bound to its driver, and a nuisance if you want to talk to it from user space, since you cannot open it with I2C_SLAVE while a driver owns it, and need I2C_SLAVE_FORCE at your own risk. Check which driver owns it:

Shell
ls /sys/bus/i2c/devices/
Text
1-0068  i2c-1

The directory 1-0068 means bus 1, address 0x68, and its name file and driver link show what it is.

When the scan finds nothing

An empty grid has a short list of causes. Check them in order. Power: is the device powered, and at the voltage the board uses? Wiring: SDA and SCL swapped or open, no common ground? Pull-ups: I2C is open-drain and needs resistors on both lines, commonly 2.2 kΩ to 10 kΩ to the supply; many breakout boards include them and many bare chips do not. Voltage levels: a 5 V device on a 3.3 V bus, or the reverse, needs a level shifter. Bus enabled: does the adapter appear in i2cdetect -l and is the pin mux right? Address: some datasheets quote 8-bit addresses including the read/write bit, so 0x90 in the datasheet is 0x48 as a 7-bit address.

A logic analyzer or oscilloscope shows the acknowledge bit directly and settles the argument quickly if you have one. Without one, an unconnected pin pulled up gives all dashes, and a bus held low by a damaged device shows an error from the tool or hangs:

Text
Error: Could not open file `/dev/i2c-1' or `/dev/i2c/1': Permission denied

That message means your user cannot open the node. Run as root, or add the user to the i2c group and set a udev rule giving that group access.

What a ghost device looks like

Some chips respond at many addresses, filling the grid with numbers, which usually means the bus is stuck low or the address pins float so that the device ignores them. A full grid with every cell present is a wiring or bus-level fault, not a bus with 100 devices.

When scanning is not the right check

Do not scan buses that carry devices you cannot afford to disturb, such as a battery fuel gauge or a PMIC managing the power rails; read the device tree and talk to the drivers through their normal interfaces. Use i2cget and i2ctransfer for reading registers once the address is known.

Share

Keep reading

Flat illustration of user space above the system call boundary, the kernel and the hardware, with the Linux, C and GNU logos.
Kernels

Write a Pin Controller Driver With pinctrl

Write a Linux 6.12 pin controller driver: pinctrl_desc, pins, groups and functions, the set_mux callback, and the device tree states that consumers select.

8 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.