In this tutorial, we will use MATLAB to turn on the built-in LED over Wi-FI, made possible by the NINA module embedded on the Nano 33 IoT board and MATLAB® Support Package for Arduino® Hardware.
The goals of this project are:
The Arduino Nano 33 IoT, Nano Motor Carrier, Micro USB cable and single cell battery are included as part of the Arduino Engineering Kit R2
A valid MATLAB® license 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.
When you connect your Arduino board, you should see the COM port under Device Manager. By referencing the COM port, you can create a two-way data link between your computer and the Arduino device. For a device connected on a network, we can use an Internet Protocol (IP) address to identify it. In the IPv4 standard, this is written as a series of four numbers such as
. By default, most routers are configured to assign a dynamic address via a Dynamic Host Configuration Protocol (DHCP). This means less configuration, but at the cost of the IP address changing. In order to reliably address your Arduino board, you may wish to utilise a static IP address. Once a static IP address is assigned, it will stay the same.
The LED in the Arduino Nano 33 IoT is connected to Digital Pin 13. By pulling this pin HIGH, we can turn the LED on and by pulling it LOW we can turn the LED off.
1. Place the Nano 33 IoT in the Nano Motor Carrier. Make sure to orient it correctly (the Micro USB connector is on the same side as the blue terminal).
2. Connect the battery. Make sure that the polarity is respected. The negative terminal of the battery should go through the black wire to ground, and the positive terminal should go through the red cable to
3. Turn on the board and connect it to your computer via a Micro USB cable.
The board should be turned on before connecting to the computer, otherwise the board may not function correctly.
4. Open MATLAB and run the command
in the Command Window.
5. 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.
6. Choose the "Nano33IoT" 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.
In order to use the full functionality of the Nano Motor Carrier, you must ensure that the I2C and ArduinoMotorCarrier components are selected.
7. 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.
8. The Arduino Nano 33 IoT 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.
9. Enter the command
. This command is similar to the
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
10. 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
11. See what happens when you change the pause duration. Try replacing the while with an
statement to make it blink a certain number of times.
1. Firstly, make sure your Arduino board is connected via the USB cable. Run
to remove the previously created arduino object and then run the
script again. This time, select the Wi-Fi option. You will see that a few options appear at the bottom left. Select your Wi-Fi encryption type and enter your SSID, password and port. By default, the Arduino board will obtain a dynamic IP from your router.
In order to have a static IP address, check the relevant box and enter in the IP address manually. Some networks/firewalls may disable port connections. Contact your network administrator if needed.
2. Select your board, choose the COM port and hit
3. Click on
to verify that the Wi-Fi connectivity functions correctly and write down the device address and port.
4. Enter the following command
, where device address and port are the values you wrote down in the last step. If successful, the
a = arduino('device address','Nano33IoT',port)
object will be added to the MATLAB workspace.
If you wish to re-run the
function again, it is advised to clear any existing Arduino objects from the Workspace. In this example,
is the Arduino object so we would run the command
. Alternatively, enter
to empty the workspace.
5. Enter the command
. Try turning the LED on and off several times
If the code is not working, there are some common issues we can troubleshoot:
You can now control your Arduino Nano 33 IoT via MATLAB by a wired or wireless connection to control your devices and make use of the powerful features for scientific computing and developing engineering applications!
You can also use the command line arguments to upload the MATLAB server to the Nano 33 IoT via the
command. See more information on using this function in the MathWorks documentation.
Try writing a MATLAB code that slowly increases and then decreases the blink speed.
Write a MATLAB function that allows you to change the speed based on input arguments.
function to control a LED on an external Pin.
Try moving a DC/Servo motor via Wi-Fi.