Main Content

device

I2C peripheral device connection

Since R2023a

    Description

    A device object represents a connection to an I2C peripheral device on a Total Phase® Aardvark™ or NI™ USB-845x controller board. You must first connect to the controller using aardvark or ni845x. Make sure you have one or more I2C peripheral devices physically connected to the controller. After you create a device object, communicate with it using read, write, readRegister, and writeRegister.

    Creation

    Description

    example

    peripheral = device(controller,I2CAddress=address) creates a connection to a peripheral device with I2C address specified by address on the controller specified by controller. You can find the address for peripheral devices on the controller using the scanI2CBus function.

    The argument address sets the I2CAddress property.

    example

    peripheral = device(controller,I2CAddress=address,Name=Value) creates a connection and sets additional properties using one or more optional name-value arguments. Set the BitRate and ByteOrder properties using name-value arguments as Name1=Value1,...,NameN=ValueN, where Name is the property name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the arguments does not matter.

    Input Arguments

    expand all

    Controller connection, specified as an aardvark object or ni845x object.

    Example: device(controller,I2CAddress=address) connects to an I2C peripheral device on an Aardvark or NI USB-845x controller controller.

    Properties

    expand all

    This property is read-only.

    Communication protocol, specified as I2C.

    Example: peripheral.Protocol returns the connection type as I2C.

    Data Types: string

    This property is read-only.

    I2C address of peripheral device to connect to on the Aardvark or NI USB-845x controller, specified as a number. You can identify I2C addresses on your controller board using the scanI2CBus function. Refer to your peripheral device's datasheet to determine its I2C address.

    Example: device(controller,I2CAddress="0x48") connects to the peripheral device at address 0x48.

    Data Types: double

    Bit rate of the controller board, specified as a positive integer in Hz. You must specify a bit rate that is supported by the controller and peripheral device. Set this property at object creation using a name-value argument. You can also change it after object creation using dot notation.

    Example: device(controller,I2CAddress=address,BitRate=250000) sets the bit rate to 250kHz.

    Data Types: double

    Sequential order in which bytes are arranged into larger numerical values, specified as "little-endian" or "big-endian". Set this property at object creation using a name-value argument. You can also change it after object creation using dot notation.

    Example: device(controller,I2CAddress=address,ByteOrder="big-endian") sets the byte order as big-endian.

    Data Types: char | string

    Object Functions

    readRead data from I2C peripheral device
    writeWrite data to I2C peripheral device
    readRegisterRead data from I2C peripheral device register
    writeRegisterWrite data to I2C peripheral device register

    Examples

    collapse all

    Connect to an I2C peripheral device on an NI USB-845x Interface Device.

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

    controller = ni845x("01F26E0A");

    Make sure that your I2C peripheral devices are physically connected to the controller. Scan for connected I2C peripheral devices. The scanI2CBus function returns a list of the I2C addresses of these peripheral devices.

    i2cAddresses = scanI2CBus(controller)
    i2cAddresses = 
    
      1×2 string array
    
        "0x48"    "0x53"

    In this example, two peripheral devices are connected.

    Create a connection to the peripheral device with address 0x48. Refer to your peripheral device's datasheet to determine its I2C address.

    peripheral = device(controller,I2CAddress="0x48");
    peripheral = 
    
      I2CDevice with properties:
    
          Protocol: "I2C"
        I2CAddress: 72
           BitRate: 100000
         ByteOrder: "little-endian"
    
      Show all functions
    

    You can communicate with the peripheral device using read, write, readRegister, and writeRegister.

    Version History

    Introduced in R2023a