Sensor Application with i.MXRT1060 EVK using IMXRT Toolbox

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Sensor Application with i.MXRT1060 EVK using IMXRT Toolbox

CosminB05
NXP Employee
NXP Employee
2 0 3,074

Introduction

This article is a tutorial on how to use the IMXRT1060 Evaluation Kit Board to create a temperature measuring application. The application will be created inside MATLAB Simulink using the IMXRT Toolbox and will follow all the verification and validation stages using the principle of model-based design to make sure the application is working properly and can generate correct code for the IMXRT1060's processor. The IMXRT1060 Evaluation Kit Board will be configured using the NXP MCUXpresso software. In the end the application will be built using two methods: External Mode in Simulink or outside of MATLAB using NXP FreeMASTER software.

Temperature is one of the most important measurement variable for monitoring and controlling in various industries. This tutorial is also suitable for home applications when you want to measure the ambient temperature of a room or a small garden.

 

Hardware required

For the development of this application the following hardware is required:

  • i.MX RT1060 EVK

The MIMXRT1060 EVK board is a platform designed to showcase the commonly used feature of the i.MXRT 1060/1064 Processor in a small, not expensive package. It is an entry level development board that familiarizes the developer to the processor before investing on further resources for specific designs.

Some of the features of the MIMXRT 1060/1064 board are Mass Storage (TF Card Slot, 64 Mbit Quad SPI Flash, 512 Mbit Hyper flash), LCD Connector, Ethernet, USB, Audio Connector, Debug Connector (JTAG or OpenSDA), User Interface Buttons, Arduino Interface and many more.

 

CosminB05_0-1629789093030.png

Frontside overview of the MIMXR1060 EVK Board

 

CosminB05_1-1629789173829.png

Backside overview of the MIMXRT 1060 EVK Board

 

  • Thermistor module

The KY-013 Analog Temperature Sensor will be used for this application, but there is freedom to choose any other thermistors modules.

This module consists of a NTC thermistor and a 10K ohm resistor.

Thermistors are temperature-sensing elements made of semiconductor material that has been sintered in order to display large changes in resistance in proportion to small changes in temperature.

NTC Thermistors are non-linear resistors, which alter their resistance characteristics with  temperature. The resistance of NTC will decrease as the temperature increases. 

 

CosminB05_3-1629789606787.png

Resistance value based on temperature

 

This module has 3 pins: Ground (-), Output (S) and Vcc

CosminB05_4-1629789701653.png

KY-013 module

 

Software required

In order to run the application on the MXRT1060 board the following software is necessary:

  • Mathworks’ MATLAB, Simulink, and the following add-ons: Embedded Coder, MATLAB Coder, Simulink Coder and Embedded Coder for ARM Cortex-M Processors, MATLAB Support for MinGW-w64 C/C++ Compiler
  • NXP’s MATLAB add-on: NXP Support Package IMXRT1xxx which is a embedded target support and plug-in to allow code generation and deployment toolbox for MATLAB
  • NXP FreeMASTER – an user-friendly real-time debug monitor and data visualization tool that enables runtime configuration and tuning of embedded software applications.

 

Model-in-the-Loop Simulation

The first step of creating the application is to create the model of the system (thermistor module).

Model-in-the-loop simulation (MIL) is a technique used to mimic the behaviour of a system in a way that this model can be used for testing and simulate different scenarious.

The model of the thermistor will be created from its circuit scheme:

 

CosminB05_0-1629790102512.png

KY-013 circuit

 

The module is nothing more than a voltage divider, but with a resistance that varies with temperature changes. In order to measure the temperature the value of the thermistor must be calculated.

It is known that the output voltage of the circuit is based on the following formula:

CosminB05_1-1629790185207.png

From this the thermistor resistance value can easily get retrieved:

CosminB05_6-1629790388647.png

 

