i2cdev - Connection to device on Raspberry Pi hardware - MATLAB (original) (raw)

Main Content

Connection to device on Raspberry Pi hardware

Description

This object represents a connection from the MATLAB® software to the device on Raspberry Pi® hardware I2C bus. Attach an I2C device to the appropriate pins on the Raspberry Pi hardware. To interact with the I2C device, use this object with the functions listed in Object Functions.

Creation

Syntax

Description

`myi2cdevice` = i2cdev([mypi](#buno%5F3a%5Fsep%5Fshared-mypi),`bus`,`i2cAddress`) creates an object that represents a connection to the device connected to the I2C bus on the specified Raspberry Pi hardware myi2cdevice.

example

Input Arguments

expand all

Connection to the Raspberry Pi hardware board, specified as a raspi object.

Properties

expand all

The hexadecimal address of the I2C bus connected to the hardware, specified as numeric or character vector. This property is set by the i2caddress input argument, and it cannot be changed after object creation. Use the scanI2CBus function to get a list of addresses.

Note

The hexadecimal address in the range 0x01 to0x77 are supported.

Example: '0x20'

Data Types: char

Object Functions

Examples

collapse all

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

Create a connection from the MATLAB software to the Raspberry Pi board.

mypi =

Raspi with Properties:

       DeviceAddress: 'raspberrypi-hysdu8X38o'
                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.

mypi.AvailableI2CBuses mypi.I2CBusSpeed

ans =

1×2 cell array

{'i2c-0'}    {'i2c-1'}

ans =

  100000

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 =

1×2 cell array

 {'0x55'}    {'0x20'}

Create a connection, i2csensor, from the MATLAB software to the I2C sensor at '0x20'.

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

i2csensor =

I2Cdev with properties:

    Bus: 'i2c-1'
Address: 32

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, i2cdisplay, from the MATLAB software to the I2C LED display at '0x55'.

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

i2cdisplay =

I2Cdev with properties:

    Bus: 'i2c-1'
Address: 85

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.

writeRegister(i2cdisplay,3,hex2dec('08'),'uint8')

If you are not using I2C, disable I2C using the Hardware Setup Screen to free additional GPIO pins. Before using I2C again, enable I2C and change the I2C bus speed using the Hardware Setup screen. In this example the bus speed is set to 400000 bits/s.

Extended Capabilities

expand all