How to Use the Arduino Explorer App in MATLAB - MATLAB
Video Player is loading.
Current Time 0:00
Duration 6:47
Loaded: 2.43%
Stream Type LIVE
Remaining Time 6:47
 
1x
  • Chapters
  • descriptions off, selected
  • en (Main), selected
    Video length is 6:47

    How to Use the Arduino Explorer App in MATLAB

    Published: 3 Jan 2023

    Hi. My name is Srijith and I'm a senior software engineer in the MATLAB hardware team at MathWorks. In this video, we will see how to detect natural colors using an Arduino board in MATLAB with the help of the Arduino Explorer app.

    The Arduino Explorer app lets you interactively configure the Arduino board, write, read, and visualize data from the connected peripherals. Now for this example, we will be using the Arduino Nano 33 BLE Sense board that hosts an onboard color sensor that can be used to detect the prominent color component of the light projected onto the Arduino from an item in the surrounding.

    Let's get started by first launching the Arduino Explorer app from the MATLAB Apps gallery. You can see here that the app's home screen displays all Arduino boards connected to my machine. In this case, it's just the one Nano 33 BLE Sense board. Now, let's go ahead and connect to this Arduino by clicking on the board's device card icon here.

    The app is now connected to the Arduino board. And the pin Explorer table displays all available GPIO pins of the board and their current status. You can also view the connected board's pinout diagram on the right hand side panel. You can maximize a panel, or open the pinout diagram in the new Figure window outside the app.

    Now, coming back to the example, we will communicate with the APS onboard sensor of the Nano 33 BLE Sense mode using the I2C protocol. To do this, let us configure the I2C interface of the Arduino board and connect to the sensor by clicking on the I2C icon here inside the app's Configure tool suite section. This will launch a model dialog that lets you configure the I2C interface and connect to the I2C device of your choice.

    You can use the Scan I2C button to scan the Arduino I2C interface and get a list of all available I2C devices. In this case, the onboard sensor resides at address 0x39 on bus number 1. So I will go ahead and select the bus number 1 and pick up the address corresponding to the onboard sensor from the I2C address dropdown.

    You can leave the other configuration settings to its default values for now and click on OK. Now, this will create the connection to the I2C sensor. And from here on, we will use the corresponding I2C interface in the Pin Explorer table to communicate with the color sensor.

    Before we start reading data from the color sensor, we need to power on the sensor module and enable color detection. For this, I need to write a decimal value of 15 to the sensor's enabled register address at register address 128. You can refer to the APDS sensor's data sheet for more details on different registers and their layout.

    So let's go ahead and write the value. To do this, I go into the Write tab inside the Pin Configuration panel and select the Write Register option. I now specify the register address of 128. After this, I go into the Pin Explorer table and write the value of 15 and press Enter.

    You can see here that the write is successful, as seen in the log table below. The raw color data from the APDS sensor is available as a uint64 value in register address 148. Let's again go into the Configuration panel and use the Read tab this time to read from register address 148.

    I go ahead and select Read Register option, specify the register address of 148, and select the precision to be uint64. After this, I go ahead and click on the Read button to read the raw uint64 value from the color sensor. You can see the raw data being displayed in the Read Value column and the corresponding entry we made in the log table below.

    The data read from the color sensor is a raw uint64 value. And this needs to be decoded to get the actual RGB color components. The Add Decode Logic button lets you specify a MATLAB function, or script, to decode this raw data. Let's use this functionality to convert the raw I2C sensor data to RGB color components, along with the ambient light value.

    The modal window that opens up on clicking the Add Decode Logic button lets you specify a MATLAB function and path that can be used to decode the raw I2C data. I've implemented a function called Decode Color Data to convert the raw sensor values to RGB color components, along with the ambient light value. Let's specify this MATLAB function name as the decode logic and click on OK.

    Now that we have successfully applied the decoded logic, as seen in this pin configuration panel, let's go ahead and try reading the decoded data from the color sensor. As you can see here, the data being read from the color sensor is decoded using the Decode Color function to display the ambient light, along with the RGB color component values in the Read Value column of the Pin Explorer table. You can also see the corresponding raw data and the decoded data in the log table, as well.

    Now that we have verified the decode logic, let's go ahead and continuously read data from the sensor and observe the changes in sensor value based on the changes in the environmental conditions. To do this, let's check the Read Continuously button in the Configuration panel and click on the Start Reading button here. Now, the data is being continuously read from the sensor. And let's try changing the illumination of Nano 33 BLE board to see any changes in the ambient light.

    You can see here the ambient light value has increased because the current illumination on the Nano 33 BLE board. You can also observe the changes when the illumination of the board is turned off.

    Now that we are happy with the results, let's go ahead and generate the equal MATLAB code to do this outside of the Arduino Explorer app using the APIs provided in the MATLAB support package for Arduino. To do this, click on the Generate Script button in the Export section of the app. This will open up a live script containing the MATLAB code to detect color using I2C interface of the Arduino board using API setup provided as part of the MATLAB support package for Arduino hardware.

    I hope you like this example and find this useful. Feel free to try this and similar examples that you can find in the MATLAB support package for Arduino hardware. Thank you.

    Related Products