The following are known values: R0 = 10k Ohm, Vin = 5V and Vo is received from the IMXRT1060 board using an Analog-Digital Convertor (ADC) peripheral.

The aforementioned formulas will be modeled in Simulink using the available blocks in the Math Operations category in the Library Browser.

For this model a LED warning system model will be added. This warning will blink the user led on the EVK board when the temperature will exceed a specific threshold value set by the user in the model.

 

CosminB05_1-1629808607177.png

Model of the circuit

 

When running the above model the expected output of the thermistor’s resistance will be 4286 if a value of 1.5 is assigned to Vo. The next step is to use the obtained resistance value and compute the temperature.

Next, we will find the temperature value using the thermistor's resistance.

The equation that models the temperature based on the thermistor values is called Steinhart-Hart equation and is the following:

CosminB05_8-1629793000924.png

Where:

  • T - temperature (K)
  • R - resistance at temperature T (Ohm)
  • c0,c1,c2 - Steinhart-Hart coefficients which vary from a thermistor to another

The following Simulink model will resolve the above equation:

 

CosminB05_2-1629808949615.png

Steinhart-Hart equation model

 

Simulate the model with the following values:

  • c0 = 0.001129148
  • c1 = 0.000234125
  • c2 = 0.0000000876741

The expected temperature value using a resistance of 4286 ohm will be 318.6 K.

For a familiar output the temperature can be converted from K to Celsius or Fahrenheit:

CosminB05_13-1629793309876.png

CosminB05_14-1629793321379.png

CosminB05_15-1629793341858.png

Kelvin to Celsius conversion

 

A LED warning system is also modelled. It works using the following logic: if the difference between the output temperature and the threshold value is greater than zero the LED will start blinking, else the LED will stay turned off.

The warning system uses a Switch block, a Discrete Pulse Generator, a Data Type Conversion and a constant block. The Discrete Pulse Generator has the following parameters:

  • Amplitude – 1
  • Period – 2
  • Pulse width – 1
  • Phase delay – 0
  • Sample time – 1

 

We use a Data Type Conversion to boolean because this is the required input for the GPIO Output block.

The inputs of the Switch block are the following:

  • 1 - Blinking command
  • 2 - The condition if the difference between measured temperature and threshold value are above zero
  • 3 - LED off state

 

The following figure displays the application model. Notice that subsystems were created for the thermistor, the equation and the temperature conversion. The output voltage was replaced with a Sine Wave and the temperature, voltage, thermistor resistance and LED state are connected to a scope.

Also note that the input voltage of 5V was replaced with a numeric value of 6148. This is happening because the output voltage received from the thermistor will be in a digital form by using a 12-bit 3.3V ADC Peripheral.

Because the ADC converts value from 0 to 3.3V and a resolution of 12 bits is used, for 3.3V a value of 4095 is returned, and for 5V a value of 6148 is returned.

 

CosminB05_12-1629802118230.png

ADC formula

 

model_mil.PNG

 Application model overview

 

scope_mil.png

Scope outputs 

 

In the scope it can be observed that when the output voltage decreases, the temperature increases as expected. When the temperature rises above the threshold value, the user led will start to blink.

In the next stage of verification C code of the model will be generated and check if the generated code and the Simulink model output the same data.

 

Software-in-the-Loop Verification

A software-in-the-loop (SIL) verification compiles generated source code and executes the code as a separate process on the host computer. By comparing model and SIL simulation results,  the numerical equivalence of your model and the generated code can be tested.

In a new folder create two models  "sil_target.mdl" and "sil_harness.mdl".

Open the "sil_target.mdl". This will be the application model. To save time, copy the content from the previous model-in-the-loop model.

Next replace the Sine Wave of the output voltage with an Input Port and the scope with four Output Ports. (Input Port and Output Port are found in Library Browser, Simulink > Ports & Subsystems)

