Skip to content
systemcoding.dev
Embedded Linux

Read and Write I2C Registers With i2cget, i2cset and i2ctransfer

Read and write device registers from the shell with i2cget, i2cset, i2cdump and i2ctransfer from i2c-tools 4.3, using a TMP102 sensor as the worked example.

6 min read
Summarize in AI
Flat illustration of a hardware register drawn as bit fields with one field highlighted, with the Linux, Rust and STMicroelectronics logos.

After i2cdetect shows your device on the bus, the next step is to talk to it: read an ID register to confirm you have the part you think you have, read a measurement, change a configuration bit. i2c-tools gives you four commands for it. i2cget and i2cset read and write one register, i2cdump prints a block of registers, and i2ctransfer builds an arbitrary transaction of your own. They are the right tools for bringing up a new sensor before you write any code, and they make datasheet tables concrete. This post works through them with a TMP102 temperature sensor at address 0x48 on bus 1, using i2c-tools 4.3 on Linux 6.12. The register map is from the TMP102 datasheet.

What a register access is on the wire

Most sensors expose a set of registers, each with a one-byte pointer, and the device holds an internal pointer to the register you last selected. Writing a register is: send the device address with the write bit, then the register pointer, then the data bytes. Reading is a write of the pointer followed by a repeated start and a read, so that the device returns the data from that register. Some devices let you skip the pointer write and read straight from wherever the pointer was left, which is why a bare read works on some chips and returns stale data on others.

The TMP102 registers are 16 bits wide for the temperature, configuration and limits: pointer 0x00 is the temperature, 0x01 the configuration, 0x02 and 0x03 the low and high thresholds. The temperature register holds a 12-bit two's complement value in the top bits of the 16-bit word, in units of 0.0625 °C.

Read a register with i2cget

The arguments are the bus, the device address, the register and optionally the data size. Without a size it does a single byte read:

Shell
i2cget -y 1 0x48 0x00 w
Text
0x0019

The w size reads a 16-bit word. Note the byte order: i2cget uses the SMBus word read, which delivers the low byte first on the wire as the least significant byte of the result. The TMP102 sends the most significant byte first, so the value is byte-swapped relative to the datasheet. The device sent the bytes 0x19 0x00 in the order MSB then LSB, and i2cget printed them as 0x0019, with the first byte in the low position. To decode, swap them back to get 0x1900, shift right four bits for the 12-bit field to get 0x190, which is 400 in decimal, and multiply by 0.0625 to get 25.0 °C.

Text
0x1900 >> 4 = 0x190 = 400
400 x 0.0625 = 25.0 degrees C

This byte-swap is the commonest confusion, and it is the reason to prefer i2ctransfer for devices with big-endian registers, as shown below.

Write a register with i2cset

The same arguments, with the value added. To set the TMP102's configuration register to the conversion rate you want, first read it, change the field and write it back. For a simple example, write the pointer and 16 bits of configuration:

Shell
i2cset -y 1 0x48 0x01 0x60A0 w
Text
(no output on success)

For a word write, SMBus puts the low byte first, so 0x60A0 goes on the wire as 0xA0 then 0x60, which the big-endian TMP102 interprets as 0xA060. Again, if you write a word to a big-endian device, swap the bytes in what you pass, or avoid the word commands. Without the -y option i2cset stops and asks you to confirm, which prevents accidents on a bus with devices you did not mean to touch. A write to the wrong address on a bus with a power management chip can switch off rails, so check the address twice.

There is a safety mask option, -m, that limits which bits change and reads the current value first:

Shell
i2cset -y -m 0x00F0 1 0x48 0x01 0x0050 b
Text
(no output on success; only bits 7:4 of the byte were modified)

Dump a block with i2cdump

i2cdump reads every register from 0x00 to 0xFF and prints a hex table, which is a quick way to see the state of a chip you have not met:

Shell
i2cdump -y 1 0x48 b
Text
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f    0123456789abcdef
00: 19 00 60 a0 4b 00 50 00 19 00 60 a0 4b 00 50 00    ?.`?K.P.?.`?K.P.
10: 19 00 60 a0 4b 00 50 00 19 00 60 a0 4b 00 50 00    ?.`?K.P.?.`?K.P.

A chip with four registers repeats them across the address space, as seen here, and a chip with a large map shows distinct values. The listing is example data. Be careful with i2cdump on devices where a read has a side effect, such as clearing a status flag or advancing a FIFO: dumping reads every register, so you may erase the very flag you wanted to see. The man page says as much, and a read of only the registers you need is safer.

Build a transaction with i2ctransfer

i2ctransfer writes and reads in one combined transaction, with the byte order untouched, which makes it the cleanest way to read big-endian registers. Each message is w<length>@<address> for a write or r<length> for a read, followed by data bytes for writes:

Shell
i2ctransfer -y 1 w1@0x48 0x00 r2
Text
0x19 0x00

This writes the register pointer 0x00, then does a repeated-start read of two bytes, and prints them exactly in wire order: 0x19 first, which is the most significant byte for the TMP102. Decode in your head or in a shell:

Shell
echo 'scale=4; 400 * 0.0625' | bc
Text
25.0000

A write of several bytes is the same syntax with the data after the length. To write the configuration register with the byte order the datasheet shows, most significant byte first:

Shell
i2ctransfer -y 1 w3@0x48 0x01 0x60 0xA0
Text
(no output on success)

w3 means three bytes: the pointer, then the two data bytes in the order they go on the wire. There is no hidden swap, and what you type is what the chip sees. Add -v to print what was sent and received if you want to confirm it.

SMBus versus plain I2C

i2cget and i2cset use SMBus protocol commands when the adapter supports them, and the byte order of word commands follows SMBus. Some adapters lack SMBus support entirely and implement plain I2C only; there the tools fall back or refuse. i2ctransfer uses plain I2C messages, so it works on any adapter that supports the I2C functionality, and it is the only tool here that can issue a transfer of more than 32 bytes, since SMBus blocks are limited to 32. Check what the adapter supports:

Shell
i2cdetect -F 1 | head -6
Text
Functionalities implemented by /dev/i2c-1:
I2C                              yes
SMBus Quick Command              yes
SMBus Send Byte                  yes
SMBus Receive Byte               yes
SMBus Write Byte                 yes

Common mistakes

Reading before the device is ready, such as right after a conversion trigger, returns old data, so check the datasheet's timing. Confusing the register pointer with a data byte: in i2ctransfer the first byte after w is whatever the device expects first, usually a register address. Forgetting that some devices auto-increment the register pointer on a multi-byte read and others do not. And using the 8-bit address from a datasheet that includes the read/write bit.

When to stop using the shell

Once the register map is understood, write the code that does the job: in a C or Python program, or as a kernel driver if the device is a standard class. A shell loop that reads a sensor every second drifts, wastes processes, and cannot handle errors well. Use the shell commands to learn and to debug.

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.