This is an old revision of the document!
This document mainly introduces how to use I2C bus on Khadas SBC. You will learn how to connect I2C devices, check I2C devices, and read device register values.
I2C A
Sensor SCL
↔ I2C_SCK_A
(PIN 22)
Sensor SDA
↔ I2C_SDA_A
(PIN 23)
Sensor GND
↔ GND
Sensor VCC
↔ 5V
or 3.3V
//depends on your sensor
I2C B
Sensor SCL
↔ I2C_SCK_B
(PIN 25)
Sensor SDA
↔ I2C_SDA_B
(PIN 26)
Sensor GND
↔ GND
Sensor VCC
↔ 5V
or 3.3V
//depends on your sensor
I2C 3
Sensor SCL
↔ I2C0_SCK
(PIN 22)
Sensor SDA
↔ I2C0_SDA
(PIN 23)
Sensor GND
↔ GND
Sensor VCC
↔ 5V
or 3.3V
//depends on your sensor
I2C 4
Sensor SCL
↔ I2C1_SCK
(PIN 25)
Sensor SDA
↔ I2C1_SDA
(PIN 26)
Sensor GND
↔ GND
Sensor VCC
↔ 5V
or 3.3V
//depends on your sensor
I2CM A
Sensor SCL
↔ I2CA_SCK
(PIN 25)
Sensor SDA
↔ I2CA_SDA
(PIN 26)
Sensor GND
↔ GND
Sensor VCC
↔ 5V
or 3.3V
//depends on your sensor
I2CM F
Sensor SCL
↔ I2CF_SCK
(PIN 22)
Sensor SDA
↔ I2CF_SDA
(PIN 23)
Sensor GND
↔ GND
Sensor VCC
↔ 5V
or 3.3V
//depends on your sensor
I2C | PIN & GPIO Number | DT Overlays Node | Device Node |
---|---|---|---|
I2CM A | SCL - PIN25 - GPIOT_20 (#466) SDA - PIN26 - GPIOT_21 (#467) | i2cm_a | /dev/i2c-0 |
I2CM F | SCL - PIN22 - GPIOY_17 (#501) SDA - PIN23 - GPIOY_18 (#502) | i2cm_f | /dev/i2c-5 |
In order to use the I2C, you need to enable the I2C function via Device Tree Overlay.
Edit /boot/env.txt
to add the i2c node to overlays
node.
Take VIM3 as an example to enable i2c3
:
overlays=uart4 pwm_ao_a pwm_f i2c3
Edit /boot/dtb/amlogic/kvim4.dtb.overlay.env
to add i2c node to fdt_overlays
node.
I2C | DT Overlays Node |
---|---|
I2C A (PIN25 PIN26) | i2cm_a |
I2C F (PIN22 PIN23) | i2cm_f |
e.g. Enable I2C A
on i2cm_a
fdt_overlays=onewire
Reboot to effect.
Detect device on I2C A:
$ sudo i2cdetect -y -r 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- 1d -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
Detect device on I2C B:
$ sudo i2cdetect -y -r 2
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- 51 -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
Detect device on I2C 3:
$ sudo i2cdetect -y -r 3
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
Detect device on I2C 4:
$ sudo i2cdetect -y -r 4
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- 0e --
10: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- --
20: UU -- 22 -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- UU -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
Detect device on I2C A:
$ sudo i2cdetect -r -y 0
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: UU -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
Detect device on I2C F:
$ sudo i2cdetect -r -y 5
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- UU -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
If you have made a connection, you will see the device address, e.g: 0x1d
,0x18
and 0x51
.
Read register 0x0d
of device 0x1d
on I2C A.
$ sudo i2cget -f -y 1 0x1d 0x0d
0x2a
For more information, please refer to the help messages.