Reading analog signal with Arduino Due and Simulink Part 1 tutorial

In this first part of the tutorial on reading analog signal with Arduino and Simulink, how to add and configure S-Function Builder. The configuration of S-Function builder is done so that we can aquire analog signal from Arduino and import into Simulink for display or any orther manipulation.

In this tutorial series on reading analog signal with Arduino Due and Simulink, we want to show how you can use Arduino Due together with Simulink to acquire analog voltage signal and plot in graph(or you can after acquiring the signal do other analysis). We will be using the S-Function Builder to write the program. We will be using the DSP time scope to plots the signals and view it in real time. The DSP timescope is much like oscilloscope. To use the 12bit ADC of Arduino Due in Simulink we can use a driver block in Simulink. There are couple of ways to create driver in Simulink such as Matlab Function block, System Object etc. But here we will use the S-Function Builder. We will not explain in details how this S-Function Builder works because we have already explained that in details in the tutorial Arduino Simulink S-function tutorial.

This is the first part of the tutorial series reading analog signal with Arduino Due and Simulink. In this first part of the tutorial we will show how to add S-Function builder into Simulink and then how to configure it. We will use the 10-bit default ADC feature of the Arduino. Also we will add DSP time scope, constant block and a product block and connect all them together. The constant block and product blocks are added so that we can re-scale the ADC converted digital 10-bit data to the range 0 to 3.3V.

In the next 2nd tutorial we show how to configure simulink and run the simulation. During the simulation we will view the analog signal read from the potentiometer.

Finally in the 3rd tutorial we will show how to read analog signal using 12-bit resolution. We will use the same hardware and simulink blocks but will make minor modification to the S-Function block and the constant block. The 12-bit resolution will give us finer details of the analog signal in real time as you will see there.

Note that we already showed you how to read analog signal using Arduino and plot in real time. But the difference between the previous tutorial and this one is that we are now using S-Function Builder and we are using Arduino Due. In previous tutorial Real Time Oscilloscope using Simulink and Arduino we used Arduino UNO for reading analog signal which has only 10bit ADC resolution. Here we will show both 10bit ADC and 12bit ADC of Arduino Due.

Let’s get started.

Step 1: Create New Simulink model and add S-Function block

First of all, create a new simulink model and save it with some suitable name. Here model name is SfcnanalogRead. Then in the new model add S-Funcion Builder block which you can find in the User Defined Functions library.

 

Step 2: Configure S-Function Builder Block

The next step is to configure the S-Function Block. Double click on the S-Function Builder to open its properties.

At the top you should see a field S-function name. Type in some name for this S-Function block. Here it is called sfcnanalogread. Then in the Initialization tab type 1 in the Number of discrete states field. Choose Sample mode to Inherited.

Next click on the Data Properties tab. Here you have to set the input and output port of the S-Function block. In the Input ports delete the existing Port using the delete button. We don’t need input port here because we are not sending any data from simullink to arduino.

In the Output ports, create a new output port called ana. We need output port here because we are reading and importing signal from Arduino to Simulink.

Leave the parameter tab empty because we don’t need any parameter in this example.

Then in the Data type attributes, specify that our ana output port should have data type of uint32.

Next we have to specify the libraries. In the Libraries tab, type in the Includes field the following code.

See below where to write the code.

Next open the Discrete Update tab. In this tab we write the code that you would normally write in the setup() function in Arduino Sketch within an if statement. However since for analog read we don’t need any setup we leave it empty. Meaning only the following code is required.

Type this code in the Discrete update tab as shown below.

Next is the Outputs tab. Here we write the Arduino Sketch code that goes in the main() function, written within an if condition statement. Type the following code into the box.

See the figure below.

Finally, open the Build Info tab. In that tab, you can see the build information for this S-Function after you have clicked on the Build button. Click on the Build button to build the S-Function. After build button is clicked you should see successful creation of the S-Function or any warning or errors.

Now close the S-Function Builder and you S-Function Builder should look like the following in the simulink model.

Step 3: Add  DSP Time Scope, Constant and Product Blocks

The final step for this tutorial is to add the DSP Time Scope, Constant block and a Product block. Then add connect them in the following manner.

In the constant block type in the constant as 3.3/1023. The reason for this is that the values that are read by simulink through the S-Function builder block are quantized to 0 to 1023 levels. 0 corresponds the 0V and 1023 corresponds to 3.3V. Between these ranges the voltages read by the Arduino Due are converted to intermediate quantized values. Now if we don’t add the constant block and just plot it on the Time Scope we will see amplitude level from 0 to 1023. This is not what we want here. We want to see the actual voltage range plot from 0 to 3.3V so we multiply the incoming voltage converted digital values to time scope.

Also you should open the Time Scope and change the Title of the graph and the amplitude y-axis level. This is shown below.

Now the amplitude level and title will change as illustrated below.

So this completes the first part of the tutorial series Reading analog signal with Arduino Due and Simulink. Here you learned how to add S-Function Builder to a new simulink model and configure and compile the S-Function block for reading analog signal. Also you learned how to scale the acquired analog converted digital signal to the correct voltage level we that we can see it on the Time scope.

In the next tutorial you will learn how to setup the simulink so that you can compile and upload the S-Function generated code into Arduino Due and configure the run time so that you can view the signal in the time scope in real time.

See next tutorial Reading analog signal with Arduino Due and Simulink Part 1 tutorial.

6 Comments

Leave a Reply
  1. Hai, I follow all the step in this tutorial, but in my code generation report shown an error : cannot open source file “Arduino.h” for file: C:\MATLAB\SupportPackages\R2016a\toolbox\target\supportpackages\arduinotarget\scheduler\include\arduinoAVRScheduler.h
    how can I add the source file “Arduino.h” in my model?

  2. I try out this example but I cannot achieve any output. From the scope, the output is zero. May I know why this happen? How much is the maximum sampling speed can achieved by using S function builder for arduino uno?

  3. Please help >how to make and configure S-function builder to output analog signal from arduino Due using DAC0 and DAC1

    • hi,
      I think it should work, haven’t tried yet but i don’t see why it should not work. could you post your code and Maybe we can help.

Leave a Reply

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