Make sure you have the following settings:

  • System target file set to ert.tlc in the Code Generation tab
  • Toolchain set to MinGW64 | gmake (64-bit Windows) in the Code Generation tab
  • Check Enable portable word sizes in Verification tab

 

Codegen.png

Verification.png

  Model Settings windows

 

 

model.PNG

Target model overview

 

Save the target model and open the harness model. Make sure you have the same settings as pointed above.

In the harness model insert a Model Block (Simulink > Ports & Subsystems). Right-click on it and open Block Parameters...

In this window, browse for the target model and set Software-in-Loop as Simulation Mode.

 

tempsnip.png

Model block properties

 

Add a Sine Wave block as an input to the model block and a scope for the outputs. Next the output signals will be logged for the Simulink Data Inspector, necessary for the verification.

To log the signals select them, then in the SIMULATION or SIGNAL tab press Log Signals.

 

sil_log.png

Logged signals 

 

To start the validation, click on APPS tab, open SIL/PIL Manager. Press on MODE button and choose Automated Verification. Set Systems under test option on "Model blocks in SIL/PIL Mode" and Top Model Mode on "Normal". Choose the stop time and press on Run Verification button.

 

SILPIL.png

 

SILPILtab.png

 SIL/PIL Manager menu

 

After the simulation and code generation is completed, the Data Inspector window will pop-up showing the results of the verification.

 

data_inspector_sil.PNG

 Data Inspector results

 

As it can be seen in the above picture, the model has passed the verification step with all the output variables having 0% tolerance and difference.

 

sil_scope.png

Scope display of the output variables

 

The Software-in-the-Loop verification step is not enough to deploy the code to the evaluation kit board because it is not known if the generated code is implementable. Thus, the next stage is the Processor-in-the-Loop validation.

 

Processor-in-the-Loop Validation

A processor-in-the-loop (PIL) simulation cross-compiles generated source code, and then downloads and runs object code on your target hardware. This step will help identify if the processor is capable of running the developed code. If there are glitches, the designer can return to the code, SIL or MIL, and correct them.

Create a new folder and to save time copy the harness and target models from the SIL stage and rename them as "pil_target.mdl" and "pil_harness.mdl".

