Main Content

Transmit Data to Peripheral Device Over I2C Interface

The typical workflow for communicating with an I2C peripheral device involves discovery, connection, communication, and cleanup. Discovery and connection are for both the controller and peripheral. You must have either a Total Phase® Aardvark™ or an NI™ USB-845x controller installed to use the aardvark or ni845x interface.

If you have not connected to your controller in MATLAB® yet, see Configure I2C Controller Board Communication. After connecting to your controller in MATLAB, you can discover and connect to any of its I2C peripheral devices.

Communicate with Peripheral Device on Aardvark

This example shows how to communicate with a peripheral device using the Total Phase Aardvark controller board. In this example, the peripheral device is an EEPROM chip on a circuit board connected to the Aardvark.

  1. Ensure that the Aardvark controller is installed and connected to your machine. Use the aardvarklist function to list physically connected Aardvark controllers and their respective serial numbers. You can determine your Aardvark controller's serial number by looking for the serial number printed on the board.

    list = aardvarklist
    list = 
    
      1×2 table
    
                     Model             SerialNumber
             ______________________    ____________
    
        1    "Total Phase Aardvark"    "2239143731" 
    
    
  2. Create a connection to the Aardvark controller using the aardvark function with the serial number.

    controller = aardvark(list.SerialNumber(1))
    controller = 
    
      Aardvark with properties:
    
                       Model: "Total Phase Aardvark"
                SerialNumber: "2239143731"
        AvailableDigitalPins: ["Pin1"    "Pin3"    "Pin5"    "Pin7"    "Pin8"    "Pin9"]
    
      Show all properties, functions
    

    Click Show all properties to view a full list of properties for the aardvark object.

                        Model: "Total Phase Aardvark"
                 SerialNumber: "2239143731"
         AvailableDigitalPins: ["Pin1"    "Pin3"    "Pin5"    "Pin7"    "Pin8"    "Pin9"]
    
                 VoltageLevel: 3.3000
        EnablePullupResistors: 1
            EnableTargetPower: 0
              DigitalPinModes: [6×2 table]
    
  3. Before creating a connection to the EEPROM chip on the controller, you must provide it with power to discover it. Set the EnableTargetPower property of the aardvark object to true. Enabling this setting provides power to the pins on the controller that are connected to the EEPROM.

    controller.EnableTargetPower = true;

  4. You can now scan the controller board for I2C peripheral devices connected to it using the scanI2CBus function. The EEPROM has an address of 0x50. You can also identify the peripheral device's address from its documentation or datasheet.

    address = scanI2CBus(controller)
    address = 
    
      1×1 string array
    
        "0x50"
  5. Create a connection to the EEPROM peripheral device using the device function with its I2C address. The property I2CAddress in the object output represents the address of the peripheral device as the decimal number equivalent (80) of the hexadecimal (0x50).

    eeprom = device(controller,I2CAddress=address)
    eeprom = 
    
      I2CDevice with properties:
    
          Protocol: "I2C"
        I2CAddress: 80
           BitRate: 100000
         ByteOrder: "little-endian"
    
      Show all functions
    
  6. Write 'Hello World!' to the EEPROM chip's register. For EEPROM chips, data is written page-by-page. Each page contains eight bytes. You must specify the page (or register) address before every byte of data written. The first byte of 'Hello World!' is 'Hello Wo' and it has a register address of 0.

    writeRegister(eeprom,0,'Hello Wo')

    The second byte of the string 'Hello World!' is 'rld!' and it has a register address of 8.

    writeRegister(eeprom,8,'rld!')

    Refer to your peripheral device documentation or datasheet for more information about commands that you can send to its register.

  7. Read data back from the chip's register using the readRegister function. The chip returns the characters sent to it.

    readRegister(eeprom,0,12,"char")
    ans =
    
        'Hello World!'
  8. Disconnect from the peripheral device and the controller by clearing the device and aardvark objects from the workspace.

    clear eeprom
    clear controller

Communicate with Peripheral Device on NI USB-845x

This example shows how to communicate with a peripheral device using an NI USB-845x controller board. In this example, the peripheral device is an Analog Devices® ADXL345 sensor chip on a circuit board, using an address of 53 hex on an NI USB-8451. The NI USB-8451 controller board is plugged into the computer and a circuit board containing the sensor chip is connected to the controller board with wires. The circuit has external pullups since the NI USB-8451 used in this example does not have internal pullups.

  1. Ensure that the NI USB-8451 controller is installed and connected to your machine. Use the ni845xlist function to list physically connected NI USB-845x controllers and their respective serial numbers. You can determine your NI USB-845x controller's serial number by looking for the serial number printed on the board.

    list = ni845xlist
    list = 
    
      1×2 table
    
                 Model        SerialNumber
             _____________    ____________
    
        1    "NI USB-8451"     "01F26E0A" 
    
    
  2. Create a connection to the NI USB-8451 controller using the ni845x function with the serial number.

    controller = ni845x(list.SerialNumber(1))
    controller = 
    
      NI845x with properties:
    
                       Model: "NI USB-8451"
                SerialNumber: "01F26E0A"
        AvailableDigitalPins: ["P0.0"    "P0.1"    "P0.2"    "P0.3"    "P0.4"    "P0.5"    "P0.6"    "P0.7"]
    
      Show all properties, functions
    

    Click Show all properties to view a full list of properties for the ni845x object.

                        Model: "NI USB-8451"
                 SerialNumber: "01F26E0A"
         AvailableDigitalPins: ["P0.0"    "P0.1"    "P0.2"    "P0.3"    "P0.4"    "P0.5"    "P0.6"    "P0.7"]
    
                 VoltageLevel: 3.3000
        EnablePullupResistors: 0
             OutputDriverType: "push-pull"
              DigitalPinModes: [8×2 table]
    
  3. Scan the controller board for I2C peripheral devices connected to it using the scanI2CBus function. The ADXL345 sensor chip has an address of 0x53. You can also identify the peripheral device's address from its documentation or datasheet.

    address = scanI2CBus(controller)
    address = 
    
      1×1 string array
    
        "0x53"
  4. Create a connection to the sensor chip peripheral device using the device function with its I2C address. The property I2CAddress in the object output represents the address of the peripheral device as the decimal number equivalent (83) of the hexadecimal (0x53).

    adxl345 = device(controller,I2CAddress=address)
    adxl345 = 
    
      I2CDevice with properties:
    
          Protocol: "I2C"
        I2CAddress: 83
           BitRate: 100000
         ByteOrder: "little-endian"
    
      Show all functions
    
  5. Write to the sensor chip. Refer to the peripheral device documentation or datasheet to determine its remote address and valid write and read commands. In this example, the chip’s device ID register is at address 0. Write 0 to the chip to indicate a read or write to the register.

    write(adxl345,0)
  6. Read data back from the chip using the read function. By sending one byte, you can read back the device ID registry. For this chip, the read-only device ID registry is 229.

    read(adxl345,1)
    ans = 
    
       229
  7. Disconnect from the peripheral device and the controller by clearing the device and ni845x objects from the workspace.

    clear adxl345
    clear controller

See Also

| | | |

Related Topics