Search code examples
matlabarduinomicrophonezero

How can I save Data from an Arduino mkr zreo to matlab?


I am using an Arduino mkr zero for recording audio with a mems-microphone. Now I want to get the Data from the Arduino into matlab for further evaluation. The Arduino support package doesn´t support the mkr zero only the mkr1000. Is there an easy way to get data saved directly to matlab or at least save it to a .txt and read it afterwards in matlab?


Solution

  • So there are a couple of options for doing this, the choice of which probably just depends on preference / experience.

    Send the data directly to Matlab

    Matlab already has built a built in Serial Object for reading and writing to the Serial ports. Below is an example of using Serial in Matlab to open, read, write and close the serial port (with "COM1" being a windows based device, see docs for more info).

    s = serial('COM1','BaudRate',1200);
    fopen(s)
    fprintf(s,'*IDN?')
    idn = fscanf(s);
    fclose(s)
    

    So you can write the data directly from the Arduino's Serial.write() and if Matlab is listening at the correct baudrate then job's a good'n. I would recommend as high a baudrate it can handle if you wanted anything in real-time, but it doesn't seem like you need it, so you can always send the data a bit delayed and buffer it (maybe to a text file or .mat file if you needed it).

    Send the data to another program

    Since Matlab to the user, (generally speaking excluding toolboxes) appears single threaded, it may make more sense to program another program specifically for recieving this serial data. Using another language may also run a lot quicker than Matlab and could help learn new skills in the process.

    You will find many examples of reading Serial data in many languages including just a few I have attached for reference; the idea being you write the data to a text file and read it from Matlab when Matlab is ready: C#, Java, Rust, Python and etc...

    If you wanted to get fancy you could do all of the Serial reading in another language and send it to Matlab via local network sockets. Or even use Java's native interface with Matlab to handle multiple Arduino's sending data at the same time (probably unecessary).

    Summary

    I would probably go with the first option if you want something simple to setup and gets the job done quick, but would maybe look at an alternative for a more permanent solution.

    Extra hardcore method (You've been warned)

    I'm assuming you're using the I2S for audio recording? So the SPI would be free to transmit all of the data to the PC via SPI for example. You could use a breakout module to convert your SPI messages to I2C and then the one I linked has Virtual COM ports so could again act as serial. Or you could build a custom driver to read the messages coming in over I2C. Maybe you could push the speed higher than the current Serial USB port? Would be cool to compare them.