Open the target model and make the following settings:

  • Same settings as SIL
  • In Hardware Implementation section, select as Hardware board "NXP MIMXRT1062xxxxA"
  • In the same section, on Hardware board settings, extend Target hardware resources. Click on Download group and select Type as "OpenSDA", and the drive location of the board (The board must be connected to the host computer).
  • On the PIL group, select Serial port as the COM number of your board connection (To find the COM number, open Windows Device Manager program.

 

Settings1.png

Settings2.png

 Model settings

 

Apply the changes and close the target model.

target.PNG

 Target model

 

Open the harness model. If you copied the SIL harness model then right click on the Model block and change the simulation mode to Processor-in-the-loop (PIL), else, follow the steps from the Software-in-the-loop chapter. Also make sure the model settings are identical to the target's.

 

harness.PNG

 Harness model

 

Similarly to the  the SIL verification, log the output signals, open SIL/PIL Manager and run the verification.

After the build is complete the following window will appear, reset the board and press OK.

CosminB05_27-1629802408803.png

Evaluation board restart pop-up window

 

Capture.PNG

Data Inspector results

 

It is noticeable that the verification fails at the 22-23 time period. This happens because at that period of time the thermistor resistance is zero and the temperature is computed using the natural logarithm, which is not defined for zero.

Therefore, based on the results it can be assumed that this stage of verification has passed.

In the next chapter we will configure the IMXR1060 EVK Board using the NXP MCUXpresso software, connect the thermistor module to the board and output real-time values using external mode or NXP's FreeMASTER software.

 

Hardware configuration

The application successfully passed all the verification and validation steps. Now it is time to configure and connect the hardware to see some actual results.

The NXP Model Based Design Toolbox for IMXRT will generate C code from the model using custom blocks to read the values from the temperature sensor and send signals back to the board.

The application will get built by using two methods: External Mode and NXP FreeMASTER. The first way of building the application will be using External Mode.

External Mode

Simulink External Mode establishes a communication channel between a Simulink model and the targeted hardware. It grants real-time parameters visualization and real-time modifying of the model parameters.

First of all, create a new model named "temperature_sensor_extmode.mdl". Copy the content from one of the existing models created at the previous validation steps (i.e "temperature_sensor_model.mdl" in the MIL simulation).

Next, delete the Sine Wave block. The input of the model is the output voltage of the thermistor. In order to read a voltage using the MIMXRT1060 board an ADC (Analog-Digital Converter) is required.

Make sure the model is configured to the requirements of the board (check PIL Validation chapter for more details)

  • Hardware board : NXP MIMXRT1062xxxxA
  • In External Mode group under Target hardware resources in Hardware Implementation section select the specific Serial Port and check Verbose option.

 

tempsnip.png

 Model settings window

 

Apply the changes and close the model settings window.

After this step, you can see that in the user workspace a file with .mex extension and a folder are created by the MCUXpresso Configuration Tool. The configuration folder is necessary in order to generate and build the application on the board. For more details on Simulink model creation for IMXRT targets see this quick start guide.

 

CosminB05_1-1632752242762.png

External mode simulation folder

 

Now it is time to use add the NXP Simulink blocks to the model. In the Simulink Library Browser head to NXP Model-Based Design Toolbox for i.MX RT MCU's. To find the ADC Blocks go to i.MX RT1xxx Core, System, Peripherals and Utilities > Motor Control Blocks > ADC Blocks. Drag an ADC Read block into the model. An ADC Start block is also necessary to be added. This block is required in order to enable the peripheral when the application is built.

In the modelling stage, the input voltage was set to a digital value of 6148. It is known that the ADC Peripheral can convert voltage up to 3.3V (digital value of 4095). The question that pops is what if the output voltage is higher than 3.3V? There is a solution by adding an additional voltage divider in the circuit, but there is no need for anything because for an output voltage of 4095 the temperature reaches 10 °C  / 50 °F, which will never be reached for our application purposes.

 

tempsnip1.png

 Simulink Library Browser

 

To control the user LED board, a GPIO (General Purpose Input Output) block is required. The GPIO blocks are found in i.MX RT1xxx Core, System, Peripherals and Utilities > GPIO Blocks. Add a GPIO Output block to the model.

 

tempsnp2.png

 Simulink model with the NXP IMXRT blocks

 

Next, these blocks must also be configured using the NXP MCUXpresso. Right-click on any NXP block in the model and press the Configure button.

 

temp32snip.png

NXP Simulink block properties

 

MCUXpresso will now be loaded. 

After the MCUXpresso is loaded, open the Peripheral Tool to configure the ADC and the GPIO peripherals according to the application.

 

mcuxpresso.png

 MCUXpresso main window with the Peripheral tool highlighted

 

To save time and memory, right-click each unnecessary peripheral driver and remove it. Keep only the ADC_1, GPIO_1 and LPUART_1. Save the changes and double-click on the ADC_1 peripheral to configure it.

Now in the middle of the screen you should see the ADC configuration window. Here you can configure the peripheral parameter according to your application needs. For this application, make the following changes:

  • Preset : Default
  • Channel configuration : Press the + button. A table will appear. Click on Channel number and select IN, 9 

 CosminB05_6-1629802022541.png

Configuration window for the ADC peripheral

 

Note that this channel already has a pin configured in the default mex file.

If the user desires to configure another channel that is not configured by default, press on Channel number and choose IN,1; IN,2 or CHANNEL_DRIVEN_BY_ADC_ETC.

Be careful when assigning pins and channels that may generate conflicts with other peripherals.

Next, configure the GPIO_1 peripheral by applying these simple changes:

  • Uncheck Enable combined interrupts 0-15 request
  • Uncheck Enable combined interrupts 16-31 request

 

gpio.PNG

 GPIO configuration window

 

The LPUART peripheral does not have to be modified. Save the changes by pressing Ctrl+S. You do not need to update the code as this operation happens automatically.

Next switch to the Pins tools in order to assign and configure the pins and signals to the peripherals.

 

PinTool.png

 Pin configuration tool window

 

In the upper-left side of the picture there are the peripheral signals that can be routed or unrouted to the specific peripheral. Let's unroute all the peripheral signals, but keep the ADC1, GPIO1 and LPUART1.

To unroute the signals simply click on the peripheral checkbox on the list and press the Unroute All button.

 

unroute.png

 Removing AOI peripheral signals

 

For the ADC peripheral check only the IN,9 signal. And for the GPIO peripheral check only the gpio_io,09 signal.

If everything is done right, the routed pins should look like this.

 

signals.PNG

Routed pins

 

Press Ctrl+S to save the changes and now you can exit the MCUXpresso configuration tool.

Back in your model, press Update on the ADC block in order to synchronize the mex configuration with the Simulink model and interface, then press Apply.

 

CosminB05_11-1629802090279.png

Update and applying the changes to the Simulink model

 

Before starting the external mode run, the thermistor module must be connected to the MIMXRT1060 EVK Board. The following table tells how to connect each pin of the module to the board.

S

+

-

Pin J23[3]

Pin J25[5]

Pin J25[6]

 

 

CosminB05_1-1629804060594.png

Hardware connection

CosminB05_22-1629802336460.png

Overview of the hardware

 

Note that is it safe to run the application without plugging the board to an external 5V supply. The OpenSDA port is enough to power the board and the thermistor module because the electrical current of the thermistor will be too small to affect the board.

Now that the hardware is configured and connected. It is time to run the external mode. To start the external mode click on HARDWARE tab in the Simulink model, make sure the hardware board is set to NXP MIMXRT1062xxxxA, choose your stop time and press Monitor & Tune button.

 

extmode.png

 External mode menu

 

results.PNG

 Scope results

 

In the scope you can see the results of the external mode. To rise the temperature you can pinch the thermistor. As expected, for a threshold of 30 degrees Celsius, the LED starts blinking when the temperature exceeds the value.

In the following chapter the application will be built the same way, but the variables will be displayed using NXP FreeMASTER software.

NXP FreeMASTER

FreeMASTER is a user-friendly real-time debug monitor and data visualization tool that enables runtime configuration and tuning of embedded software applications.

Once again, to save time, copy the content of the external mode Simulink model to a new model called "temperature_sensor_fm.mdl". Make the same configurations as described in External Mode chapter.

Open the model and remove the scope.

In order to create a communication between Simulink model and FreeMASTER software we need to add Data Store blocks.

In the Library Browser go to Simulink > Signal Routing and add into the model four Data Store Memory blocks.

A FreeMASTER Config block must also be added into the model. It can be found in the Utility Blocks > FreeMASTER Blocks.

Double click on each Data Store Memory block and make the following changes:

  • Set an appropriate Data store name in the Main tab
  • Select Data type "uint32" for the output voltage, "boolean" for the LED state and "double" for the other blocks.
  • Set Storage class on Volatile in the Signal Attributes tab

 

Datastore1.PNG

 

Datastore2.png

Data Store block properties

 

Add Data Store Write to the model. You can do it manually on the Library Browser or you can click or the Data Store Memory block, hold left-click on the left arrow and swipe to the left.

CosminB05_5-1629810232193.png

Connect each Data Store Write to its correspondent variable. The model should look as the following figure:

 

fmmodel.png

 Simulink model

 

Make sure you have the same model settings as in the external mode model. In order to build the application on the board, complete the following steps:

  • Click on the APPS tab in the Simulink model
  • Click on Embedded Coder button
  • Press the Build button

 

embedded.png

build.png

 Embedded Coder menu

 

If everything is configured well, the application will get downloaded on the board and on the Simulink model will appear the generated code.

 

CosminB05_0-1632817236981.png

Model with the generated code

 

In order to view the output variables a FreeMASTER project needs to be created. The next chapter will describe how to create one.

 

FreeMASTER Project

In this chapter a FreeMASTER project will be created in order to display the four variables from the Simulink model: output voltage, thermistor resistance, measured temperature and LED state.

Open the FreeMASTER program.

Save the project in the folder with the other Simulink model and configuration files.

 

CosminB05_2-1632817554910.png

FreeMASTER project file in the Simulink model folder

 

To run the application on the FreeMASTER software complete the following steps:

  • Go in the Tools menu and press Connection Wizard…
  • Press Next, click on Use direct connection to on-board USB port
  • Select the UART probe and its baud-rate (this should be automatically selected).
  • Press Next, select Yes, use the detected port settings and start using FreeMASTER tool, and press Finish.
  • Back in the main screen, in the upper-left press the Go! Button.
  • Click Yes to browse for an ELF or MAP file.

 

CosminB05_0-1632822146155.png

Selecting the UART Port and the baud-rate

 

CosminB05_28-1629802444260.png

Browse for the ELF or MAP file

 

CosminB05_1-1632822207113.png

Press the green GO! button

 

tempsnip.png

 Selecting the symbol file for the project

 

Now the application will start but no data is available because no data are selected for display.

In the bottom of the screen, right-click on the table and press "Create New Watched Var..."

Right-click on the Variable Watches window in the bottom of the screen and press Create New Watched Var..

 

CosminB05_30-1629802488665.png

Create a new watched variable

 

A window will open. Here you can set a name for a variable, the address of the variable and its format. Add the four variables: Temperature, Thermistor, LED State and Voltage. For the Output Voltage set "unsigned int" and "float" type for the others . You can also specify the unit for each variable in the Real type transformation section. 

 

CosminB05_31-1629802513989.png

 Variable options menu

 

Press apply and OK. In the bottom screen you should now see your three variables.

CosminB05_32-1629802528172.png

Table of watched variables

 

Now let’s add some plots for our variables.

Right-click on New Project under Project Tree in the upper left corner and click Create Oscilloscope …

CosminB05_33-1629802547884.png

Creating an oscilloscope

 

In the Main Settings tab give the name of your oscilloscope and in the Variables tab press the Add Variable button. In the Variable section in the lower left section, press the arrow and choose the wanted variable.

CosminB05_34-1629802565316.png

Add variable to oscilloscope

 

When you added all the oscilloscopes needed, press OK. Now, back on the main screen, press the green Go! Button. Now the application is running. To see the oscilloscopes, double click on each one of them in the left. To add multiple windows, simply right-click on the oscilloscope and press New window.

CosminB05_2-1632822575596.png

FreeMASTER project overview

 

Conclusions

This article teaches how to create a sensor temperature application using the IMXRT1060 EVK Board. To do this a model based design approach is used. Firstly, a Model-in-the-loop simulation is made in order to create a model that mimic the real system. Then, code is generated from this model and a Software-in-the-loop verification is being made to see if the code has the same behaviour as the model. The validation stage is concluded by running a Processor-in-the-loop validation to see if the cross-compiled code can actually be run on the IMXRT1060 EVK Board processor. After the verification stages the hardware is being configured using NXP MCUXpresso. ADC, GPIO and LPUART peripherals are being used by the application. The application then is deployed using two methods: External Mode and using NXP FreeMASTER.

In future works the application will be extended by creating a closed-loop system which will allow to control the measured temperature using an actuator (i.e. cooling fan or heating element) driven by a control algorithm (i.e. PID Control).

You will find attached all the files needed for this application.

In the end, I want to give you my sincere gratitude for your attention and interest in this article!