I2C on Linux: Scan and Access Devices with i2cdetect and i2cset

Use i2c-tools on Linux to list buses, scan for devices, and read or write registers with i2cdetect, i2cget and i2cset. Includes safety warnings.

Embedded Published 6 min read

Contents

The i2c-tools package gives Linux users shell commands to list I2C buses, scan them for devices, and read or write device registers. This guide covers i2cdetect, i2cget, i2cset and i2cdump, and marks which steps need a real board with a device attached.

The man pages cited here are the Debian bullseye versions. Your distribution may ship a different i2c-tools release, so check man i2cdetect locally if something differs.

How Linux exposes I2C buses

The kernel’s i2c-dev interface lets user space talk to I2C adapters through character devices named /dev/i2c-N. The kernel documentation says the adapter numbers are assigned dynamically and can change from one boot to the next. Look up the right one with i2cdetect -l or in /sys/class/i2c-dev/ instead of assuming it.

You need the i2c-dev module loaded for user-space access. If no bus is listed, the controller may not be described in the device tree. For how to apply device tree changes, see Device Tree Overlays.

sudo modprobe i2c-dev
ls /dev/i2c-*

Install i2c-tools

✓ tested · debian,ubuntu
apt-get update
apt-get install -y i2c-tools
✓ tested · fedora
dnf install -y i2c-tools
✓ tested · arch
pacman -Sy --noconfirm i2c-tools

Confirm the tools are available. Both commands work without any I2C hardware, and -l simply lists nothing in a container.

✓ tested · debian,ubuntu,fedora,arch
i2cdetect -V
i2cdetect -l || true

List buses and scan for devices

i2cdetect -l lists the installed buses. Wherever a command asks for i2cbus, you can give either the number or the name shown there.

i2cdetect -l

To scan bus 1, run:

sudo i2cdetect -y 1

-y skips the interactive confirmation, which mainly helps in scripts. Without it, the tool asks before probing. The default scan range is 0x08 to 0x77 in the bullseye man page. Optional first last arguments narrow the range.

The output is a table of addresses. The legend:

  • --: the address was probed and nothing answered.
  • UU: probing was skipped because a kernel driver already uses that address, which strongly suggests a chip is there.
  • A hex number such as 48: a chip responded at that address.
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:                         -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- -- 

That sample is illustrative, not captured from a specific board. It shows a single device at 0x48.

Probing is not harmless

I2C has no standard way to detect a device, so i2cdetect probes with SMBus quick write and receive byte commands. The man page warns the program can confuse the bus and cause data loss. It also advises against -q and -r, which force one probe method. According to the page, -q is known to corrupt an Atmel AT24RF08 EEPROM found in many IBM ThinkPad laptops, and -r can lock the SMBus on some write-only chips such as clock chips at 0x69. Avoid scanning buses on a machine whose attached hardware you do not know, and avoid -a, which scans non-regular addresses.

Read a register with i2cget

The syntax is i2cget [-f] [-y] [-a] i2cbus chip-address [data-address [mode]]. This reads register 0x00 of the device at 0x48 on bus 1:

sudo i2cget -y 1 0x48 0x00

The default mode is a byte read (b). Use w for a 16-bit word, or c for a byte write followed by a byte read. Appending p to a mode enables packet error checking. If you leave out the register, i2cget reads from the chip’s currently active register where the chip supports that.

sudo i2cget -y 1 0x48 0x00 w

The man page warns that some chips treat an SMBus read as a write, especially in cp mode, so check the datasheet first.

Write a register with i2cset

The syntax is i2cset [-f] [-y] [-m mask] [-r] [-a] i2cbus chip-address data-address [value] ... [mode]. These examples follow the ones in the man page. The first writes 0x42 to register 0x11 of the device at 0x2d on bus 1 and asks for confirmation. The second clears the 3 least significant bits of that register without asking. The third writes 0x5000 to 16-bit register 0x02 of the device at 0x48.

sudo i2cset 1 0x2d 0x11 0x42
sudo i2cset -y -m 0x07 1 0x2d 0x11 0x00
sudo i2cset 1 0x48 0x02 0x5000 w

Useful options:

  • -m mask: only the bits set to 1 in the mask are taken from the value. Bits set to 0 are read from the register and preserved. This assumes the device reads and writes symmetrically, which neither I2C nor SMBus guarantees.
  • -r: read the register back after writing and compare it with the value you wrote. The same symmetry caveat applies.
  • Modes: b byte (default with a value), w word, s SMBus block write, i I2C block write, and c for a short write with no value.

The man page calls i2cset extremely dangerous if misused. In particular, writing to a serial EEPROM on a memory DIMM (addresses 0x50 to 0x57) may destroy the memory and leave the system unbootable. Never run it against addresses you cannot identify.

Dump many registers with i2cdump

i2cdump [-f] [-r first-last] [-y] [-a] i2cbus address [mode [bank [bankreg]]] prints a register table. -r limits the range and works with modes b, w, c and W.

sudo i2cdump -y -r 0x00-0x0f 1 0x48

The -f flag and drivers

The -f flag forces access even if a kernel driver already controls the device. The man pages say this can confuse the driver and return invalid values, and the i2cset page adds that writes may go to the wrong register.

What you can test without hardware

Installing the package and running i2cdetect -V works anywhere. Everything that touches a bus needs an I2C controller, and meaningful reads and writes need a device attached, so those commands are not run in this site’s automated tests. For another bench skill, see Serial Console on Embedded Linux.

Sources

  1. i2cdetect(8) manual page (Debian bullseye)
  2. i2cget(8) manual page (Debian bullseye)
  3. i2cset(8) manual page (Debian bullseye)
  4. i2cdump(8) manual page (Debian bullseye)
  5. Implementing I2C device drivers in userspace: the dev interface (kernel docs)

#I2c #I2c-Tools #I2cdetect #Embedded #Hardware

This article was drafted with AI assistance, fact-checked against the sources listed above and reviewed by a human editor before publication. Spotted an error? See our corrections policy.