Main Content


Read data from I2C peripheral device

Since R2023a



    data = read(peripheral,count) reads the number of values specified by count from the I2C peripheral device peripheral and returns the data as a row vector of doubles. The function suspends MATLAB® execution until the specified number of values is read.


    data = read(peripheral,count,datatype) reads the number of values specified by count in the form specified by datatype and returns the data. The datatype argument is a character vector of a standard MATLAB data type. For all numeric datatype types, data is a row vector of double values.


    collapse all

    Connect to an I2C peripheral device on an NI™ USB-845x controller and read data from it.

    Connect to the NI USB-845x using its serial number. In this example, the NI USB-845x controller board that is connected to the computer has the serial number 01F26E0A.

    controller = ni845x("01F26E0A");

    Connect to an I2C peripheral device on the controller board. In this example, the peripheral has an address of 0x48.

    peripheral = device(controller,I2CAddress="0x48");

    Read five values of data from the I2C peripheral device as the default uint8 data type.

    data = read(peripheral,5)
    data =
         0     0     0     0     0

    You can also specify the data type to read. Read five values of uint32 data from the I2C peripheral device.

    data = read(peripheral,5,"uint32")
    data =
         0     0     0     0     0

    Input Arguments

    collapse all

    I2C peripheral device, specified as a device object.

    Example: read(peripheral,count) reads data from the I2C peripheral device object peripheral connected to an Aardvark™ or NI USB-845x controller.

    Number of values to read, specified as a positive integer value.

    Example: read(peripheral,5) reads five values of data.

    Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

    Size and format of each value, specified as a character vector or string scalar. datatype determines the number of bytes to read for each value and the interpretation of those bytes as a MATLAB data type.

    Example: read(peripheral,10,"double") reads ten values of double data.

    Data Types: char | string

    Version History

    Introduced in R2023a