Main Content

Working with Arduino SD Card File Read Blocks

This example shows how to use the Simulink® Support Package for Arduino® Hardware to read data from a csv file using the SD Card File Read block on 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

  • ESP32-WROOM (Arduino Compatible)

  • ESP32-WROVER (Arduino Compatible)

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 Simulink Model for Supported Arduino Hardware

1. Open the arduino_SDCard_FileRead_CSV Simulink model.

2. In the Simulink model, click Modeling tab and then select Model Settings to open Configuration Parameters dialog box.

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

4. Click OK.

Create CSV File

1. In the MATLAB® command window, type rnum = rand(2,10). This creates random double values with an array size 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 CSV File

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

1. Double click the SD Card File Read block to open the block parameters 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. On the Hardware tab of the Simulink model, in the Mode section, select Run on board and then click Monitor & Tune to deploy and read the data. The Scope block displays the data being read in the target hardware.