UniversalSerial Quick Start

This page describes how to write an echo sketch to an Arduino, send a string from Unreal Engine, and receive the response returned by the Arduino.

Prerequisites

  • Complete the Setup and enable UniversalSerial.
  • Have an Arduino board (such as an Arduino Uno) and a USB cable.
  • Have the Arduino IDE installed on your PC, ready to write a sketch to the Arduino.

Write a sketch to the Arduino

Create a new sketch in the Arduino IDE with the following content. It is an echo program that returns each received line as [Uno] Echo: <received string> (Length: <length>).

void setup() {
  Serial.begin(115200);
  // Discard noise right after startup
  while (Serial.available()) {
    Serial.read();
  }
  Serial.println("Uno ready for communication");
}

void loop() {
  if (!Serial.available()) {
    return;
  }

  char buffer[64];
  int len = Serial.readBytesUntil('\n', buffer, sizeof(buffer) - 1);
  buffer[len] = '\0';

  // Remove trailing newline characters
  while (len > 0 && (buffer[len - 1] == '\r' || buffer[len - 1] == '\n')) {
    buffer[--len] = '\0';
  }

  Serial.print("[Uno] Echo: ");
  Serial.print(buffer);
  Serial.print(" (Length: ");
  Serial.print(len);
  Serial.println(")");
}

After writing the sketch to the Arduino, keep the Arduino IDE’s serial monitor closed. On Windows, UniversalSerial cannot open a port while another application, such as the serial monitor, has it open.

Blueprint overview

When finished, the flow looks like this. BeginPlay creates the Serial Port Manager and opens the port, the response is displayed on screen when received, and EndPlay closes the port.

  1. BeginPlay → create a Serial Port Manager with Construct Object from Class and store it in a variable
  2. Bind the events
  3. Specify the port name and communication settings with Initialize
  4. Start communication with Open
  5. Send a string with Send String UTF8
  6. Receive the echo in the Data Received String event
  7. EndPlay → end communication with Close

1. Create an Actor Blueprint

  1. In the Content Browser, create a Blueprint Class → Actor and name it BP_ArduinoEcho or similar.
  2. Open the Event Graph and show the BeginPlay and EndPlay events.

2. Create the SerialPortManager

Execute Construct Object from Class with the Class set to Serial Port Manager, and store the return value in a variable (for example, SerialPort). The type is a Serial Port Manager reference.

Blueprint example for creating a SerialPortManager

Keep referencing this variable during communication, so store it in a member variable, not a local one. The steps below also use the SerialPort variable.

3. Bind the events

Add the following events from the SerialPort variable.

EventWhen it firesParameters
ConnectedThe port was openedSerial Port: the manager itself
DisconnectedThe port was closedSerial Port: the manager itself
Data Received StringA string was receivedSerial Port, Data: the received string
ErrorAn error occurredSerial Port, Error Message: the error content
Blueprint example for binding SerialPortManager events

In this procedure, connect Print String to Connected, Data Received String, and Error so you can observe the behavior.

4. Specify the port and settings with Initialize

Run Initialize on SerialPort in the BeginPlay flow. The settings must match the sketch.

SettingValueDescription
Port NameThe first element of Get Available Serial PortsThe port of the Arduino you wrote the sketch to
Baud Rate115200Matches Serial.begin(115200) in the sketch
Data Bits8Use the default
ParityNoneUse the default
Stop Bits1Use the default
Flow ControlNoneUse the default
Data FormatUTF8StringSelect UTF-8 String to receive strings
Initial DTRtrueUse the default
Initial RTStrueUse the default

You can retrieve the list of currently available port names with the static function Get Available Serial Ports. Run it with the Arduino connected over USB, and pass the first element of the array to Port Name. Port names differ by platform (COM3-style names on Windows, names starting with /dev/ on Mac and Linux). If you want to be precise, you can also type the target port name directly.

Example of the Initialize node settings

5. Start communication with Open

Connect and run Open on SerialPort after Initialize. When the port opens, the Connected event fires and the Print String output appears.

Opening the port resets the Arduino, so wait 1-2 seconds for the Arduino to finish booting. Insert a Delay (2.0 seconds) after BeginPlay before the send step that follows.

If Open fails, the Error event fires. Check the error message.

6. Send with SendStringUTF8 and receive the echo

Run Send String UTF8 on SerialPort with Hello. Leave Add Delimiter as true. When true, the delimiter (default \r\n) is automatically appended to the end of the string.

Example of running the Send String UTF8 node

After sending, the Arduino returns the echo and the Data Received String event fires. Data contains the following string.

[Uno] Echo: Hello (Length: 5)

Run PIE. If this output appears on screen, the communication run is complete. To send repeatedly, change the setup so that a keyboard event (for example, Keyboard Events → 1) runs Send String UTF8.

7. End communication

Call Close on SerialPort from EndPlay. When PIE ends, the receive thread stops and the port closes.

Example of running the Close node

If you switch levels without calling Close, the port may remain open. Always include the shutdown step.

Troubleshooting

SymptomWhat to check
Connected never firesIs the Arduino connected? Is the Arduino IDE serial monitor closed? Does Port Name point to the correct port?
Error shows “Failed to open”Is another application holding the port? Is the port name correct?
Data Received String never firesDoes the baud rate match 115200? Is Data Format UTF8String? Did you wait long enough after sending (Arduino reboot)?
Received strings are garbledDo the baud rate and other settings match the sketch?
Received strings are cut off midwayDoes the sketch respond with a newline (\r\n)? Is the delimiter setting left at the default?
Error logs appear when PIE endsAre you calling Close in EndPlay?

Only the receive event matching the Data Format is notified. See Switch the data format in Use Cases for details.

  • Use Cases — binary communication, automatic port identification, multiple ports, and ObjectDeliverer integration
  • Blueprint API Reference — inputs and outputs of Initialize, the send nodes, and the events
  • Data Formats and Production — serial settings, port names per platform, and troubleshooting