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)
3. Join both the blocks by clicking the first block and pressing cntrl key and then clicking other block. (Figure 2)
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)
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)
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)
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)
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)
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.
For the above system you just need a LED, a resistor of 220 ohm and connection wires.
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)
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.
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)
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.
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)
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)).
For this the Hardware required is just potentiometer, connecting wires and Arduino connected with MATLAB Simulink. (Figure 12)
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