Working with Arduino SD Card File Read Blocks

This example illustrates reading from a csv file using SD Card File Read block on an Arduino hardware.

Supported Hardware

  • Arduino Mega 2560

  • Arduino Mega ADK

  • Arduino Uno

  • Arduino Due

  • Arduino MKR1000

  • Arduino MKR WIFI 1010

  • Arduino ZERO

  • Arduino Leonardo

  • Arduino Nano 33 IoT

Introduction

In this example, the model with a SD Card File Read block is used to read data from a csv file on an Arduino hardware.

Prerequisites

Before you start with this example, you must:

Required Hardware

To run this example, you must have the following hardware:

  • Supported Arduino board

  • Connecting wires

  • SD Card Shield

  • USB cable

Configure Arduino Hardware

1. Connect the SD File Read block on the Arduino board.

2. Connect a USB cable from the computer to the Arduino board.

Configure the Model for Supported Arduino Hardware

1. In your Simulink model, click Modeling tab and then select Model Settings to open Configuration Parameters dialog.

2. Select the Hardware Implementation pane and select your required Arduino hardware from the Hardware board parameter list. Do not change any other settings.

3. Click OK.

Create a CSV File

1. In the MATLAB command window, type rnum = rand(2,10). This creates random double values with an array sixe of 2x10.

2 Now type csvwrite('csvtext.txt', rnum). This creates a csv file with the values stored in rnum.

3. Copy csvtext.txt to SD card by plugging it to a computer or through a card reader connected to the computer.

4. Remove the card and plug it to the SD card shield connected to Arduino hardware.

Read Data from a CSV File

In this example, data is read from a csv file using SD Card File Read Block on a Arduino Hardware.

open_system('arduino_SDCard_FileRead_CSV');

1. Double click the SD Card File Read block to open the block paramters dialog.

2. Set the SD Card File Read block parameters as shown in this image.

  • File type: Type of data file. In this example, select ASCII.

  • File name: Name of the csv file. You can change the name, if required.

  • Number of times to read file: The number of times to read a file must be a positive integer.

  • Enable Delimitter: When selected, you can choose the required output data type. Select this option.

  • Data type: Type of data. In this example, select Double.

  • Sample time: Block sample time. In this example, specify as 0.01.

3. In your Simulink model, click the Hardware tab and then click Monitor & Tune to read and deploy the data. The Scope block displays the data being read in the target hardware.