Main Content

Read Data from I2C Sensors

This workflow shows how to read data from I2C-based third-party sensors using the LEGO® MINDSTORMS® EV3 hardware.

Introduction

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.

Prerequisites

Required Hardware

  • LEGO MINDSTORMS EV3 brick

  • I2C slave device

  • EV3 brick connector

Hardware Setup

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

  • Data type

  • Data size

  • 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.

open_system('ev3_i2cread_hitechnic_colorsensor')

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 NameParameter DescriptionPreconfigured Parameter Values
EV3 brick input port numberEV3 brick input port connected to the color sensor1
Slave addressColor sensor address on which the EV3 hardware performs the read operation1
Slave register addressColor sensor register address on which the EV3 hardware performs the read operation1
Data typeData type of the color sensoruint8
Data size (N)Data size to be read by the EV3 hardware3
Output error statusStatus of the read operationClear
Sample timeEV3 hardware read operation sampling time on the color sensor0.1 seconds

Run Model

On the Hardware tab of the Simulink model, click Monitor & Tune to run the model on the LEGO MINDSTORMS EV3 Hardware.

Related Links

For more information on the HiTechnic color sensor, see HiTechnic color sensor datasheet.