Read Data from I2C Sensors
This workflow shows how to read data from I2C-based third-party sensors using the LEGO® MINDSTORMS® EV3 hardware.
In the master read operation in the I2C serial data communication protocol, the master device requests data from the I2C slave device or slave device registers using a unique 8-bit address. Both, the master and the slave device must be connected over the same I2C network to establish a successful communication. In this workflow, the EV3 brick acts as the master device, and any one of the I2C-based third-party sensors connected over the I2C network act as the slave device.
Install the Simulink® Support Package for LEGO MINDSTORMS EV3 Hardware.
Complete the Getting Started with LEGO® MINDSTORMS® EV3™ Hardware and Communicating with LEGO® MINDSTORMS® EV3™ Hardware examples.
LEGO MINDSTORMS EV3 brick
I2C slave device
EV3 brick connector
Connect the slave device to the input port of the EV3 brick.
You can connect any I2C-based sensor as a slave to the EV3 brick. Determine the following parameters in the sensor datasheet to correctly configure the I2C Register Read block parameters:
I2C slave address
I2C slave register address that holds the input data
Endianness of the I2C data transfer
Number of bytes
Simulink Model for HiTechnic Color Sensor
This example uses a preconfigured model from the Simulink Support Package for LEGO MINDSTORMS EV3 Hardware. In this model, the EV3 brick acts as the master and the HiTechnic color sensor acts as the slave. The EV3 brick reads the R-G-B data from the color sensor. You can use this data to drive an algorithm on the Arduino hardware depending on the type of application.
To open the model, run this command in the MATLAB® Command prompt.
Configure I2C Register Read Block Parameters for HiTechnic Color Sensor
This table describes the parameters configured on the I2C Register Read block using the HiTechnic color sensor datasheet. With these set parameters, the EV3 brick would read data from three registers (corresponding to R-G-B values) starting from the configured slave register address.
|Parameter Name||Parameter Description||Preconfigured Parameter Values|
|EV3 brick input port number||EV3 brick input port connected to the color sensor||1|
|Slave address||Color sensor address on which the EV3 hardware performs the read operation||1|
|Slave register address||Color sensor register address on which the EV3 hardware performs the read operation||1|
|Data type||Data type of the color sensor||uint8|
|Data size (N)||Data size to be read by the EV3 hardware||3|
|Output error status||Status of the read operation||Clear|
|Sample time||EV3 hardware read operation sampling time on the color sensor||0.1 seconds|
On the Hardware tab of the Simulink model, click Monitor & Tune to run the model on the LEGO MINDSTORMS EV3 Hardware.
For more information on the HiTechnic color sensor, see HiTechnic color sensor datasheet.