Getting Started to Develop Program with Matlab Simulink and Run on Arduino

Make it to the Right and Larger Audience

Blog

Getting Started to Develop Program with Matlab Simulink and Run on Arduino

This is a study note after reading Mathworks’  Getting Started with Arduino® Hardware, and Agus Kurniawan’s ebook “Getting Started with Matlab Simulink and Arduino”.

Arduino is an open-source electronics prototyping platform and it uses Atmel microcontroller series. A typical development flow is to download and install Arduino development package from  http://arduino.cc/en/Main/Software to your platform, either linux or windows. Then you can write C code using the installed package tool and then download and run the compiled code to Arduino board through USB connection.

Another development flow is to use Matlab Simuink as discussed in above two references. First you need to install Arduino support package to Matlab tool which is of 2013a or later version. You can click on “Add-Ons” in Matlab menu, select “Get hardware Support Packages”, use internet option to search packages from Mathwork online database, and select Arduino as below to install. As can be seen, other open-source platforms such as Raspberry Pi, BeagleBoard, and PandaBoard are also supported.

arduino1

After install, you should see Arduino blocks as below. There are analog input, digital input/output, serial receive/transmit, etc. What we can do is to use these blocks to read input pins of microcontroller on Arduino board, run some calculation, and then drive microcontroller output pins. The beauty of this flow is you can use simulink to easily develop complicated algorithm or integrate your existing simulink blocks without writing any C code. The calculation can be debugged using simulink and eventually be downloaded to microcontroller on Arduino board and runs over there.

arduino2

The ebook gives several examples. One is hello-world as below. Simulink shows a pulse generator drives a digital output pin of the Arduino microcontroller which in turn turns on and off an LED on board. There is also an scope to view the pulse generator output. When you download and run this program onto Arduino, what happens is pulse generator is implemented using microcontroller clock resources and scope is implemented so that the microcontroller is instructed by this program to send back some results.
arduino3

A more complicated example is as below which uses both digital input pin and digital output pin.

arduino4
The ebook has other examples that use analog input pins, serial in/out (shown below), and Sevo Motor. You can check the ebook for details. Developing above examples or other applications just follow the typical simulink development methodology. The only thing worth mentioning is how to download and run the model on Arduino board. The first time you do it, you need to go to “Tools”, “Run on Target Hardware”, and then click on “Prepare to Run”.

arduino5

In the pop up dialog, select your Arduino board in Target hardware, and then configure the setup. Use automatically for “set host COM port” and check “Enable External Mode” to obtain feedback from board which, I think, is why simulink scope mentioned above can display data when program is running on Arduino board. After above steps, you just use “Tools”, “Run on Target Hardware”, and “Run” to download and launch the program on Arduino board.
arduino6

 

As mentioned above, I think the beauty of this simulink+Arduino flow is to hide Arduino board details, bypass C coding, easily develop complicated data processing algorithm using the popular simulink tool, and be able to run it on Arduino board. However, the final program seems standalone and is not able to be embedded into an exisitng C program. But in that case, you can just use simulink without Arduino supporting package to develop some algorithm and export as C code to be included in your existing C program.

 

 
Master Student
Author brief is empty
2 Comments
  1. Profile Photo
    JLee 5 years ago
    0
    -0

    Anyone know if Arduino support package for Matlab is a free addon or I need to purchase it from Mathworks?

    0
  2. Imran 5 years ago
    0
    -0

    Interesting. Quick and simple. As for integrating existing C code, I think it may be possible to add block to simulink and put C code in that block?

    4

Contact Us

Thanks for helping us better serve the community. You can make a suggestion, report a bug, a misconduct, or any other issue. We'll get back to you using your private message ASAP.

Sending

©2021  ValPont.com

Forgot your details?