How to Integrate Arduino with Unity 3D

Featured Img Arduino Integrate With Unity3d Software

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 (or just Unity), a famous gaming engine. The following guide will show you how to integrate Arduino with Unity and help you make the Unity games technically compatible with Arduino for all your project requirements.

Did you know? You can use different operating systems to work with Arduino? Here is a short list of the best operating systems.

1. Install Serial Command Library in Arduino

  1. You need to have a proper serial command library in Arduino which can be imported from “Add .ZIP library.”
Add ZIP library from Include Library in Arduino's main console window.

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.)

Import ZIP folder containing serial command library in Windows 11.
  1. A better way though is to use “File > Open” and import the required sketches for working with Unity, which will help you sync both programs in Windows and other operating systems.
File Open in Arduino IDE for Windows.

Here we are importing the downloaded Serial Command Example as a Processing IDE (or PDE) file. This would automatically install in Arduino IDE.

PDE file of SerialCommandExample imported via Arduino's Windows folders.
  1. Once the Serial Command Example has been opened in Arduino IDE, you can modify its values based on comparable parameters in Unity which we will demonstrate here.
SerialCommandExample PDE file imported in Arduino IDE.

If you don’t like the plain theme of Arduino IDE, you can always modify it from the various settings options available in the editor.

2. Getting Started with Unity Hub

If you’re new to Unity Hub, you can download its package for Windows, Mac, and Ubuntu.

  1. In Windows, you will be dealing with a third-party .exe file, which a firewall would block, so special access should be enabled before or after installation if such a message appears.
Unity Hub blocked by Windows 11 Firewall.
  1. Give your consent to a license agreement to proceed with the installation process.
UnityHub license agreement in Windows 11 during installation.
  1. It takes a few minutes for the installation to complete on your computer.
Unity Hub installing in Windows 11.
  1. In the meantime, you can register to create a Unity account and subscribe to a free personal plan for individuals.
Create Unity Account from its Windows application.
  1. Creating a Unity ID is part of the process. This is what you will use to link your Arduino projects with Unity account details.
Creating a Unity ID online.
  1. The Unity ID registration will take you to a browser page where you will get the option to directly launch Unity Hub from the browser itself.
Open Unity Hub from browser window.
  1. In latest versions of Unity for Windows, you must install a Unity Editor software separately. It requires considerable space and time as the installer file is several GB in size.
Install Unity Editor in the application latest recommended version.
  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.
Process of installing Visual Studio in Unity.
  1. Once your Unity Editor is installed properly, you are ready to begin.
Unity Editor is installed properly inside Unity Official for Windows.

3. Create an Arduino Staging Environment in Unity

  1. Go to “Projects” in the newly installed Unity Editor and launch a new project from the “New project” menu.
Arduino Unity3d Launch New Project
  1. Choose a 3D project and give it a name of your choice. Again, wait for the Unity window to be launched.
Create 3D project in Unity window as shown.

The project homescreen should now be visible. This is your main interface to design various games on Unity.

Unity Editor Project homescreen
  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.
Import a package in Unity Editor Project.
  1. Once done, import the free communication utility as a “custom package” from Unity’s Assets menu as shown here.
Ardity components installing in Unity software one by one.
  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.
Unity's API compatibility settings changed to latest version of .NET as shown.

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.

Demo scenes of Ardity visible in Unity Projects.
  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/11) from Portable Devices option. If it’s COM6 or anything else, manually edit the port name so that it matches Arduino’s.
COM3 port and Baud rate
  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.
  2. To create a new scene that you want to work on, go to “File > New Scene” and use a basic built-in scene or create from an “empty” scene.
Create New Scene in Unity

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

New scene created in Unity

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

4. 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.

  1. Open the sketch from any Arduino window using “File > Examples > Communication > SerialCallResponse.” You can also use an imported library sketch as shown in the first section on top.
SerialCallResponse in Arduino
  1. Refer 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.
  2. 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.
Baud rate serialcommandexample as value 9600
  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.
Compiling sketch in Arduino for Unity 3D.

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

Frequently Asked Questions

What are the applications of Unity Engine in Arduino?

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.

Can I connect Unity to Arduino using plugins?

Unity has a few built-in plug-ins available which can connect to Arduino, such as Uduino. Apart from them, you can use external open-source plug-in components such as Ardity.

Subscribe to our newsletter!

Get the best of IoT Tech Trends delivered right to your inbox!

Sayak Boral

IoT-addicted since early 2016. Love to explore the challenges, opportunities and trend insights into what is becoming the third wave of Internet.