Using Arduino / Microcontrollers with MATLAB

Microcontrollers are great for simple data processing, but sometimes you need a little more "oomph" for your project.

AlexWulff
over 4 years ago

In this article, I'll show you how you can use MATLAB to read in data from a microcontroller and save it as you need it. The code used in this project requires MATLAB R2019b.

To facilitate sending data from your microcontroller to MATLAB we'll use a serial connection. Sending data from the microcontroller is as easy as calling Serial.print(). Serial connections on many devices are implemented at a hardware level, meaning that while you send data to your computer your microcontroller is free to do other things.

There are many instances in which you might want to use MATLAB for additional processing. One of my favorites is the large variety of plotting options available in MATLAB. You can use your microcontroller as a data source, and display the data in real time on a highly-customizable MATLAB plot.

Plotting data as a function of two sensors

MATLAB also makes it very easy to perform data analysis. Rather than write complicated C code to analyze your data on the microcontroller you can read it into MATLAB and do the analysis there. A common example of this is performing a Fourier transform, which is much more complicated to compute and display in C than in MATLAB.

To begin, you'll need a microcontroller with serial support (most microcontrollers except for those based on the ATtiny series of processors have the Serial library). If you've used Serial.print() on your board before then it should be OK. We can start with a very simple example sketch for the microcontroller:

This sketch initiates a serial connection to the computer and then outputs a random value between 0 and 1023 every 30 ms. Connect your board to your computer and upload this sketch. The Arduino IDE also uses a serial connection to upload code to your board. Only one program can access a given port at a time, so quit out of the IDE once the sketch is uploaded to free the port.

Next, fire up MATLAB R2019b or later and type the following code into the command prompt:

ports = serialportlist; ports(end)

If this command outputs the port that you selected to upload code to your microcontroller then you're all set. If it doesn't, find the correct port in the ports list by printing the ports variable.

The MATLAB code for the rest of this article is below. Create a new MATLAB file and copy this in:

This code opens the serial connection to the microcontroller, and prompts the user for the name of a file into which it will write the output. The program will then read data line by line as it becomes available and save this to a vector of user-configurable length. The data is also displayed in real time as it is collected. After it finishes collecting, the program will write the data out to a file.

If you needed to select a different port than the last one in the port list, replace ports(end) with a string of the correct port. You can also change the amount of data sampled and the width of the plot by changing sample_len and plotWinSize variables.

The sampling rate is configured by the delay() function in the Arduino code. Increase the delay value to decrease the sampling rate, and vice-versa. After the data is collected you can either manipulate the data vector directly or interact with the outputted file.

Hopefully you found this short article helpful! If you want to see more of my work, check out my Hackster profile, my website, and my book.

AlexWulff

I'm a maker and student at Harvard. I love Arduino, embedded systems, radio, 3D printing, and iOS development. www.AlexWulff.com

Latest Articles