Control Arduino with unity

How we create an app in Unity and then control an Arduino!

Using the Arduino development board we are able to do some pretty cool stuff. In this article / Video we are going to control Arduino with unity game engine.

We are going to control an RGB LED that is connected to arduino from Unity.

Game Engine unity

Game engine Unity, is a game engine where you can build games and applications. It uses C# as a programming language and has an easy to learn interface.

You can create both 2D and 3D games.

The project we created could be development using a different software more proper but we chose unity.. well because it was cool.

The project

We need a program that will control through Unity an RGB LED.

If you take a minute to think what the possibilities are for these kind of projects you will be amazed.

You can connect a device that has temperature or humidity sensors (wind, water, barometric) and you can read these data and interact from a PC or a Smartphone.

This can work both ways

Through a user interface that you can make in Unity (or any other sofware) you can control hardware like your garagedoor or tents, activating the alarm system, cameras, turning on-off the boiler, etc..

The circuit

We used Fritzing to design the circuit. Just connect as you see after you gather the following components:

  1. Arduino / Genuino Uno /Mega
  2. 3x resistors 220Ohm
  3. 1x RGB LED common cathode
  4. Jumper wires
  5. Breadbroad to connect all

The connection is simple as it looks we connect the three colors of RGB LED to inputs 7,8,9 which are PWM (very important to choose PWM inputs)

Resistors are in series at 7,8,9 pins for protection. The GND pin of the led is connected to GND pin of Arduino mega.

unity send data to arduino

In the video i use 270Ω instead of 220Ω but the light is bright enough for me.

What is RGB LED?

LEDs or photodiodes usually have 2 pins. One is anode(+) and the other is kathode(-). when we apply DC current with the right polarity they emmit light.

There is a special photodiode category, RGB where they have 3 photodiodes in their head, one with different color. Red, Green, Blue.

We can apply to each head of this LED DC current to light a different color.

In theory the different combinations of colors we can get are 16.7 million of colors( 256*256*256)

As you see in the above image (looking from left) there are three LEDs with different color. So, if we wanted to have different light color we would need that many LEDs Orrrr we could just use an RGB LED (image on right)

For this project we had to connect the Arduino mega 2560 to the computer. That means we can “talk” to arduino through serial communication.

If you can understand that Serial communication is the key to read / write values you can achieve this with ANY programming language or Programming Software.

We created a UI at Unity with 3 sliders that represent each color on the RGB led.

Next we created 3 point lights Red, Green and Blue so that we can see in the monitor how the light is mixing colors. Finally we attached scripts so that the sliders handle the intensity of point lights

The code requires that we add ΙΟ.Ports library.

Before we can use the IO.Ports Library we have to take the following steps:

Unity Code

We are going to need two scripts. One will be responsible for the serial communication, while the other is for data send.

script communication.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO.Ports;

public class communication : MonoBehaviour
{
  public SerialPort stream = new SerialPort("COM9", 115200);
  void Start ()
  {
    stream.Open();
  }
}

What is happening is we are creating a connection to serial port COM9. You may have to change COM9 to your corresponding serial port when you connect arduino to your computer.

See next, how you locate the COM port that is correct for you or just watch the video.

We are going to attach the script to main camera. I renamed the main camera to camera (easier to remember)

Other script is called Handle:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HandleLight : MonoBehaviour

 {
    private int Multiplier = 128;
    private float sliderValue;
    GameObject Connection;
    private void Start()
    {
        Connection = GameObject.Find("Camera");
    }

public void OnLightChange(Light light)
    {
        sliderValue = this.gameObject.GetComponent().value;
        light.intensity = sliderValue / Multiplier;
        if (light.name == "redPin")
        {
            Connection.GetComponent().stream.WriteLine("7" + sliderValue.ToString());
        }
        else if (light.name=="greenPin")
        {
            Connection.GetComponent().stream.WriteLine("8" + sliderValue.ToString());
        }
        else
        {
            Connection.GetComponent().stream.WriteLine("9" + sliderValue.ToString());
        }
        
        Debug.Log(sliderValue.ToString());
        //communication.stream.WriteLine(light.name+light.intensity);
    }
}

The line of code that sends data to arduino for each color is

Connection.GetComponent().stream.WriteLine(“7” + sliderValue.ToString());

Where we used to serial communication object and with the stream.WriteLine (string), we send the data to string format. Arduino receives in Serial communication in byte.

We are going to attach the Handle light script to each of the three sliders and then, we are going to use unity’s listener to detect changes on slide. (βλ. video)

Data receive from Arduino

First of all let’s connect our arduno to usb port and check which COM is being used. If you have COMX, then use it on the first unity script to get it working.

Don’t mind the IDE is in Greek language, just follow along to the same menu option to see which port you are using.

See which port arduino is using tools-> ports – COMX.

If you have many ports, check before pluging in the arduino, plug the arduino and check again. The new port that will appear is arduino’s COM port.

The port that you see is arduino port

public SerialPort stream = new SerialPort(“your port”, 115200);

It is important to know that arduino reads byte in serial communication!

So in arduino code we will read each byte seperately and then convert it to string.

Then will convert the string to Int with .toInt() command in order to use it.

arduino code

You can download code from here

int redPin= 7;
int greenPin = 8;
int bluePin = 9;
String valueFromSerial;
void setup() 
{
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  Serial.begin(115200);
}

void loop()
{
  while (Serial.available() > 0) 
  {
    int inChar = Serial.read();

    if (isDigit(inChar))
    {
      valueFromSerial += (char)inChar;
    }
    if (inChar == '\n') 
    {
      analogWrite(valueFromSerial.substring(0,1).toInt(),valueFromSerial.substring(1,3).toInt());
      valueFromSerial = "";
    }
  } 
}

We read the bytes one by one.

If a byte is a number they are converted to string and added to a variable. Next we use that variable like valueFromSerial.toInt()

Final, to lighten up the desired LED color we use analogWrite (x,y) which has the following syntax,

analogWrite(pin, value)

TIP: Code is case sensitive!.

Watch for the details, arduino IDE does now give you very good feedback on what is going wrong.

0 responses on "Control Arduino with unity"

    Leave a Message

    Your email address will not be published. Required fields are marked *

    Σχεδιασμός © GetCert. Με επιφύλαξη παντός δικαιώματος.