F. Saving the Experimental Data

How the Metro M4 Sends Data to Matlab

The Adafruit Metro M4 is able to send data directly to the computer via the serial port. A Matlab script is able to then capture the data directly and automatically plot it. Here is the code from the Arduino sketch that sends the data.

Note that the Arduino serial monitor and the Matlab script can’t run at the same time. Both will vie for exclusive access to the serial communication port. When the user initiates the Matlab script, there is initially a handshaking routine between the Metro M4 sketch and Matlab before any data transfer commences. During the data transfer, the function (Serial.availableForWrite()) assesses whether more data can be transferred to the serial port. If the Arduino sketch keeps writing data but the Matlab script does not read it, the communication channel will eventually be clogged. Now, here is the code from the Matlab script file that receives the data from the serial port.

The function (instrfind()) is used to page all serial ports that might be opened by Matlab previously and close them. The Matlab script then looks for any serial ports that are currently active using (seriallist). The script then opens a channel to the last active serial port using (serial). After the handshaking routine, the script reads the data from the Arduino microcontroller and prints out time plots and table summaries of the data in the format shown in the web page Results > Readouts. The (csvwrite) command at the end of the script will save the data to a .csv file.

Extracting the data file from the flash memory

After an experiment is over and the data is sent to Matlab, double-click the reset button. METROM4BOOT will appear in an Explorer window. Using your mouse, drag the [adafruit-circuitpython-metro_m4_express-en_US-4.1.0.uf2] file to the METROM4BOOT drive icon in Explorer. The METROM4BOOT drive will be renamed Usb Drive. This will display the flash memory’s contents. If the file [data.csv] was properly written, you will see it in the Explorer window. Cut and paste it to your computer.

Here is a link to the complete Matlab script that can receive the data from the Adafruit Metro M4 and plot the data: PlotMagMtnResp.m

Here is a link to a Matlab script that can plot data that was previously saved to a CSV file: PlotMagMtnResp2.m