Documentation Center

  • Trial Software
  • Product Updates


Create connection to I2C device


  • myi2cdevice = i2cdev(mypi,bus,i2cAddress)


myi2cdevice = i2cdev(mypi,bus,i2cAddress) connects to an I2C device and returns a handle, myi2cdevice. You can exchange data with the I2C device by using myi2cdevice with the read, write, readRegister, and writeRegister methods. You can use myi2cdevice to read properties of the I2C device.


expand all

Connect and Exchange Data with I2C Devices

You can connect and exchange data with a pair of I2C devices.

Create a connection to the Raspberry Pi™ board.

mypi = raspi
mypi = 

  Raspi with Properties:

           DeviceAddress: 'raspberrypi-computername'
                    Port: 18725
               BoardName: 'Raspberry Pi Model B Rev 2'
           AvailableLEDs: {'led0'}
    AvailableDigitalPins: [4 7 8 9 10 11 14 15 17 18 22 23 24 25 27 30 31]
    AvailableSPIChannels: {}
      AvailableI2CBuses: {'i2c-0'  'i2c-1'}
             I2CBusSpeed: 100000

  Supported peripherals

Redisplay AvailableI2CBuses and I2CBusSpeed.

ans = 

    'i2c-0'    'i2c-1'

ans =


Show the location of the I2C pins.


The pin map shows that, for this model and revision of the board, the i2c-1 bus is available on the GPIO header pins I2C1_SDA (GPIO 2) and I2C1_SCL (GPIO 3).

After physically connecting your I2C device to the I2C pins, get the addresses of I2C devices attached to the I2C bus, 'i2c-1'.

ans = 


Create a connection to the I2C sensor at '0x20' and assign that connection to a handle, i2csensor.

i2csensor = i2cdev(mypi,'i2c-1','0x20')
i2csensor = 

I2C with Properties:
     Bus: i2c-1
    I2CAddress: 0x20
       TimeOut: 10

Read two uint8 numbers from the sensor.

output1 = read(i2csensor,2);

Read the value of register 14 from the sensor

output2 = readRegister(i2csensor,14);

Create a connection to the I2C LED display at '0x55' and assign that connection to a handle, i2cdisplay.

i2cdisplay = i2cdev(mypi,'i2c-1','0x55')
i2cdisplay = 

I2C with Properties:
     Bus: i2c-1
    I2CAddress: 0x55
       TimeOut: 10

Write characters to the display.

write(i2cdisplay,[hex2dec('20') hex2dec('51')]);

Write a scalar hexadecimal value, hex2dec('08'), to register 3 on an I2C device.


If you are not using I2C, disable I2C to make additional GPIO pins available.


When you use I2C again, enable I2C.


You can only change the mypi.I2CBusSpeed property when you enable I2C.

ans =


Input Arguments

expand all

mypi — Connection to specific boardhandle

Connection to a specific board, specified as a handle.

Use raspi to create this connection.

Example: mypi

bus — I2C bus'2c-0' | '2c-1'

I2C bus, specified as a string.

Example: 'i2c-1'

Data Types: char

i2cAddress — I2C device addresshexadecimal address

I2C device address, specified as a string. Use scanI2CBus to get a list of addresses. The address is a Hexadecimal value.

Example: '0x20'

Data Types: char

Output Arguments

expand all

myi2cdevice — Connection to specific I2C devicehandle

Connection to a specific I2C device, specified as a handle.

You can use myi2cdevice with the following methods:

You can read values from the following myi2cdevice properties:

  • Bus

  • I2CAddress

See Also

| | | | | | | |

Was this topic helpful?