Main Content

Communicating with Arduino Hardware

This example shows how to tune the parameters and monitor the signals of an algorithm running on Arduino® board.

Supported Hardware:

  • Arduino Mega 2560

  • Arduino Mega ADK

  • Arduino Due

  • Arduino Leonardo

  • Arduino Micro

  • Arduino Uno

  • Arduino MKR1000

  • Arduino MKR WIFI 1010

  • Arduino MKR ZERO

  • Arduino Nano 3.0

  • Arduino Robot Control Board

  • Arduino Robot Motor Board

  • Arduino Nano 33 IoT

  • Arduino Nano 33 BLE Sense

  • Arduino compatible ESP32 - WROOM

  • Arduino compatible ESP32 - WROVER

Available versions of this example:

Arduino Mega 2560 board: arduino_communication

The provided model is pre-configured for Arduino Mega 2560 and can be run on any of the board listed in the "Supported Hardware" section, by changing the "Hardware board" parameter in the configuration parameters dialog box of the model as described in Task 2 of this example.

Introduction

Simulink® Support Package for Arduino Hardware enables you to monitor and tune algorithms running on Arduino board from the same Simulink models from which you developed the algorithms.

In this example, you will learn how to tune and monitor the algorithm in real time as it is executing. When you are developing algorithms, it is often necessary to determine appropriate values of critical algorithm parameters in an iterative fashion. For example, a surveillance algorithm that measures motion energy in a room may use a threshold to determine an intruder in the presence of ambient noise. If the threshold value is set too low, the algorithm may erroneously interpret any movement as an intruder. If the threshold value is set too high, the algorithm may not be able to detect any movement at all. In such cases, the right threshold value may be obtained by trying different values until the desired algorithm performance is reached. This iterative process is called parameter tuning.

The External mode (Monitor and Tune) feature enables you to accelerate the process of parameter tuning by letting you change certain parameter values while the model is running on target hardware, without stopping the model. When you change parameter values from within Simulink, the modified parameter values are communicated to the target hardware immediately. The effects of the parameters tuning activity may be monitored by viewing algorithm signals on scopes or displays in Simulink.

This example introduces the Simulink External mode feature by showing you how to:

  • Set up communication between Simulink and Arduino board.

  • Use a Simulink model to tune the parameters of an algorithm that is running on Arduino board.

  • Use Simulink scopes to monitor the state of an algorithm running on Arduino board.

Prerequisites

Before you start with this example, we recommend you complete the Get Started with Arduino Hardware example.

Required Hardware

To run this example you will need the following hardware:

  • Supported Arduino board

  • USB cable

  • LED

  • 220 Ohm resistor

  • Breadboard wires

  • Small breadboard (recommended)

Task 1 - Connect LED to Arduino Output Pin

In this task, you will connect an LED to an Arduino digital output pin so you can see changes in the logical state of the pin.

1. Attach one end of the 220 Ohm resistor to digital output pin 9 on the Arduino board. Use the recommended breadboard and the breadboard wires.

2. Attach the long leg (positive) of the LED to the resistor. Attach the short leg (negative) to the ground pin on the Arduino board.

Task 2 - Configure Simulink Model for Supported Arduino Hardware

In this task, you will configure the model for the supported Arduino board.

1. Open the arduino_communication Simulink model.

2. In your Simulink model, go to the Modeling tab and click Model Settings to open the Configuration Parameters dialog box.

3. Go to the Hardware Implementation pane and select your required Arduino hardware from the Hardware board parameter list. Do not change any other settings.

4. Click OK.

Task 3 - Simulate Model

To simulate the model, follow these steps:

1. Observe that the model plays the motion energy recorded in a room and compares it with a threshold to detect intrusion.

2. In the Simulation tab of the Simulink Model toolbar, change the Simulation mode to Normal. This tells Simulink to run the model on the host computer. See Task 4 below to run the model on the Arduino target hardware.

3. In the model, click the Run button in the Simulink toolbar.

4. Click the Scope block. Observe that the algorithm detects multiple intrusions.

5. Click Stop button in the Simulink model.

Task 4 - Run Simulink Model

1. Connect a USB cable from your computer to the Arduino board.

2. On the Hardware tab of the Simulink model, in the Mode section, select Run on board and then click Monitor & Tune.

Task 5 - Communicate with Simulink Model over Serial

At this point, your model is running on Arduino board. As the model runs on hardware, it communicates with Simulink model in External mode using serial connection over USB.

1. Notice that the LED attached to pin 9 is glowing almost constantly. This means that the selected threshold is too low and that the algorithm misinterprets even a minor motion energy change as an intrusion. You need to find a more optimal value of the threshold.

2. Double-click the Threshold block in the model, increase its value, and click OK or Apply. This changes the threshold value in the model running on the board.

3. Check whether the glowing pattern of the LED has changed. The LED should light up every 10 and 11.5 seconds in a correctly tuned algorithm.

4. If there is no change in LED light pattern, repeat the Steps 2 and 3 until you find the right value of the threshold.

5. Click Stop in the Simulink model.

Troubleshoot Serial Communication

1. An error stating 'Attempting to establish connection with hostname 127.0.0.1 through port ...' indicates that the TCP/IP port 17725 on the local loopback is held by some other application. External mode uses a process in the background to buffer data and improve data logging. This process needs the port 17725.

2. High base rates may cause the time in the scope window to freeze. This is a limitation of the firmware on Arduino boards. Using an FTDI chip as the USB-Serial bridge resolves this issue.

Task 6 - Configure Simulink Model to Run in External Mode over TCP/IP

Refer to the documentation section: Configure Model to Run in External Mode over Ethernet.

Task 7 - Configure Simulink Model to Run in External Mode over WiFi

Refer to the documentation section: Configure Model to Run in External Mode over Wi-Fi or Serial.

Other Things to Try

  • Monitor other signals in the model. For example, add another scope to monitor the value of the recorded motion energy.

  • Improve the detection algorithm to filter out any motion energy changes that are shorter than 0.2 seconds.