1

First off. This is not in any way a class assignment. This is my own personal work and research. I just want to get that out of the way.

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.

I am trying to send some numerical data from Matlab (via a GUI) to my Arduino Uno and have the Arduino write it to my micro SC card. This is a temporary step to my larger project. However, there is no need to go into those specifics as they are outside of the scope of my issues.

I am fairly confident that the Matlab code works and the Arduino code is slightly modified from another project I did where I wrote and read random numbers from my micro SD card.

However, as I run the Matlab code, the Arduino blinks as if it is receiving the data but after I check the micro SD card it remains blank.

I am confident that my Arduino is wired correctly to my micro SD card adapter since this remains the same from my prior project.

Therefore, I am sure I am missing something trivial to get it to work.

I have researched several websites on the subject and their method and mine seem to align very well.

I am fairly certain the problem is in the conditional statement:

if (Serial.available() > 0) {

As you will see.

The Matlab code is below:

arduinoCom = serial('COM3', 'BaudRate', 115200);  
sendData = 5;
fopen(arduinoCom);
fprintf(arduinoCom,'%i',sendData); %this will send 5 to the arduino
fclose(arduinoCom); 
delete(arduinoCom);

The Arduino code is as follows:

#include <SD.h> // load SD library

int chipSelect = 4;  // 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(115200); // start serial connection to print out debug messages and data
     pinMode(chipSelect, OUTPUT); // chip select pin must be set to OUTPUT mode
     while (!Serial) {
     }
}

void loop() {
        // Open file, Write data, Close file only when you receive data
        if (Serial.available() > 0) {
           incomingByte = Serial.read();
           SDF = SD.open("SerialDataFile.txt", FILE_WRITE); // open "SerialDataFile.txt" to write data
           SDF.println(incomingByte, DEC); // write ASCII-encoded decimal number to file
           SDF.close(); // close file
           }
}  

The expected result would be a file "SerialDataFile.txt" stored on my micro SD card with the value 5.

Thank you for your help!

7
  • Some moderator could migrate this question to the Arduino Exchange please? Commented Jan 21, 2019 at 3:31
  • 1
    @Brethlosze - Apologies if this was the wrong location. Is there a way I can migrate it myself? Commented Jan 21, 2019 at 3:54
  • 1
    I dont think it is wrong, ... but perhaps you can wait some moderator to move it, or do it on your own. In any case the community is arduino.stackexchange.com. Commented Jan 21, 2019 at 4:06
  • 1
    The Matlab code looks ok, that should be enough to send the bytes. Instead Matlab try just the console or the Arduino serial simulator for sending information, and then move into Matlab. Commented Jan 21, 2019 at 4:13
  • @Brethlosze - I posted this at the location you suggested. Should I just delete this post? Commented Jan 21, 2019 at 18:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.