Main Content

Communicating with Android Devices

This example shows how to use the Simulink® Support Package for Android® Devices to tune the parameters and monitor the signals of an algorithm running on an Android device.


Simulink Support Package for Android Devices enables you to monitor and tune algorithms running on Android devices from the same Simulink models, which you use to develop the algorithms.

In this example you will learn how to tune and monitor an 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, you may want to adjust the gains of a simple filter that smooths out varying values of a sensor. This iterative process is called parameter tuning.

Simulink's External mode 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 an Android device.

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

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

When you have completed this example, try this example:

  • Working with Android Devices: How to use Simulink blocks from Android Devices block library.


We recommend completing Getting Started with Android Devices example

Required Hardware

  • Android device, configured using androidhwsetup

  • USB cable to connect the device to host computer

Task 1 - Setting up the Device

In this task, you will ensure that you are able to communicate with your device.

1. Connect your Android device to your host computer.

2. Turn on the device and ensure USB debugging is enabled. If not, run androidhwsetup to setup the device.

Task 2 - Run Simulink Model

1. Open the androidCommunicationExample Simulink model.

2. On the Modeling tab of the toolstrip, select Model Settings to open Configuration Parameters dialog.

3. Select the Hardware Implementation pane and select Android Device from the Hardware board parameter list.

4. Click Device options under the Target Hardware Resources section and ensure the listed Device matches your connected device. Click OK.

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

Task 3 - Communicate with Simulink Model

In this task, you will change value the gains of the filter.

1. Leave the device on a stable surface. Notice the values changing rapidly in the display.

2. Double click Gain1 and change Gain from 1 to 0.0167. Click OK or Apply.

3. Similarly change Gain in Gain2 and Gain3 from 0 to 0.4833.

4. Click OK or Apply.

5. Notice the output changing less rapidly than before.

6. Click Stop button in the Simulink model.

You have interactively found an optimal value for your filters while the algorithm is running on the device.

Other Things to Try

  • Replace Accelerometer with Gyroscope and repeat the same exercise to smooth out the output.

  • Increase the number of filter taps by adding pairs of Unit Delay and Gain.