USB Serial Communication Using Android Devices

This example shows how to use the Simulink® Support Package for Android™ Devices to send and receive serial data from Arduino hardware.

In this example, you can exchange data between an Android device and Arduino hardware using a USB cable.

This example uses two Simulink models:

  • Android model: In this model, the azimuth data from orientation sensor block is sent to Arduino and receives data from the Arduino device.

  • Arduino model: In this model, data received from your Android device is sent back to the Android device.

With these models, you can:

  • Set up a USB serial connection between an Android device and Arduino board

  • Configure and run Simulink models for Android device and Arduino board

Prerequisites

Before you start this example, you must:

Required Hardware

  • Android phone or tablet

  • Arduino Mega 2560/Mega ADK/Due/Leonardo/MKR1000

  • USB cable

  • USB OTG converter

Task 1 - Configure the Model for Supported Arduino Hardware

You can skip this task if you are using the Arduino MKR1000.

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

1. In your Simulink model, open the Modeling tab and press Ctrl+E to open the Configuration Parameters dialog box.

2. Select the Hardware Implementation pane and select your Arduino hardware from the Hardware board parameter list. Do not change any other settings.

3. Click OK.

4. Similarly, configure the model for Android device

Note: Ensure that Baud rate (Hardware Implementation > Hardware board settings > Target Hardware resources > Serial port properties) is the same in both the Android device and the Arduino hardware. Serial communication is not possible if Baud rate is different.

Task 2 - Configure and Run Models on Android Device and Arduino Hardware

1. Open the Android model.

open_system('androidSerialSendReceive');

2. Go to the Hardware tab and click Build, Deploy & Start to run this model on the Android device.

3. Open the Arduino model

open_system('arduino_android_serial');

4. Go to the Hardware tab and click Build, Deploy & Start to run this model on the Arduino device.

5. Connect the micro-USB end of the USB OTG converter cable to the Android device. Connect the USB OTG converter end to regular USB end of the USB cable. Also connect the micro-USB end of the regular USB cable to the Arduino hardware. You can also connect the USB OTG converter to the Arduino hardware and USB cable to Android device.

Note: For security reasons, whenever an USB device is connected to an Android device, accept the message that appears on the Android screen.

6. On your Arduino hardware, verify if the LED is glowing. This indicates that the Arduino hardware is receiving power from the Android device.

Other Things to Try

Summary

This example showed you how to:

  • Power the Arduino board using an Android device and control the application running on an Arduino board from an Android device.

  • Send serial commands from an Android device to Arduino hardware.