MATLAB Answers

Difficulty sending data from Matlab R2018b to Arduino Uno to Micro SD

8 views (last 30 days)
dsmalenb on 22 Jan 2019
Commented: Andrew Borghesani on 30 Jan 2019
Dear Fellow Matlab Enthusiasts!
Disclaimer: This is my own personal project and is not related to a school assignment, etc.
I am learning how to use Matlab with various Arduino projects. I am a seasoned Matlab user but I am fairly new to the entire Arduino space. Ironically, I think my issue is with Matlab when it pertains to interfacing Matlab and the Arduino Uno.
My objective is to send some numerical data from Matlab 18b to my Arduino Uno and then have the Arduino write it to my micro SD card. My actual project is more involved but this is where I am stuck.
The micro SD card to Arduino Uno pin configurations are as follows:
  • CS <-> pin 10
  • MOSI <-> pin 11
  • SCK <-> pin 12
  • MISO <-> pin 13
In addition, I have installed the Matlab Support Package for Arduino Hardware "Upload Arduino Server":
  • I configured it for USB.
  • Chose Board: Uno
  • Choose Port: COM3
  • Libraries included: Adafruit/MotorShieldV2, I2C, RotaryEncoder, SPI, Servo, ShiftRegister
My Matlab code is as follows:
arduinoCom = serial('COM3'); % insert your serial properties here
set(arduinoCom, 'BaudRate', 9600);
set(arduinoCom, 'Parity','none');
set(arduinoCom, 'DataBits',8);
set(arduinoCom, 'StopBit',1);
sendData = 5;
fprintf(arduinoCom,'%i', sendData); %this will send '5' to the arduino
My Arduino code is as follows:
#include <SPI.h>
#include <SD.h> // load SD library
int chipSelect = 10; // Chip select pin for the MicroSD Card Adapter
int incomingByte = 0; // for incoming serial data.
File SDF; // Serial data received is saved here.
void setup() {
Serial.begin(9600); // start serial connection to print out debug messages and data
pinMode(chipSelect, OUTPUT); // chip select pin must be set to OUTPUT mode
if (!SD.begin(chipSelect)) { // Initialize SD card
Serial.println("Could not initialize SD card."); // if return value is false, something went wrong.
Serial.println("SD card initialized.");
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
Serial.println("Serial Connected");
void loop() {
// Open file, Write data, Close file only when you receive data
if (Serial.available() > 0) {
SDF ="DATA.txt", FILE_WRITE); // open "SerialDataFile.txt" to write data
//Serial.println("Serial Available");
incomingByte =;
//incomingByte = incomingByte - 48;
SDF.print(incomingByte, DEC); // write ASCII-encoded decimal number to file
SDF.close(); // close file
Serial.println(incomingByte, DEC); // debug output: show written number in serial monitor
Several observations:
  1. The arduino does not seem to get output. It certainly does not not write anything to the micro SD.
  2. I am able to write data to the microSD from the serial terminal.
I expect that the issue is with my Matlab code with how it communicates with the Arduino Uno microcontroller.
I would appreciate any assistance the community can offer!

  1 Comment

Andrew Borghesani
Andrew Borghesani on 30 Jan 2019
Are you able to write data to the SD card using the serial interface from the arduino IDE?
Please also note that when installing the MATLAB support package for arduino, all code currently on the board was wiped. The support package is not necessary for communicating with the Arduino via serial.

Sign in to comment.

Answers (0)

Sign in to answer this question.