Communication between Arduino and Unity

Yifei Yin
2 min readApr 3, 2017

--

Tutorial:

This might be the simplest way to communicate between Arduino and Unity. I use the serial port to communicate between Arduino and Unity.

In Unity, set the network configuration as NET 2.0 instead of NET 2.0 subset.

Edit>ProjectSettings>Player>ApiCompatibilityLevel.

Arduino Part

  1. Set a baud rate in the Arduino code by Serial.begin(9600);
  2. Serial.flush(); Waits for the transmission of outgoing serial data to complete.
  3. Serial.println(); Prints data to the serial port as human-readable ASCII text followed by a carriage return character (ASCII 13, or ‘\r’) and a newline character (ASCII 10, or ‘\n’). This command takes the same forms as Serial.print().
  4. The Port name can be found on the bottom right corner of the Arduino interface or on the Tools> Port > …. Mine is “/dev/cu.usmodem1421”
Arduino Code

Unity

  1. Use the System.IO.Ports library
  2. Set the port and baud rate by SerialPort stream = new SerialPort (“device name here”, 9600);
  3. Initialize: stream.Open();
  4. In update, read the stream data by stream.ReadLine();
  5. Display the information received on GUI
Unity Code (c#)

--

--