In this tutorial, we will use MATLAB® to turn on an external LED, and then to adjust the output using MATLAB® Support Package for Arduino® Hardware.
The goals of this project are:
A valid MATLAB® licence is needed. Your workplace or education institution may have a subscription. Alternatively, a one-year trial subscription to MATLAB® is included as part of the Arduino Engineering Kit R2.
MATLAB® is an educational and industrial programming platform used to analyse data, perform simulations and carry out model based designs. Through an interactive communication with an Arduino board, you can expand MATLAB®'s capabilities, while also gaining access to a wide range of math, engineering and plotting functions. Check out the capabilities of MATLAB® over on the MathWorks website. In this tutorial, we will establish the connection with MATLAB® via a USB and Wi-Fi connection. The MATLAB® Support Package for Arduino Hardware must be installed (which may require a MathWorks® account), in order to facilitate the communication between the MATLAB® software on your computer and your Arduino board.
1. Connect your board to the computer via the USB 2.0 Cable Type A/B.
2. Open MATLAB and run the command
in the Command Window.
3. A Graphical User Interface (GUI) appears, that will help you set up the MATLAB®-Arduino connection. To establish the connection over the USB cable, make sure that the USB radio box is selected and then click on Next.
4. Choose the "UNO" from the dropdown menu, as well as the relevant COM port, and click on the blue
button to upload the Arduino Server to the board. When you have done so, click on next.
5. In this step you can review the connection type, Port, board and loaded libraries. You can also click on Test connection to evaluate the Arduino-MATLAB® connection. Next, you should see a Green check-mark signalling the successful connection, as shown in the image below.
6. The UNO R3 is now configured to interact with MATLAB®. Now to create the object in MATLAB®, we run the command
. The properties of the object, including the COM port, are displayed as shown in the example below.
a = arduino()
7. Enter the command
. This command is similar to the
writeDigitalPin(a, 'D13', 1);
by the Arduino programming language. Which means that digital pin 13 D13 is connected to the built-in LED,
is the Arduino object we created, and 1 represents a HIGH or ON state. Try turning the LED ON and OFF several times by changing 1 to 0 and vice-versa.
Unlike in the Arduino IDE, here you cannot address the built-in LED using the
8. Now, you can use this command to continuously blink an LED as part of a
loop. Try entering the script below.
1while (1)2 writeDigitalPin(a, 'D13', 0);3 pause(0.5);4 writeDigitalPin(a, 'D13', 1);5 pause(0.5);6end
On one breadboard there is a white jumper wire sending 5 volts to the potentiometer. The orange cable is connected to GND(Ground) and the gray wire is our input to A0. A0 will read the variable voltage coming in from pin13 after being adjusted through the potentiometer. On the right breadboard we have a simple LED circuit comprised of a red output wire from pin11, a resistor and the LED finally connected to GND via the green wire.
Make sure to connect the longer leg of the LED (anode) to the resistor and the shorter (cathode) to GND.
1. Set your output to 1 in the previously mentioned way using
. As mentioned earlier, 1 means ON or HIGH and sets the digital pin 13 D13 to 5V allowing the current flow towards the LED. Check to see if the analog pin 0 A0 is actually reading an adjustable voltage using
writeDigitalPin(a, 'D13', 1);
in a loop and then use
to print the value of
on the "Command Window".
1a = arduino();2writeDigitalPin(a, 'D13', 1)3while(1)4 Volts = readVoltage(a, 'A0');5 display(Volts);6 pause(0.5)7end
The amount assigned to
should be changing as you rotate the potentiometer's knob.
2. Set the value of
as the voltage to be set on digital pin 11 D11 using the command:
. This command is adjustable between 0 and 5 volts. It will convert the value to a PWM Signal on the digital pin.
1a = arduino();2writeDigitalPin(a, 'D13', 1)3while(1)4 Volts = readVoltage(a, 'A0');5 writePWMVoltage(a, 'D11', Volts)6 display(Volts);7 pause(0.5)8end
, for example
will clear the Arduino object.
ensure that you have installed the MATLAB® Support Package for Arduino Hardware.
Unrecognized function or variable 'arduinosetup'
You can now control your output using variable inputs to control your devices and make use of the powerful features for scientific computing and developing engineering applications! Different input modulators similar to the potentiometer processed by the various toolboxes offered my MATLAB can lead to infinite types of output beyond that of the brightness of an LED.
command. See more information on using this function in the MathWorks® documentation.