主要内容

write

Write to I2C device connected to NVIDIA Jetson board

Since R2026a

    Description

    Add-On Required: This feature requires the MATLAB Coder Support Package for NVIDIA Jetson and NVIDIA DRIVE Platforms add-on.

    write(i2cObj,data) writes the data, data, to the I2C device, i2cObj, as uint8 values.

    example

    write(i2cObj,data,dataPrecision) writes in the data type dataPrecision.

    example

    Examples

    collapse all

    This example shows how to write data to an I2C device connected to an NVIDIA® Jetson™ board.

    Create a jetson object namedhwObj. Use the scanI2CBus function to scan the I2C buses on the Jetson board. In this example, the Jetson board has two I2C buses, i2c-1 and i2c-8. Each bus has a device at address 0x74.

    hwObj = jetson;
    addresses = scanI2CBus(hwObj);
    Devices on i2c-1: 74.
    Devices on i2c-8: 74.

    Create an i2cdev object, i2cObj, connecting to the device on the i2c-1 bus.

    i2cObj = i2cdev(hwObj,"i2c-1",0x74);
    Connected to device at address 116 on bus i2c-1.

    Write the value 42 to the I2C device. By default, the connection object writes the value as a uint8 data type.

    write(i2cObj,42);
    

    Write values to an EEPROM device connected to a Jetson board via I2C. The jetson object, hwObj, represents the connection to the Jetson board. The i2cdev object, i2cObj, connects the EEPROM to MATLAB®.

    For the EEPROM, specify the address to write to as the first entry of data. Write the value 42 to address 3.

    write(i2cObj,[3 42]);

    To verify the data on the EEPROM, use the readRegister function to read the data from register 3. The read matches.

    readRegister(i2cObj,3)
    ans =
    
      uint8
    
       42

    Write values to an EEPROM device connected to a Jetson board via I2C. The jetson object, hwObj, represents the connection to the Jetson board. The i2cdev object, i2cObj, connects the EEPROM to MATLAB.

    Create a variable named writeData that contains the hexadecimal data 0xA0B0C000.

    writeData  = 0xA0B0C000;

    In this example, the EEPROM uses the first byte of the data to write as the address to write to. Use 0x01 as the address.

    writeData = writeData + 0x00000001; 
    dec2hex(writeData)
    ans =
    
        'A0B0C001'

    Write the data to the I2C device. Specify that the data is the uint32 data type.

    write(i2cObj,writeData,"uint32");

    To verify the data, read the three bytes starting at address 1 and display them in hexadecimal format. The I2C object writes the data in little-endian format.

    for i=1:3
        readOut = readRegister(i2cObj,i);
        dec2hex(readOut)
    end
    ans =
    
        'C0'
    
    ans =
    
        'B0'
    
    ans =
    
        'A0'

    Input Arguments

    collapse all

    Connection to the I2C device, specified as an i2cdev object.

    Data to write to the I2C device, specified as a vector.

    Data type, specified as "uint8", "int8", "uint16", "int16", "uint32", "int32", "uint64", or "int64".

    Version History

    Introduced in R2026a

    See Also

    Objects

    Functions