> For the complete documentation index, see [llms.txt](https://auvidea.gitbook.io/i2c/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://auvidea.gitbook.io/i2c/guide/how-to-use-i2c.md).

# How to use I2C

the following examples are using an external GPIO expander.&#x20;

## List i2c devices on a specific bus

`i2cdetect` is used to identify available I2C devices on a given I2C bus. An overview of the available I2C busses can be achieved by using `i2cdetect -l`&#x20;

{% code title="" overflow="wrap" lineNumbers="true" %}

```bash
test@test-desktop:~$ i2cdetect -l
i2c-3	i2c       	3190000.i2c                     	I2C adapter
i2c-1	i2c       	c240000.i2c                     	I2C adapter
i2c-101	i2c       	15210000.nvdisplay              	I2C adapter
i2c-8	i2c       	31e0000.i2c                     	I2C adapter
i2c-6	i2c       	31c0000.i2c                     	I2C adapter
i2c-4	i2c       	Tegra BPMP I2C adapter          	I2C adapter
i2c-2	i2c       	3180000.i2c                     	I2C adapter
i2c-0	i2c       	3160000.i2c                     	I2C adapter
i2c-9	i2c       	i2c-2-mux (chan_id 0)           	I2C adapter
i2c-10	i2c       	i2c-2-mux (chan_id 1)           	I2C adapter
i2c-7	i2c       	c250000.i2c                     	I2C adapter
i2c-5	i2c       	31b0000.i2c                     	I2C adapter
```

{% endcode %}

### Symols:&#x20;

1. `--`: No device found
2. `21`: There is a device on address 0x21
3. `UU`: A Linux driver is currently using this device &#x20;

Syntax: `i2cdetect [options] <busNr>`

{% code title="i2cdetect example" lineNumbers="true" %}

```bash
test@test-desktop:~$ i2cdetect -y -r 2
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: 20 21 -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: UU -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --                         
test@test-desktop:~$ 
```

{% endcode %}

## Dump i2c device registers

The `ì2cdump` operation is good tool to see the content of a given I2C device to get a better overview of the stored data in its registers.

Syntax: `i2cdump [options] <busNr> <deviceAddress>`

{% code title="i2cdump example" lineNumbers="true" %}

```bash
test@test-desktop:~$ i2cdump -y -f 2 0x21
No size specified (using byte-data access)
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f    0123456789abcdef
00: 2c ff 00 ff XX XX XX XX XX XX XX XX XX XX XX XX    ,...XXXXXXXXXXXX
10: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
20: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
30: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
40: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
50: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
60: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
70: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
80: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
90: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
a0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
b0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
c0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
d0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
e0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
f0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX

```

{% endcode %}

## Read register value:

`i2cget` is used for reading a byte value from a given device I2C register address. &#x20;

Syntax: `i2cget [options] <busNr> <deviceAddress> <register> <address>`

{% code title="Read register value with i2cget" lineNumbers="true" %}

```bash
test@test-desktop:~$ sudo i2cget -y -f 2 0x21 0x03 0x00
0x00
test@test-desktop:~$
```

{% endcode %}

## Set register value:

`i2cset` is used for writing a byte value to a given device I2C register address. &#x20;

Syntax: `i2cset [options] <busNr> <deviceAddress> <register> <address> <value>`

{% code title="Set register value using i2cset" lineNumbers="true" %}

```bash
test@test-desktop:~$ sudo i2cset -y -f 2 0x21 0x03 0xff
test@test-desktop:~$ sudo i2cget -y -f 2 0x21 0x03 0x00
0xff
```

{% endcode %}
