LinuxCommandLibrary

i2cset

Write values to I2C devices

TLDR

Write to a register of an I2C device

$ i2cset [i2cbus] [device_address] [register_address] [value]
copy

Write to a register of an I2C device without asking for confirmation
$ i2cset -y [i2cbus] [device_address] [register_address] [value]
copy

Write to a register of an I2C device using a specific mode
$ i2cset [i2cbus] [device_address] [register_address] [value] [b|w|c|s|i]
copy

SYNOPSIS

i2cset [-f] [-y] [-r] [-a] I2CBUS CHIP-ADDRESS DATA-ADDRESS VALUE [MODE]
i2cset [-f] [-y] [-r] [-a] -- I2CBUS CHIP-ADDRESS DATA-ADDRESS VALUE1 [VALUE2 ...]
i2cset -V

PARAMETERS

-f
    Force access to device even if adapter reports unsupported operation

-y
    Disable interactive confirmation prompt

-r
    Read back value after write and verify match

-a
    Enable SMBus Packet Error Checking (PEC) if supported

-V
    Display version information and exit

DESCRIPTION

i2cset is a command-line utility from the i2c-tools package designed to write data to registers on I2C or SMBus slave devices connected to a Linux system. It enables low-level interaction with hardware peripherals like sensors, EEPROMs, displays, and microcontrollers via the I2C bus protocol.

To use it, specify the I2C bus number (e.g., 1 for /dev/i2c-1), the hexadecimal slave address of the chip, the register (data) address, and the value to write. The tool supports byte, word, and block transfer modes, making it versatile for various device requirements. For instance, writing a byte value to initialize a sensor or configuring display brightness.

Key features include forcing access to busy devices, disabling confirmation prompts for scripting, verifying writes by reading back data, and optional SMBus Packet Error Checking (PEC) for reliability. It requires the i2c-dev kernel module loaded and write permissions on the I2C device file, often necessitating root privileges or group membership in 'i2c'.

Common in embedded systems, IoT projects, and hardware debugging, i2cset is lightweight and precise but demands caution—incorrect writes can damage or brick hardware.

CAVEATS

Requires i2c-dev module and write access to /dev/i2c-*; incorrect usage can permanently damage hardware; addresses and values are hexadecimal

DATA MODES

b (default): byte data (1 byte value)
w: word data (2 bytes)
c: byte command (no value)
i: I2C block write (length byte + data)
s: SMBus block write (length byte + data)
ms: SMBus block process call

EXAMPLE

i2cset -y 1 0x76 0xf4 0x01 b # Write 0x01 to register 0xf4 on bus 1, chip 0x76 (byte mode)

HISTORY

Developed as part of i2c-tools around 2003 by Frodo Looijaard, later maintained by Jean Delvare; integrated with Linux I2C subsystem for hardware probing and control

SEE ALSO

i2cget(8), i2cdetect(8), i2cdump(8), i2ctransfer(8)

Copied to clipboard