This example shows how to use Simulink® Support Package for Arduino® Hardware to run a Simulink model on Arduino board.
Supported Hardware:
ArduinoLeonardo
ArduinoMega 2560
ArduinoMega ADK
ArduinoMicro
ArduinoNano 3.0
ArduinoUno
ArduinoDue
ArduinoMKR1000
ArduinoMKR WIFI 1010
ArduinoMKR ZERO
ArduinoNano 33 IoT
ArduinoNano 33 BLE Sense
Available versions of this example:
ArduinoMega 2560 board:arduino_gettingstarted
The provided model is pre-configured for Arduino Mega 2560 and can be run on any of the board listed in theSupported Hardware部分,通过改变Hardware boardparameter in the Model Configuration Parameters dialog box of the model as described in Task 4 of this example.
Simulink Support Package for Arduino Hardware enables you to create and run Simulink models on Arduino board. The target includes a library of Simulink blocks for configuring and accessing Arduino sensors, actuators and communication interfaces. Additionally, the target enables you to monitor and tune algorithms running on Arduino board from the same Simulink models from which you developed the algorithms.
In this example you will learn how to create and run a simple Simulink model on Arduino board. See other examples for Arduino board to learn how to use External mode and to learn how to implement more complex algorithms.
If you are new to Simulink, we recommend completing theInteractive Simulink Tutorial,Get Started with Simulink, andSimulink Getting Startedvideo.
To run this example you will need the following hardware:
Supported Arduino board
USB cable
LED
220 Ohm resistor
Breadboard wires
Small breadboard (recommended)
Consider this Simulink model.
In this task, you will connect an LED to an Arduino output pin so you can see changes in the logical state of the pin.
1.Attach one end of the 220 Ohm resistor to output pin 9 on the Arduino board. Use the recommended breadboard and the breadboard wires.
2.Attach the long leg (positive) of the LED to the resistor. Attach the short leg (negative) to the ground pin on the Arduino board.
Simulink Support Package for Arduino Hardware provides an easy way to create algorithms that use Arduino sensors and actuators by using the blocks that can be added to your Simulink model. The blocks are used to configure the associated sensors and actuators, as well as to read and write data to them.
1.EnterslLibraryBrowserat the MATLAB® prompt. This opens the Simulink Library Browser.
2.In the Simulink Library Browser, navigate toSimulink Support Package for Arduino Hardware > Common.
3.Double-click theDigital Output块。检查块面具,它包含一个description of the block and parameters for configuring the associated Arduino digital output pin.
In this task, you will create a simple Simulink model that changes the state of the Arduino digital output pin.
1.In MATLAB, selectHome>New>Simulink Model.
2.Drag thePulse Generatorblock from the SimulinkSourceslibrary to your model.
3.Double-click thePulse Generator块。Set thePulse typeto parameter toSample basedand set theSample timeparameter to 0.1 second.
4.Drag theDigital Outputblock to the model. Use the default block settings.
5.Connect thePulse Generatorblock to theDigital Output块。
In this task, you will configure and run your model on the supported Arduino board.
1.Connect the Arduino board to your computer with a USB cable.
2.In your Simulink model, clickSimulation > Model Configuration Parametersto openConfiguration Parametersdialog.
3.Select theHardware Implementationpane and select your required Arduino hardware from theHardware boardparameter list. Do not change any other settings.
4.ClickOK.
5.On theHardwaretab of the Simulink model, in theModesection, selectRun on boardand then clickBuild, Deploy & Start. The Simulink model will now be deployed to the connected Arduino hardware.
6.Observe the LED connected to pin 9. The LED should blink one time every second.
7.Save your Simulink model.
Apre-configured modelis included for your convenience.
Experiment with other blocks in the Arduino block library. For example:
Create and run a model that turns the LED on if a signal is applied to a digital input pin.
Create and run a model that repeatedly brightens and dims an LED. Hint: use the PWM block.