Using MATLAB and Arduino for Motor Control
This hands-on tutorial shows how commands from MATLAB® Support Package for Arduino® can be used to control DC, stepper, and servo motors. We use Adafruit Motor Shield V2 and Arduino Due with MATLAB and step through a series of MATLAB commands to show these motors in action.
Published: 7 Apr 2015
In this demo, we are going to see how to control motors using MATLAB Support Package for Arduino. For software, I have MATLAB and MATLAB Support Package for Arduino pre-installed on my machine. If you do not have the Support Package installed, you can go to the add-ons tab and click on Get Hardware Support Packages to download and install the Support Package. For the hardware, I have Arduino Due, Motor Party Pack for Arduino, DC Battery Pack, and a USB Micro-b Cable.
To follow along with this demo, and to make the connections that I have, connect the Servo 1 pin on the Motor Shield to the Servo Motor. And one pin to the DC Motor, and M3 and M4 pins to the Stepper Motor. It is important to note that the DC Battery Pack is necessary to control the DC Motor and the Stepper Motor. Connect the DC Battery Pack to the power pin. The Micro-b end of the cable needs to be connected to the programming -ort on the Due, and the USB end to the PC on which you have MATLAB installed. For more detailed instructions on how to make connections, please follow this link.
Now that we have made all the necessary connections, let us go to MATLAB and see how we can control motors. For this, I have written a script that I am going to show. The Arduino function in this script establishes a connection with the Due that disconnected on the com port mentioned in the first input. This function also provides instructions to MATLAB on the libraries that need to be included in the Arduino Server Code. When I execute this in MATLAB Command Window, MATLAB creates an object that represents the physical object it is connected to.
The add-on function can then operate on the object that was created to connect to the Motor Shield. Executing a command on the Command Window and running a MATLAB script has the same effect. So in the rest of this video, I'm going to use the button to go to the next section of Code after executing the current section.
MATLAB creates a new object in the workspace every time it connects to a new physical object. In this case, the Motor Shield. Here in this section of Code, the Servo and right position functions are the ones we need to pay attention to. Servo function lets you communicate from MATLAB to the Servo Motor that is connected in the port number mentioned in the second input. The right position function allows you to command the Servo Motor to move to the position mentioned. Here, I'm using a 4 loop to move the motor from 0 to 180 degrees five times, back and forth. I've attached an image of a wheel here to the Servo Motor to show it in action.
In the next section of Cord, the Stepper function helps us establish a connection to the Stepper Motor that is connected on Port 2. The third input to this function specifies the number of steps the Stepper Motor must take in every revolution. The Move function commands the Stepper Motor to move the number of steps that is mentioned in the second input. Let us see if MATLAB can make the ballerina dance to its tunes.
To communicate with the last type of motor that comes along with the Motor Party Pack from MATLAB, the DC Motor function can be used. The stop and start comment does exactly what the name suggests. Remember, these functions can only be used after the speed of the motor is mentioned. The speed of the motor must be mentioned as a percentage value for the maximum speed of the motor. The hypnotizing disc here shows the working of the DC Motor.
With that, we have seen how to control motors using MATLAB and Support Package for Arduino.