Arduino Simulink S-function tutorial

A step by step guide to how to program Arduino Due using S-function in Simulink

In this Arduino Simulink S-function tutorial we want to share step by step guide to how to program Arduino Due using the S function builder in Simulink. The S function builder block in Simulink allows you to write program for Arduino in Simulink in native Arduino C++ language. That is once the simulink model is run and compiled, the block produces codes and uploads into the Arduino Due.

Why use S function?

As you perhaps know Simulink already has support for Arduino hardware(but you have to download as addons, see the Arduino Simulink Tutorial 1 – LED blinking) which allows you to program Arduino using those blocks when run and compiled. So why do you need another S function you might ask. The reason is that those Simulink Support for Arduino Hardware are simple blocks which allows you to create basic bridge between Arduino and Simulink. The S function on the other hand can do the same thing as the Simulink Support for Arduino and allows you to create model with libraries that are not supported by Simulink Support for Arduino. For example, if you want to use the Arduino LCD or motor libraries in simulink, you can do that with S function but not with the ordinary simulink support for arduino hardware libraries.

How to program arduino using S function in Simulink?

Next we will give you step by step guide to how to program Arduino using S-function builder in Simulink. For this basic introduction we will create a simulink model that receives ON or OFF signal from simulink constant switch block and sends to that to S-function Output block. The output block is programmed to send the switch state to the Arduino pin 11. A LED is connected physically to the Arduino pin 11. Thus when there is ON signal from simulink, the physical LED will turn on.

Note that this is essentially Arduino digitalWrite implementation. We take binary input and write that to the digital pin 11 using S function Input block. See Arduino Due tutorial Blinking LED tutorial where we have used the Arduino Due for LED blinking using Arduino programming language.This is similar project but here we are using Simulink S-Function block. On the physical implementation side, you should have a LED connected to the pin 11. An example is shown below how to connect a LED to pin 11 of Arduino Due.

How to create S function in matlab simulink?

Now we show how to create S-function block in Simulink for Arduino. Open a new Simulink model and save it with some name of your choice. Then look for S-function builder in the User Defined Functions library. Import that into your simulink model.

S function builder

Now we have to configure the S-Function builder to use Arduino native programming language. To do this open it by double clicking on it. You should see the following.

Herein we have to type our Arduino Program, configure input and output ports to communicate with Simulink blocks(like constants, scope etc). At first the many configuration tab may discourage to learn this thing but it is actually very easy. You should observe what Arduino info goes where in this S-Function block. After you get the bigger picture it will be easier for you to understand. See the following diagram to know what goes where and what they mean.

Just to give you an example, below is picture that maps an Arduino digitalWrite program to this S-Function block.

Arduino program mapping to S-Function block

Note that this is just an example where in the arduino program pin 13 is used whereas in our tutorial we will be using the pin 11.

So hopefully you now understand that the S-Function is just more or less systematic mapping of arduino program.

First enter some name for the S-Function block at the top field S-function name. Here it is called InputSfcn. Enter 1 into the Number of discrete states field and use Inherited in sample mode(you can use other sample time like 0.05)

Initialization tab

Click on the Data properties tab. Here you define the input and output port to be used to communicate the S-Function block with Simulink blocks. For this tutorial we require one input port to acquire values from constant blocks via manual switch(you will see this later down below). There is no need for output port.

In the Input ports tab you should initially see x0 as the name of your port. Delete it and rename it to something like in in our example. See figure below.

Next click on the Output port and delete the initially y0 port.

Next click on the Parameters tab. There type in LED as the parameter name. Then at the top change the value to 11. This is like writing,

int LED =11

in Arduino program. Also change the data type to uint32 because we are using Arduino Due.

In the Data type attributes change the input ports in to boolean because this port is accepting boolean 0 or 1 from constant blocks via switch from simulink.

Moving on the next tab libraries type in the followings,

Next click on the Discrete Update tab, no the Output tab, because we want serially write Codes as in usual Arduino program. In Discrete update tab you should enter things that you would write inside the void setup() function in Arduino program.

So there type in the following,

As a general rule every arduino native code should be placed within the # ifndef MATLAB_MEX_FILE and #endif. The xD[0] is a state variable for this S-Function block. The if statement checks the state variable xD[0], whether it is 1 or not and if it not 1 then the lines within the #ifndef and #endif are wrapped for producing compiled code for Arduino. At the end there is xD[0]=1 which makes the xD[0] state variable 1 and so next time the same code within the ifndef and endif are not compiled/executed. This in ordinary language means the settings with the void setup() function is executed only once as required.

Now we configure the main program code. To do this the Outputs tab is used(although the names Outputs and Discrete Updates are somewhat misleading w.r.t program codes). There write in the following program code.

So here we are writing the digitalWrite() function that we would normally used in the void main() function in Arduino program. The value to be written is in[0](state variable convention) and it should be written to LED[0] pin.

That’s shown below.

Now the S-Function configuration is complete. We next must build this S-function configuration. Click on the Build at the top right corner and observe the output message.

If you look at the block in simulink you should see the updated block with defined in port and name as shown below.

Connect Manual Switch to the input in port.

Then connect two constant blocks to the two inputs of the manual switch. Don’t forget to use 0 and 1 in the two constant blocks and also don’t forget to configure the data type as boolean.

Simulation Configuration and Running Simulation

The next task is to set the simulink to work with Arduino Due, set the run time and set the simulation mode to external. This process has been explained in details in the tutorial Arduino Simulink Tutorial 1 – LED blinking.

First to set the board to be used, go to Tools > Run on Target Hardware > Prepare to Run…

There in the window that opens select your Hardware as Arduino Due.

Next set the simulation run time to inf and set the simulation mode to External.

Click on the Run button and Simulink will start building, compiling and uploading the program code into your Arduino. Then while the simulation is running, double click on the manual switch to change from 0 to 1. This will then turn on the LED. The following figure shows this.

led turned on using S function

 

Leave a Reply

Your email address will not be published. Required fields are marked *