How to program Arduino Due using Simulink S Function | applied electronics engineering


How to program Arduino Due using Simulink S Function

By Applied Electronics - Monday, March 13, 2017 No Comments
In this tutorial we want to show how you can program Arduino Due using Simulink S Function. This tutorial taken from Arduino Simulink S-function tutorial.

There is many advantages for simulating Arduino Due with Simulink. For this reason there is Simulink support for Arduino Hardware. But if you want to simulate with other Arduino libraries such as LCD or motors then using Simulink Support for Arduino Hardware might not work.

On the other hand by incorporating Arduino program with S function you can simulate all the Arduino libraries. In this tutorial we show how to blink a LED connected to Arduino pin 11. We will send digital values 0 and 1 to from simulink constant blocks to turn off and on the LED. This is much like native Arduino programming where the function digitalWrite() is used. See Arduino digitalWrite and digitalRead tutorial for an example. But here we will be using Matlab Simulink programming.

The physical hardware setup is shown below.

The first step is to add the S-Function Builder from the User Defined Functions library into Simulink as shown.

Next double click it to open the configuration setting window. Then there are couples of settings we must set one by one. The setting is to map the Arduino program code into the S-Function such as adding libraries, adding variables, adding input and output ports that interacts with other simulink blocks, adding the codes to setup the pin modes, adding the main codes etc.

The first thing you need to do is to give the S function some names which you can at the top. Then in the Initialization tab, you write the number of discrete states as 1 and Inherited for the sample time. That's shown below.

Next in the Data Properities tab you define the input port, the output port for the S function block and also add any parameters that your program is using. You can add or delete any number of input or output ports as required.

The following figure shows the input port configuration.

We don't need any output port so the output port configuration is shown below

In the parameter tab we need to define the variable name LED which is assigned to pin 11. Just add parameter LED and use uint32 as data type. This 11 pin number is set at the top of the parameter window as shown below.

Next is the data type attribute. Here we specify that the input port in has data type attribute boolean.

Moving on to the next tab we have the libraries tab. Here we write the libraries to be added.

Next we move on to the Discrete Updates tab. There we type in the codes that we would write to initialize the port mode in the setup() function. See below.

Then in the Outputs tab, we write the codes that does the actual work in the main function. This is as below.

The setting is complete and now we have to build the S-Function block. Go to the Build tab and click on the Build button at the top right corner. This will try to build the S-Function block and if everything is ok you should get something like the one shown below.

Now you can close the S-Function builder window. The S-Function block should look like the following after we have set the name and ports.

What remains now before simulation is to add the constant blocks to turn on or off the LED. We will add a manual switch and connect to two constant blocks of data type boolean and values 0 and 1. This is shown below.

Finally we are now ready for real time simulation. Before we can simulate the model we have to configure the simulink to use the Arduino Due board and set the device vendor to ATMEL and also set the com port. This was explained in great details in one of the previous tutorial Arduino Simulink Digital Input example.

Now when you click on the run button the model will be build, compiled and program code uploaded to Arduino microcontroller. If everything succeeds you should see the LED blink after you change the switch from 0 to 1. The turned on LED is shown below.

See also the basic Arduino Due tutorial, how to test Arduino Due?

No Comment to " How to program Arduino Due using Simulink S Function "