Getting Unity and Arduino Working Together

Yesterday I set myself a goal of getting Unity talking to Arduino, the microcontroller that’s hugely popular in the maker community. I was interested in doing so because several of our VR projects have been site-specific installations that could benefit from a large LED scoreboard, physical actuators (rumble motors, heat lamps, fans, etc.) to heighten the experience, or just a “This person flailing his arms around can’t see you; please stand back!” warning light.

Fortunately, given than both Unity and Arduino are very popular, this path is fairly well-trodden already. The most common approach is to establish a serial connection between the computer running Unity and the Arduino board. This is straightforward to do, though it requires tweaking a few settings in Unity to allow access to the serial libraries.

For the Arduinos that have USB port, the physical connection is as simple as plugging in a USB cable. For the boards that have only serial pins, wiring in a converter chip will be necessary to bridge the two. I used the Arduino Uno since it supports USB and I didn’t feel like soldering.

The next decision is whether to write your code from scratch or to use a library to ease this task. If you’re more of a DIY person, the amount of code you have to write isn’t outrageous. Alan Zucconi has an excellent article that will guide you through what you need on both systems.

If you’re looking to get up and running quickly, however, you may prefer one of the options available in the Unity Asset Store. These come with all the code you need for many use cases already written, a variety of examples, and niceties like custom UI in the Unity editor. I ended up using Marc Teyssier’s excellent Uduino package, which has good documentation and supports digital input and output, analog input and output, and servo motor control out of the box.

(I ended up having trouble at first, as my board wasn’t setting the pin mode to digital output when the code commanded it to. I wasn’t sure whether this was a problem with my board or with the code, but adding some additional commands to the Arduino code to make sure that the pin mode got reset cleared up the problem.)

I rigged up a simple relay circuit, created a sample scene, and before too long, had a light in my living room turning on and off with the scene lighting in Unity. Viola!

This is merely a proof of concept — a technical spike to sort out some unknowns. Now that we’ve got these waters mapped, I hope to add some production physical effects to future projects. Excelsior!