In this tutorial, I used Serial.print to print the information from Arduino to Unity. With the most recent update, you can use uduino.print, which is doing the same but ensures compatibility with Uduino Wifi plugin

Our aim in this tutorial is to show you how you can integrate any libraries to Uduino. For this purpose, we show an example to connect a rotary encoder directly to Unity. Encoders use advanced Arduino features (interrupts) and are easily controllable with external libraries.

This example can be found in the Example folder of the assets under the name Encoder Library.

Before starting

Each library works differently. In each case, you have to think about what is the best way to communicate between Unity and Arduino, to optimize your code. Sometimes you'll need to trigger events (so add custom commands) and other times you'll need to simply read a value. In our case, there are two visions to perform the reading task.
Each time the encoder is turned, change in real-time an object on Unity.

  1. On Arduino, we have to always loop to have the value and print it on the serial
  2. On Unity, we want to set the board to automatically read the value and trigger an event when the value is changed

At specific moments on Unity, I want to know what is the value of the knob

  1. On Arduino gather the encoder position in the loop and create a command (link) to return the knob value.
  2. On Unity trigger the command with Read() only when you need to.

For the purpose of this example, we are choosing the first way to do it.

Arduino sketch

To get started, we are using the library by Paul Stoffregen that can be installed from Arduino IDE. (Sketch>Include Library > Manage Libraries>Encoder).

First step, we include Uduino and name our board.

Uduino uduino("myEncoder"); 

Then we include the library. In our case, we declare a new encoder, connected to the pins 5 and 6.

#include <Encoder.h>
Encoder myEnc(5, 6);
long oldPosition  = -999;

We read the encoder value following the library example and print it directly in the loop.

void setup()

void loop() {
  uduino.update();  //Mandatory part, to maintain the connection between Arduino and Unity

  long newPosition =;    // This is how...
  if (newPosition != oldPosition) {  // ...the library...
    oldPosition = newPosition;      // ...normally works !
    uduino.println(newPosition); // we print the encoder value when a change happens

Important note on the use of delay() and Serial.print()

TLDR; Remove the Serial.print and delay from your code !

If you use delay() in your sketch, it will pause your sketch for some ms. Hence the connection between Unity and Arduino will not work smoothly; for instance, a message sent will be waiting in your serial buffer.
If you want to use delay, you should use the function uduino.delay(), that works similarly to the Arduino delay function but maintain the Uduino connection open.

Similarly, you should know that every Serial.print() that you use in your loop is susceptible to be read by Unity. hence I recommend to remove unnecessary print and only use uduino.print()when you want to send a variable.

Unity communication

Back on Unity, we want to link the encoder value to a graphical slider. Each time a new value is set, we are going to launch an action animating the slider by adding a custom function as a parameter of the AlwaysRead() method.

    void Start () {
          UduinoManager.Instance.onValueReceived += ValueReceived

    void ValueReceived(string value, string board) {
           slider.value = int.Parse(data);

Example: Uduino\Examples\Advanced\EncoderLibrary