Skip to main content Skip to local navigation

Matlab and Arduino

Matlab and Arduino

The programming and embedded systems curriculum in engineering should be constantly revised as the underlying methods and technologies are constantly changing.

Here, I'm exploring how we can update the systems we use in our first year programming class at the Lassonde School.    Low barriers to entry are key as many of our students have a negative perception of programming and electronics or don't realize how relevant it might be to their future education and careers.  So, one of the options is to use Arduino hardware with Matlab.

 

Arduino MKR from Matlab with a RC Servo

Driving a 5v RC Servo motor using the 3.3v Arduino MKR Zero from Matlab 2019b.

While the 8-bit Arduino Uno is appealing for all kinds of reasons, the 32-bit Arduinos, like the MKR Zero, is more future-oriented: Python-compatible, the SWD debugger pads are available.  It's also compatible with the 2019b edition of MATLAB.  While we currently use Java in the 2nd programming class, Python is a potential future option.

So far, I've been able to run the MKR Zero using the Arduino 1.8 IDE and MATLAB 2019b.

To run the servo, here are examples of commands I used:

>> % Connect servo to 5v, Ground and GPIO D7.

>> % Connect USB from Arduino MKR Zero to computer

>> % Matlab flashed the MKR Zero already with an executable 

>> % that will "listen" to commands from Matlab.

>> clear all

>> a = arduino();           % connect to the Arduino from Matlab.

>> s = servo(a, 'd7')     % signal line on GPIO D7

>> s.writePosition(0.5)  % move the servo to a new angle (values 0.0 to 1.0)

Before I ran commands from Matlab to move the servo I had to get Matlab to write a program to the Arduino MKR Zero that would listen to commands over the USB line.

Matlab allows you to place specific elements for the "server" that it places on the Arduino.

Matlab allows you to place specific elements for the "server" that it places on the Arduino.

Matlab flashes "listening" code on the MKR Zero

Matlab flashes "server" code on the MKR Zero

Matlab tests the connection after updating the "listening" code

Matlab tests the connection after updating the "server" code