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 Rev 2
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 Rev 2.
MATLAB® is a programming platform in education, academia and industry to analyse data, perform simulations and carry out model based design. By communicating interactively 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 can see the COM port in 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 reliabily 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 to your computer via a Micro USB cable.
The board should be 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 setup the MATLAB-Arduino connection. To establish the connection over the USB cable, make sure the USB radio box is selected then click on Next.
6. Choose the Nano33IoT from the dropdown menu, as well as the relavent COM port. Click on the blue Program 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. You can review the connection type, Port, board and loaded libraries. Click on Test connection to evaluate the Arduino-MATLAB connection. You should see a Green checkmark signalling the successful connection.
8. The Arduino Nano 33 IoT is now configured to interface with MATLAB. Now to create the object in MATLAB, we run the command
. The properties of the object, including the COM port, are displayed.
9. Enter the command
. This command is similar to the
by the Arduino programming language. 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 the Arduino IDE, you cannot address the built in LED using
10. 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 if statement to make it blink a certain number of times.
1. Make sure the Arduino board is connected via the USB cable. Run
to remove the previously created arduino object. Then, run the
script again. This time, select the Wi-Fi option. You will see that a few options appear in 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. 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 exsisting 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.
1. 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
2. Try writing a MATLAB code that slowly increases then decreases the blink speed
3. Write a MATLAB function that allows you to change the speed based on input arguments
4. Use the writePWMVoltage function to control a LED on an external Pin
5. Try moving a DC/Servo motor via Wi-Fi