How to use Arduino in Matlab and Simulink

10,762 views

Simulink is an extension to Matlab that provides an interactive, graphical environment for simulating and analyzing dynamic systems. It also allows you to incorporate Matlab algorithms into models and export the simulation results into Matlab for further analysis. Today, we will look at a step by step procedure for interfacing & using Arduino in Matlab and Simulink.

In Simulink, systems are represented as block diagrams. Several elements of block diagrams are available, such as transfer functions, summing junctions, etc. And also some virtual input and output devices such as function generators and oscilloscopes. Similarly, we can also use Arduino in Matlab and Simulink by installing it and in this tutorial, we will show you how to install and use them.

Working Explanation

By using the Simulink Support Package for Arduino Hardware, you can use Simulink to develop and simulate algorithms that run standalone on your Arduino. In this video, I will write a code of Arduino in Simulink by using different blocks to generate a square wave at the output.

Select a pulse generator block from the library. The pulse generator block will generate a square wave and set the duty cycle to 50%. The output of the pulse generator goes to the Arduino output block and to the scope. The scope provides a function to view the output waveform. To upload the code in Arduino, connect the Arduino to the computer and click the “deploy to hardware” button in the Simulink software. Once the code is successfully uploaded, you can test it by connecting an oscilloscope to the output of Arduino.

Procedure

Follow these steps to for using Arduino in Matlab and Simulink.

1) Open the MATLAB software & then go to the Add-Ons option in the toolbar.

2) Click “Get Hardware Support Packages” and then select “Install from Internet”

3) Select the “Arduino UNO/Nano/Mega” package from the list

4) Click the “next” button & then log in to your Matlab account.

5) Check ‘I Accept’ on the license agreement screen & then press next.

6) Now, Click ‘Next’ again.

7) Click ‘Install’.

8) Once the package is successfully installed, you can use Arduino in MATLAB.

Now we will go over a simple example of writing an example code for Arduino in Simulink, using different blocks to generate a square wave at the output.

Sample Code

9) Go to NEW in the toolbar & then select ‘Simulink model’ option from the dropdown menu

10) Select ‘Simulink Library’ & then click on Simulink support package for arduino from the side menu.

11) Select ‘Common’ & then choose ‘Digital Output’ block.

12) Go to common simulink library and then select Sink option from side menu, then select scope block to display output signal.

13) Now for a signal input to the digital output, go back to the simulink common library and then select source option, click on the Pulse generator block and then click on your simulink work space.

14) Draw the circuit as shown below

15) Set the correct pin number for the Arduino Model that you have connected to Simulink, pin 9 for Arduino Uno & pin 13 for Arduino Mega.

16) Double-click on the pulse generator block & then set the amplitude, time period & pulse width (duty cycle) of the input signal

17) Click RUN & then double-click on the scope block.

18) Now after ensuring the correct output on the simulation you can upload the code to the Arduino by selecting tools –> run on target hardware –> prepare to run.

19) Select your device.

20) Set hot COM port to manual & then type in the appropriate COM port no. to which you have connected your Arduino (you can find this at My PC (right click) –> Properties –>Device Manager–>Ports (COM & LPT).

21) Click ‘OK’ & then after saving the file, Select ‘Deploy to hardware’.

22) If no error occurs, then you successfully uploaded you simulink code to the Arduino, here’s a sample result on an oscilloscope.

Application

  • You can quickly visualize your data using the vast array of plot types in MATLAB.
  • You can make processes such as signal processing, control design, state logic, and other advanced math a part of your hardware projects.

See Also: Sine & Cosine Wave Plotting Using MATLAB|Audio Signal Processing Using MATLAB