Main Content

Encode and Decode Serial Data Using Arduino Hardware

This example shows how to use Simulink® Support Package for Arduino® Hardware to encode and decode serial data with Arduino hardware.

Supported Hardware

  • Arduino Mega 2560

  • Arduino Mega ADK

  • Arduino Uno

  • Arduino Due

  • Arduino MKR1000

  • Arduino MKR WIFI 1010

  • Arduino ZERO

  • Arduino Leonardo

  • Arduino Nano 33 IoT

  • Arduino Nano 33 BLE Sense

Introduction

In this example, the Simulink model, performs these actions:

  • At the transmission end, multiple fields are encoded into packet using Protocol Encoder block and the resulting uint8 byte stream is transmitted using Serial Transmit block.

  • At the Receiving end, the byte stream is received using Serial Receive block and decoded into individual fields using Protocol Decoder block. The status output of Serial Receive block, which indicates a new data is available, is used to trigger the enabled subsystem containing Protocol Decoder block.

In this model, the TX1 pin sends serial data to the RX1 pin of your Arduino hardware.

This model is configured to run in External mode. For more information on External mode, see Tune and Monitor Model Running on Hardware.

open_system('arduino_encode_decode_packet');

The model provided in this example is preconfigured for the Arduino Mega 2560 board. You can run these models on any of the boards listed in the Supported Hardware section by changing the Hardware board parameter in the Simulink model. For more information on how to change the Hardware board parameter, see the Step 2: Configure the Model for Arduino Hardware section of this example.

Prerequisites

Before you start with this example, we recommend you complete the following:

Required Hardware

To run this example, you must have the following hardware:

  • Supported Arduino board

  • Connecting wires

  • USB cable

Step 1: Connect TX and RX Pins on Arduino Hardware

1. Connect your Arduino board to your computer using the USB cable.

2. Connect the TX1 pin to the RX1 pin of your Arduino hardware. This connection is a loopback connection.

Step 2: Configure the Model for Arduino Hardware

1. Open the Simulink model model. This model is configured to run in External mode.

2. To configure the model, click Hardware Settings in the Hardware tab of the Simulink toolbar.

3. In the Configurations Parameters dialog box, select Hardware Implementation.

4. From the Hardware board list, select the type of Arduino board that you are using.

5. From the Groups list under Target hardware resources, select Serial port properties.

The Serial 1 baud rate parameter is set to 9600. In this example, the baud rate of Serial 1 baud rate is set because the TX1 and RX1 pins correspond to the Serial port 1 of the Arduino hardware. If you are using any other serial port pins for communication, specify the baud rate of the corresponding serial port.

6. Click Apply. Click OK to close the dialog box.

Step 3: Configure the Blocks in the Simulink Model

The packet structure used in this example is:

Double-click the blocks and verify the parameter values specified in the Block Parameters dialog box.

For other blocks, the parameters are:

     Block           | Parameter Name                      | Value
    ---------------------------------------------------------------------------------
     Constant        | Interpret vector parameters as 1-D  | selected
                     | Sample time                         | inf
     Serial Transmit | Serial Port                         | 1
                     | Send mode                           | write
     Serial Receive  | Port number                         | 1
                     | Data type                           | uint8
                     | Data length                         | 10
                     | Sample time                         | 0.1
     Display Status  | Format                              | short

The value for Data length parameter of Serial Receive block is set to 10, which is the sum of these values:

Header size (1 byte) + Data1 (1 byte) + Data2 (2 bytes) + Data3 (4 bytes) + Checksum (1 byte) + Terminator (1 byte)

Note: Because the TX1 pin is connected to the RX1 pin, specify the Serial Port parameter of the Serial Transmit block and the Port number parameter of the Serial Receive block as 1.

Step 4: Validate the Model Design Using Connected I/O

You can simulate the model in Normal mode with Connected I/O to validate the model design before generating the code and deploying the model on Arduino. When you simulate the model, the TX1 pin transmits data to the RX1 pin of your Arduino hardware. This communication between the model and Arduino does not require any code generation or model deployment, thus accelerating the simulation process. For more information on Connected I/O, see Communicate with Hardware Using Connected IO.

To simulate the model in Normal mode with Connected I/O:

1. On the model toolbar, in the Simulation tab, set Simulation mode to Normal.

2. To run this model in the Connected I/O mode, on the Hardware tab, in the Mode section, select Connected IO and then click Run with IO.

The lower left corner of the model window displays status while Simulink prepares, downloads, and runs the model on your computer.

At each time step, data specified in the Constant blocks are encoded into uint8 byte stream (packet) and transmitted by the TX1 pin to the RX1 pin of your Arduino hardware. You can see the uint8 byte stream generated as per the packet structure in the Display encoded data block.

The RX1 pin receives the uint8 byte stream which is decoded using Protocol Decoder block and displays it on the Display Decoded data blocks. Observe the output in the Display Decoded data blocks which will be same as the value given in the Constant blocks at the transmission end. Try changing values in the Constant blocks and observe the changes in the display blocks.

3. Disconnect the wire from the RX1 pin. Display Decoded data blocks now output 0. A value of 0 in the Display IsNew block indicates that no new data is received at the given time step. If you reconnect the wire again, you can see that the values at the Display Decoded data blocks are restored.

4. To stop running the model, click Stop.

Step 5: Run the Model in External Mode

After you successfully simulate the model in Connected I/O, simulate the model in External mode. Unlike Connected I/O, the model is deployed as a C code on the hardware. The code obtains real-time data from the hardware.

The Stop Time (under Simulation tab) is already set to inf.

1. To tune parameters and monitor signals in this model while the application runs on the target hardware, on the Hardware tab, in the Mode section, select Run on board and then click Monitor & Tune.

The lower left corner of the model window displays status while Simulink prepares, downloads, and runs the model on the hardware.

2. To stop running the model, click Stop.

Other Things to Try

  • Try specifying a different packet structure using the block properties and observe the encoded data and decoded data in the Display blocks.