How to Integrate Arduino with Unity 3D

Featured Image Integrate Arduino With Unity 3d

Arduino has a great IDE, but it lacks serious coding features and attractive dashboards. Such limitations can be overcome by integrating Arduino IDE with professional software environments such as Unity 3D, a famous gaming engine. The following guide will show you how to integrate Arduino with Unity 3D and help you make the Unity games technically compatible with Arduino for all your project requirements. As you will learn, this can be achieved without any extra coding: all you need is a free, open-source plugin.

Install Serial Command Library in Arduino

First, you need to have a proper serial command library in Arduino which can be imported from “Add .ZIP library.”

Arduino Unity Add Zip Library 1

For our demo, add the required Serial Command library from this GitHub link. This is used for communication between the Arduino board and a computer or other devices. Once uploaded, it will be visible in the contributed libraries. (Learn how to add a library in Arduino here.)

Arduino Unity Added Library Arduinoserialcommand 1

Getting Started with Unity Hub

If you’re new to Unity Hub, you can download its package for Windows, Mac, and Ubuntu. It has an .exe file, which a firewall would block, so special access should be enabled before installation.

Arduino Unity Windows Firewall Access 1

Wait for the interface to launch properly while the modules and components are imported gradually. The installation can take a while, so be patient.

Arduino Unity Launch

In the meantime, you can create a Unity ID and subscribe to a free personal plan for individuals.

Arduino Unity Create Id 1

Once your Unity dashboard is generated properly, you are ready to begin.

Arduino Unity Dashboard 1

Create an Arduino Staging Environment in Unity

Go to “File -> New Project” and give it a name of your choice. Again, wait for the Unity window to be launched.

Arduino Unity Create New Project 1

To allow bidirectional communication between Arduino and Unity, you will need a free plug-in. We recommend Ardity, and you can download a GitHub package for that.

Arduino Unity Custom Package 1

Once done, import the free communication utility as a “custom package” from Unity’s Assets menu as shown here.

Arduino Unity Import Unity Package 1

If you notice any errors while importing the package, you will have to fix the outdated Unity build which came as a default. Go to “File -> Build Settings -> Player Settings,” and modify the API compatibility levels to the latest .Net version as shown below.

Arduino Unity .net4x 1

As shown, navigate down to your imported asset and select a DemoScene which you will integrate with Arduino IDE. Selecting it will open a serial controller.

Arduino Unity Assets Ardity Selected 1

Take note of the port name which is COM3 as a default. Once you connect your Arduino device to a PC, you can trace the exact port in “Device Manager” (for Windows 10) from Portable Devices option. If it’s COM6 or anything else, manually edit the port name so that it matches Arduino’s.

Arduino Unity Port Name Selection 1

Note the Baud Rate frequency which we will use in Arduino IDE settings. This is a messaging frequency – for example, 9600 bits per second – which Unity’s serial port will be transferring to Arduino. The vital information will be used in the next section.

To create a new scene that you want to work on, right-click over the communication plugin and go to “Create -> Scene.”

Arduino Unity Create New Scene 1

The following shows a new scene with a cube gaming object which can move along an X-Y-Z trajectory.

Arduino Unity Import Gaming Object 1

After the scenes have been properly created and the Arduino port connections match, you can hit “Play,” and Unity will spring into action.

Integrate Arduino IDE with Unity

For the last step of communication, you will need your Arduino to interface with Unity. Let’s use one of the example sketches: “SerialCallResponse.” It allows multi-byte communication from the Arduino or Genuino board to the computer using a call-and-response (handshaking) method.

Arduino Unity Serialcallresponse 1

Refer to the baud rate in the Unity plug-in as shown in the above section. It gives the example frequency of 9600 bps which already matches the two-way protocol frequency for our example Arduino sketch. If your Arduino example code does not have the same frequency, you need to edit this value to match the Unity plug-in’s baud rate.

Arduino Unity Serialcallresponse Frequency 1

Now, go to “Tools” and select the Arduino board you are using and the port (“COM3/COM6,” etc.). Once done, click verify and upload to set the Arduino sketch in action. Correspondingly, if Unity 3D is also running at the same time on your PC, you will notice a “message established” status update.

Arduino Unity Compiling Sketch 1

To recap our integration sequence, you need the following things in common to connect Unity 3D with Arduino IDE.

  • Arduino’s serial frequency should match Unity engine’s Baud rate.
  • The same port should be enabled for both software interfaces.
  • Both should be on the same network and remain connected to the same peripheral device, such as a PC.

Whatever action scenes you create using your imagination in Unity 3D, the same motion sequences and trajectories can be transferred to an Arduino physical device over Arduino IDE. This is a good related example by a developer that was published with Arduino’s official website.

Arduino Unity Motion 1

In Summary

Unity is a popular 3D software environment widely used in mobile games, along with augmented reality and virtual reality devices such as Microsoft Hololens and Samsung Gear VR. There can be so many applications of a Unity-Arduino merger: from designing your own gaming joystick to an Arduino-based VR headset.

Leave a Comment

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.