Main Content

SD Card File Read

Read data from an SD card

Add-On Required: This feature requires the Simulink Support Package for Arduino Hardware add-on.

  • SD Card File Read block

Libraries:
Simulink Support Package for Arduino Hardware / Common

Description

The SD Card File Read block reads data from files stored in an SD card that is connected to the hardware. The hardware communicates with the SD card over Serial Peripheral Interface (SPI). Specify the SD Card SPI SS pin for communication in the Configuration Parameters dialog box.

If you simulate a model that contains the SD Card Read block without connecting the hardware, the block outputs zeros. For more information, see Block Produces Zeros or Does Nothing in Simulation.

Click View pin map to open the Arduino Pin Mapping table.

To know how to assign pins for the block, see Pin Mapping for Arduino Timer Independent Blocks.

Note

You can now deploy a Simulink® model containing the SD Card File Read block on your Arduino® compatible ESP32 WROOM and ESP32 WROVER boards.

Ports

Input

expand all

The port accepts the index to read data. For raw and ASCII files, the block starts reading data from the specified index. For audio files, the block starts reading data from the location that is calculated using the specified index, excluding the Waveform Audio File Format (WAV) header.

Dependencies

To enable this port, select the Enable reading from index parameter.

Data Types: uint8

Output

expand all

The block outputs the data read from the SD card. The format of the output depends on the File type parameter. If you select the File type as:

  • Raw –– The block outputs the data in uint8 format.

  • ASCII –– By default, the block outputs the data in uint8 format. If you select the Enable Delimiter parameter, the block outputs the data in the Data type format.

  • Audio (wav) –– The block outputs the audio data as an M-by-N matrix, where M is the Frame size, and N is the Number of Channels supported by the audio.

The block outputs zeroes when it encounters the end of the file (EOF).

Data Types: double | single | int8 | uint8 | int16 | uint16 | int32 | uint32 | Boolean

The block outputs the status of read operation as a uint8 value. Each value corresponds to a data transfer status.

Status ValueStatus Description
0SUCCESS
1FILE CANNOT BE OPENED
2NOT A WAVE FILE
3BLOCK MASK DATA MISMATCH FOR AUDIO
4INDEX VALUE EXCEEDS FILE LENGTH
5FILE HAS CHARACTERS OTHER THAN (0-9, Period, and Delimiter)
6END OF FILE

Data Types: uint8

Parameters

expand all

Specify the SPI module on the Arduino hardware to communicate with SPI peripherals. For more information on Arduino pin mapping and modules that hardware boards support, see Pin Mapping for Arduino Timer Independent Blocks.

Specify the format in which the block reads the file.

Specify the path and name of the file from which the block reads data. For example, to read the sample.wav file that is located inside the TestAudio folder of the SD card, specify the File name as /TestAudio/sample.wav

Specify the number of times the block reads the file from beginning. To read the file until you stop the simulation, specify the Number of times to read file as Inf.

When you select the Enable reading from index parameter, the block configures an input port. Specify an index at this port.

For raw and ASCII files, the block starts reading data from the specified index. For audio files, the block starts reading data from the location that is calculated using the specified index, excluding the WAV header.

  • off –– The block reads data and outputs it in uint8 format.

  • on –– At each sample time, the block reads data until it encounters the next delimiter, specified in the Delimitter parameter. The block outputs the data in the Data type format, omitting the delimiter at the end. The block continues to read the data until the EOF is encountered.

Dependencies

To enable this parameter, set File type to ASCII.

Specify the delimiter to read the data. At each sample time, the block reads data until it encounters the next delimiter. The block outputs the data in the Data type format, omitting the delimiter at the end. The block continues to read the data until the EOF is encountered.

Dependencies

To enable this parameter, select the Enable Delimiter parameter.

For raw and ASCII files, specify how often the block reads data from the SD card. When you set this parameter to -1, Simulink determines the best sample time for the block based on the block context within the model.

For audio (wav) files, the block calculates the sample time (Ts) from the frame size (N) and sampling rate (Fs) of the selected audio file.

Ts = N / Fs

For example, if N is 4410 samples and Fs is 44,100 Hz, the block sample time is 4410/44,100 = 0.1 seconds.

N is the value that you specify in the Frame size parameter. Fs is the value that you specify in the Sample rate (Hz) parameter.

Specify the sample rate used by the audio input device to read audio data, in Hz. Larger frame sizes may result in a delay in your audio input. Ensure that the audio sampling frequency matches the input sampling frequency to avoid any issues in the output.

Tip

To find the sample rate of the specified audio file, use the audioinfo command in the MATLAB® Command Window. Copy the sample rate from the SampleRate property of the audioinfo command and paste the copied rate to the Sample rate (Hz) parameter.

Dependencies

To enable this parameter, set File type to Audio (wav).

Specify if the audio file is mono or stereo. If the audio file is mono, set this parameter value as 1. If the audio file is stereo, set this parameter value as 2.

Tip

To find the number of channels that the specified audio file supports, use the audioinfo command in the MATLAB Command Window. Set the Number of Channels parameter to the value displayed in the NumChannels property of the audioinfo command.

Dependencies

To enable this parameter, set File type to Audio (wav).

Specify the number of bits in each sample of audio.

Tip

To find the bits per sample for the specified audio file, use the audioinfo command in the MATLAB Command Window. Set the Bits per sample parameter to the value displayed in the BitsPerSample property of the audioinfo command.

Dependencies

To enable this parameter, set File type to Audio (wav).

Specify the number of samples in a frame. Larger frame sizes may result in a delay in your audio output.

Dependencies

To enable this parameter, set File type to Audio (wav).

Version History

Introduced in R2019b