In this Arduino Simulink Tutorial 1 – LED blinking we shows how to blink LED connected to the Arduino UNO board using simulink. This is the first simulink arduino example where we show what you need to download, how to download, how to configure simulink to work with external hardware and finally real time simulation. Blinking LED is like Hello World program when testing microcontroller based hardware for the first time. So it good to know because it will help you whether things are properly configured, whether things are correctly working or not and the basic steps.
It good to point out here that this tutorial is basically the same as LED blinking using arduino IDE sketch program where digitalWrite function is used or using matlab script program where writeDigitalPin function is used. The only difference is that this is done via simulink.
Installation of Simulink Support Package for Arduino Hardware
In order that Arduino hardware can communicate with Simulink you need to download and install Simulink Support Package for Arduino Hardware. If you don’t already have installed the Arduino hardware support for simulink follow this step to download and install the Simulink hardware support for Arduino.
To add Simulink Support Package for Arduino Hardware start Matlab Software and look for Add-Ons at the top. Click on that Add-Ons button and search for Simulink Support Package for Arduino Hardware and download and installed it.
This step is shown below.
Create Simulink Model
Create a new blank simulink model page. On the simulink model add the Pulse Generator from simulink/commonly used blocks or simulink/sources library and the Digital Output from the simulink support package for arduino hardware library/common.
How to add these is shown below.
Now simple connect the Pulse Generator to the Digital Output block as shown below.
Then double click the Digital Output block and change the Pin number to 8 then click ok to save. Then double click on the Pulse Generator block and change the sample time to 0.5 second. Click ok to exit. This is shown below.
Arduino Hardware Connection
Next step is to connect a LED to Arduino Hardware. Connect the LED positive to the Arduino digital pin 8 and the LED negative a 220Ohm resistor. Then connect the other end of the resistor to the ground. The connection diagram is shown in the schematic below.
Or as shown in the figure below.
Now plug in the USB to your computer.
Next we have to configure Simulink to use the Arduino Board and set the com port properly. To do this there are two ways. Either you can go to the Simulation > Model Configuration Parameter > Hardware Implementation and set your Arduino board to use there as shown below,
or, you can configure which board to use from the Tools > Run on Target Hardware > Options which is shown below.
And in both cases select your board to use. In this example, Arduino UNO has been used.
Running Simulation with External Hardware Arduino UNO
In this final step we run the simulink model. But before doing that we have to tell matlab/simulink to use the external device which in this case is the Arduino hardware. This is done by selecting External in the deployment option.Also enter “inf” into run time field. Click on Deploy to Hardware button or the run button to start the simulation. How to do this is also shown below.
At this point the LED should be blinking. If so you have successfully completed this tutorial Arduino Simulink Tutorial 1 – LED blinking.
If your LED is not blinking or if you get error then there are few things you try. Unplug and plug the USB cable and see it this solve the problem. Try to see if you can program using Arduino IDE and arduino program code.
See also other Arduino tutorials:
- Arduino Due tutorial – Blinking LED
- Real-Time data acquisition from Arduino to Matlab
- how to read analog signal with Arduino