MATLAB Datalogger with Arduino
“As an Amazon Associates Program member, clicking on links may result in Maker Portal receiving a small commission that helps support future projects.”
MATLAB is a powerful tool utilized by over 4 million engineers and scientists around the world [MATLAB facts]. It is often the first programming language exposed to students during university and is also a helpful stepping stone for transitioning into data analysis and numerical algorithm development. In this tutorial, I will introduce MATLAB as an interface for data acquisition from an Arduino board. The Arduino, in this particular case, will communicate with a Windows computer via the serial port and send data from an Arduino-compatible sensor, which will subsequently be read by MATLAB through its serial communication library. Serial communication from hardware to MATLAB is very simple and requires only a few lines of code. I will also introduce a real-time analysis and plotting routine to visualize the Arduino data as it arrives in real time. This particular method of data analysis and visualization in real time is incredibly useful for engineers interested in experimentation where microcontrollers and sensors may be used, along with complex data acquisition systems.
In my particular case, I will be using the AMG8833 infrared thermal camera sensor that is fully compatible with Arduino. I’m using the AMG8833 to test the limits of the serial upload speed and my computer’s ability to update a real-time plot of the 8x8 pixel temperature array data. It is an interesting project that has real-world applications in heat transfer, equipment health monitoring, and even military applications. The parts list used in this experiment is given below:
Wiring the AMG8833 to an Arduino board is simple - all the AMG8833 needs is power and I2C communication with the following connections:
|AMG8833 Pin||Arduino Uno Pin|
In the next section, the basics of printing from Arduino and reading from MATLAB will be introduced.
Here, I’m using randomized integers in Arduino that are printed to the serial port. Then, MATLAB reads the random data and plots it in real time. The code for the Arduino portion is shown first, followed by the MATLAB code.
The general process for reading data from the serial port involves:
Setting the correct serial port
Setting the baud rate
Opening the serial port
Reading from the serial port
This process is shown below:
The resulting output variable named ‘out’ contains the printed data plus a carriage return and newline. We can split the output using the MATLAB ‘split(out,‘,’)’ function, which separates the comma-separated values. We can finally convert the values to doubles using the ‘str2double()’ function. This process of reading from the serial port and converting to doubles is shown below:
Next, if we want to create a loop for iterating through data and plotting in real time, we need to setup a more complex system for plotting. The MATLAB code for reading the Arduino serial data in real time is given below:
The resulting output plot created by MATLAB is shown below:
The following process should be followed when carrying-out this MATLAB serial port read procedure:
Upload sketch to Arduino that prints out to the serial port
Ensure the correct device comm port is inputted. In Windows, my port serial read port is ‘COM6,’ so the serial read is updated to : “serial('COM6')”
Run MATLAB code
When finished or if there is an error, hit the ‘Break Loop’ button in the bottom left corner of the plot (similar to that shown above)
In the next section, I will read AMG8833 thermal camera data from the Arduino serial printout, and use MATLAB to plot the 64 data points to populate a mesh plot that represents the 8x8 thermal temperature array.
In a similar method as above, we can read the AMG8833 Thermal Array sensor over the serial port by printing all 64 pixels onto the serial port from the Arduino side, then parsing the 64 pixels back on the MATLAB side. The Arduino code is shown below, where the 64 pixels are read and printed to the serial port, then at the end of the 64th pixel, we print a carriage return to notify MATLAB that the AMG8833 printout has finished.
On the MATLAB side, we have to read the 64 AMG8833 pixels as a string from the serial port, convert each to a double, rearrange them as a 8x8 grid, and plot them onto our figure. This is done in the code given below:
With the subsequent figure plotted out as a result:
If we wanted to create a real-time plot, similar to the single-variable real-time plot above, we can incorporate a more complicated routine to plot the thermal camera in real time. I also included an interpolation scheme to scale up the pixels from 8x8 to 100x100. The code is shown below:
The resulting output should produce real-time images that look similar to the one shown below (and also the GIF at the top of this page):
MATLAB is a powerful processing tool used by researchers and data analysts because of its vast set of libraries and wide-reaching community of users. In this tutorial, MATLAB’s serial tool was used to read Arduino data from the serial port in real time. This type of procedure could be useful for real-time data acquisition and data visualization. In the particular example given above, the AMG8833 thermal camera was used to create real-time thermal imagery. Thermal imagery can be used to diagnose hotspots in machine health, intruder alert systems, HVAC leakage detection, and much more. This short introduction into data acquisition with MATLAB acts as a starting point for anyone interested in interfacing their computer with Arduino, while also harnessing the powerful computational power of MATLAB.
See More in MATLAB and Data Analysis: