Build Arduino-Simulink Model & Run on Hardware

It is very easy to drag and drop blocks in MATLAB Simulink library and use them making electrical system/circuit you want. One can Deploy the electrical simulink model into the microcontroller directly and run system as independent of MATLAB. Also with a few easiest steps (explained below) one can run Arduino MATLAB in external mode observing, recording, communicating electrical circuit in real time and executing further commands. (For a Quick Review watch videos)

Note:Before building and run electrical system on Arduino deployed by MATLAB Simulink, make sure you have MATLAB installed in your computer with Arduino Support Package in its Simulink library. if not, don’t worry, Click here to learn about support packages and easiest steps for installing MATLAB-Arduino Support Package

Adding Blocks from Library to Simulink Model

1.  Go to the Simulink-Arduino tool and click common modes and then click on the digital output block. By right clicking it, options will be available. Click on add to new model (Figure 1)

 Digital output block, Add to new Model
Figure 1: Digital Output Block, Add to new Simulink Model
2.  From the Simulink Sink library, find the block for Pulse generator. Also add this block to the new model. It will look like as in figure 4.

3.  Join both the blocks by clicking the first block and pressing cntrl key and then clicking            other block. (Figure 2)

Pulse generator with digital output block in Simulink Model
Figure 2: Pulse generator with digital output block in Simulink Model

4.  Double Click on the blocks to change the variables and the output pin of the Arduino.

     (For Digital output from Arduino, we can select any digital pin. For Arduino mega 2560 and Due, you can select pin from 0 to 53. For Arduino UNO and Nano 0-13.) (Figure 3)

Setting Output Digital Pin of Arduino
Figure 3: Setting Output Digital Pin of Arduino

5.  Similarly, set the parameters of Pulse generator such that for how much time you want        ON and OFF the output pin.

6.  We are going to blink the LED in this simple example so we will give PWM of 50% for 1        sec. (Figure 4)

Pulse generator Properties
Figure 4: Pulse generator Properties


Deploy to Hardware:

1.  Click on the tools menu in menu bar and click on the Run on Target Hardware and then click on Prepare to Run. (Figure 5)

Prepare to Run on Hardware i.e. Arduino
Figure 5: Prepare to Run on Hardware i.e. Arduino

2.  When you will click on prepare to Run, a window will open of configuration parameter. In the left side there are further options, click on the Run on Target Hardware in the last.

3.  It will show further option asking of what type of hardware you attached with your computer (depending upon Support Packages you installed in Simulink). Find your attached hardware in dropdown menu. (Figure 6)

Run on Target Hardware
Figure 6: Run on Target Hardware (1.) Click Run on Target. (2.) Selecting the Hardware attached

4.  Finally, the moment comes when you click on the option on the top right side of Simulink saying deploy to hardware (or click cntrl+B). (Figure 7)

Deploy to hardware.
Figure 7: Deploy to hardware.

This will run on Arduino board without needing signal from MATLAB as stand-alone system. Arduino Board just need power which can be provided using external adapter or through USB cable.

Hardware Portion:

For the above system you just need a LED, a resistor of 220 ohm and connection wires.

1. Find the pin in Arduino board which you specified in Simulink

2. Take out a wire from it and connect with Anode/positive leg of LED in Bread Board
3. From the negative leg of LED, connect one leg of 220 ohm resistor.

4. Place the other leg of resistor to the Ground wire from the Arduino Board. (Figure 8)

Hardware of Basic Blink LED with Simulink_Arduino
Figure 8: Hardware of Basic Blink LED with Simulink_Arduino

In Figure 9, the number(s)

  • 1 and 7 are pointing at ground wire in bread board and in Arduino, respectively.
  • 2 represents the 220 ohm resistor between cathode leg of LED and ground wire.
  • 3 is LED
  • 4 and 6 are showing the signal wire to the Anode/positive leg of LED and from Arduino, respectively.
  • 5 shows the connection wire from PC for communication and power for Arduino board.
Glow LED with Arduino_Simulink Simple Model (Getting_Started)
Glowing LED by Similink_Arduino Hardware Deploy as Stand-alone System at rate of 50% ON time with 1 sec

Similarly, you can glow the multiple LEDs at different rates and time periods by changing the pulse generator settings i.e. by changing the ON/OFF time of the signal pin for the LEDs. (Figure 9)

Or Click here for a short video showing complete procedure; explained above

Multiple LEDs at different PWM signals
Figure 9: Different Arduino Digital output pins at different pulse rates

After deploying to the Hardware the LEDs will glow in a sequence. (shown in figure below)

        This examples shows that we can program the digital output pins of Arduino with our desired signal by just using Simulink blocks tool.

LED glowing in sequence with different PWM signals in Simulink_Arduino Model
LEDs glowing in sequence as standalone system with different PWM signals in Simulink_Arduino Model

Arduino_Simulink in External Mode:

Examples discussed above show the stand-alone electrical system of Arduino deploy in microcontroller of Arduino using the Simulink blocks/support package. Now, we will run Arduino in external mode in real time and will observe and record the change in external hardware attached with Arduino in MATLAB.

Here, comes the function of Analog/Digital input blocks. Input blocks checks the voltage at that Arduino pin and recorded in MATLAB/Simulink.

  • Place Analog input block to a new model.
  • Set the input pin you want to get a signal at by going into the properties of that block. Also the sampling time for input signal.(Figure 10)
  • Take a scope form sink library under the Simulink library and place in the same model for observation in input signal as graph.
  • Also place a gain block for magnification of the graph in MATLAB. (Figure 11)
Setting Analog Pin Arduino for External mode
Figure 10: Arduino Analog pin configuration

Figure 11 below, shows simple model for Analog input of Arduino with MATLAB i.e. Arduino as External mode of operation with real time observation and record to the MATLAB. Also showing steps for External mode.

  • First Click on the drop down menu in top middle of option bar of MATLAB and selectExternal in it. (Figure 11 (1)).
  • As we want for infinite time so, write inf for running it in external mode infinite time. (Figure 11 (2)).
Arduino MATLAB Run in external mode
Figure 11: Arduino MATLAB in External mode

For this the Hardware required is just potentiometer, connecting wires and Arduino connected with MATLAB Simulink. (Figure 12)

Hardware configuration for Arduin simulink external mode
Figure 12: Hardware Configuration for Arduino in External Mode (A Simple model)

In Figure 12, the number;

  • 1 (red wire) shows the +5V probe from the Arduino and the leg of potentiometer connected.
  • 2 (black wire) is the ground (GND/0V) wire from Arduino connected with Potentiometer.
  • 3 is three legged 1k ohm potentiometer.
  • 4 (blue wire) is signal wire to the Analog pin A4 of Arduino.

The video below explains all the same. i.e. Arduino in external mode with MATLAB and same system in stand-alone mode


Asad Ullah

I am MSc Electrical Scholar under a fellowship program. Working with Modeling and Simulation software related to my field are my activities in leisure.

What do you think?