i2cset
Write values to I2C devices
TLDR
Write to a register of an I2C device
Write to a register of an I2C device without asking for confirmation
Write to a register of an I2C device using a specific mode
SYNOPSIS
i2cset [-f] [-y] [-r] I2C_BUS_NUMBER CHIP_ADDRESS DATA_ADDRESS [VALUE] ... [MODE]
PARAMETERS
-f
Forces access to the device even if it's currently busy or handled by a kernel driver. Use with extreme caution.
-y
Disables interactive confirmation for writes, proceeding directly. Useful in scripts.
-r
Reads back the written value(s) immediately after the write operation to verify the data.
I2C_BUS_NUMBER
The number of the I2C bus to interact with (e.g., 0, 1, 2). This corresponds to /dev/i2c-N.
CHIP_ADDRESS
The 7-bit I2C address of the target chip in hexadecimal format (e.g., 0x50).
DATA_ADDRESS
The register address within the chip to write to, in hexadecimal format (e.g., 0x01).
VALUE
The data value(s) to write, in hexadecimal format. One or more values can be provided for block write modes.
MODE
The transfer mode, affecting how DATA_ADDRESS and VALUE are interpreted:
b: Byte mode (default, writes a single byte).
w: Word mode (writes a 16-bit word, low byte first).
s: SMBus block write.
i: I2C block write.
p: Write to a PEC (Packet Error Checking) register.
c: Combined write/read transaction.
DESCRIPTION
i2cset is a command-line utility from the i2c-tools package used to write data to registers of I2C and SMBus devices. It allows specifying the I2C bus number, the chip's 7-bit I2C address, the target register address, and the value(s) to write. The command supports various data transfer modes, including byte (b), word (w), SMBus block (s), and I2C block (i), providing flexibility for different device interactions. It's an essential tool for debugging I2C hardware, configuring sensors, or interacting with EEPROMs directly from the Linux command line.
CAVEATS
Using i2cset with the wrong addresses or values can potentially damage I2C devices or cause system instability. Always consult the device datasheet.
Requires root privileges or appropriate permissions to access /dev/i2c-N devices.
The -f (force) option should be used with extreme caution, as it can interfere with kernel drivers managing the device.
The actual MODE options available can vary slightly depending on the i2c-tools version and kernel I2C bus driver capabilities.
PERMISSIONS
To use i2cset, you typically need root privileges or your user must be part of a group (like i2c or gpio) that has read/write access to the /dev/i2c-N device files.
BUS NUMBER DISCOVERY
The correct I2C bus number (I2C_BUS_NUMBER) can often be found by listing /dev/i2c-* or by using i2cdetect -l to list all detected I2C adapters.
HEXADECIMAL VALUES
All addresses and values for i2cset are specified in hexadecimal format (e.g., 0xNN).
HISTORY
i2cset is part of the i2c-tools package, which has been a staple in Linux for interacting with I2C and SMBus devices for many years. It leverages the Linux kernel's I2C subsystem, providing a userspace interface for hardware control. Its development has focused on robustness and supporting various I2C/SMBus protocols and features.