Arduino Tutorial Series: Connecting to Unity

Rodolfo Cossovich
interface-lab
Published in
4 min readJul 13, 2020

Connecting a button

Using a similar circuit to the one we used to connect a button to a potentiometer, this time we will connect the signal to D2. The board definition we are using as a reference for the pins of Arduino Nano 33 IoT is https://itp.nyu.edu/physcomp/introduction-to-the-nano-33-iot/

We will use the code from File / Examples / Basics / DigitalReadSerial. Remember before uploading to verify that you have selected the correct port at Tools / Port.

Arduino with Push Button on Digital Pin 2

NOTE: The code we use has the modification of changing the delay to 100ms. This will be important in the following steps for the plugin to correctly work.

/*
DigitalReadSerial connected to Unity using Ardity

Reads a digital input on pin 2, prints the result to Serial
On my mac, it uses /dev/cu.usbmodem1451301
The main modification is the delay for 100ms
This example code is in the public domain and based on http://www.arduino.cc/en/Tutorial/DigitalReadSerial
*/
// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;
// the setup routine runs once when you press reset:
void setup() {
Serial.begin(9600);
pinMode(pushButton, INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
bool buttonState = digitalRead(pushButton);
Serial.println(buttonState);
delay(100); // IMPORTANT ---> delay in between reads
}

Setting up Unity

After starting a new project in Unity (I’m using the version from 2019.3), we go to Window / Asset Manager to install and import the asset called Ardity. Once installed, you will still need to configure the settings manually to make it work on your computer. The first thing to check once imported is within the Console the errors reported; it’s very likely that you need to get into your File / Build Settings / Player Settings to choose NET 4.X instead of NET Standard 2.X

Settings for App Compatibility Level need to be modified

For our example, we will need two 3D objects: a cube and a plane. Make sure that they can be seen from the camera. The PDF guide provided with the Ardity plugin explains in greater detail how to configure it and there are also instructions at the author’s Github Repository and Developer’s Website.

We will link the SerialController script located in Assets / Ardity / Scripts to the Cube object. We will need to configure the name of our communication port, which in Mac computers has the shape of “/dev/cu.usbmodemXXXX” and in PC computers “COMXXXX”. Any misspellings will be an obstacle for the plugin to work, so double-check you have it right.

If everything was properly configured, you should see in your console an endless “Queue Full: Message dropped”. This is a good sign because it indicates that your Arduino is sending information and you are receiving it from Unity.

Using Arduino with a button to control an object in Unity

Following the instructions from the PDF guide, we need to indicate within the SerialController that we will include a MessageListener in the Cube object and that we will have such a script attached to the object. To that end, we created a script called MyListener. Pay attention that the naming convention of the file name should be coincident with the script content so that Unity finds it.

Make sure you are not running Arduino Serial Monitor when you are trying to use Unity. The serial port has exclusive handling from the operative system and you will not be able to receive messages while the Serial Monitor is open.

With this script, we first confirm that the message is received and decoded by seeing it’s content in the Console and then we can use it to alter parameters of the Cube itself.

/**
* Ardity (Serial Communication for Arduino + Unity)
* Author: Daniel Wilches <dwilches@gmail.com>
* Modifications for InterfaceLab 2020 to move a cube
*
* This work is released under the Creative Commons Attributions license.
* https://creativecommons.org/licenses/by/2.0/
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyListener : MonoBehaviour
{
GameObject cubeModifier;

void Start() // Start is called before the first frame update
{
cubeModifier = GameObject.Find("Cube");
}
void Update() // Update is called once per frame
{
}
void OnMessageArrived(string msg)
{
Debug.Log("moving at speed: " + msg);
float speed = float.Parse(msg) * 100;
cubeModifier.gameObject.transform.Translate(Vector3.up * Time.deltaTime * speed);
}
// Invoked when a connect/disconnect event occurs. The parameter 'success'
// will be 'true' upon connection, and 'false' upon disconnection or
// failure to connect.
void OnConnectionEvent(bool success)
{
Debug.Log(success ? "Device connected" : "Device disconnected");
}
}

All the code used here is also hosted in the Github repository at https://github.com/todocono/InterfaceLab2020. You will see the Arduino code and a Unity package ready to download and import. When using any code example, you should still rename the USB device name to match your computer.

--

--

Rodolfo Cossovich
interface-lab

Rudi. From Argentina but with a dozen of years in China, and counting. My research interests are diverse as robotics, education and interactive arts.