Skip to content

Custom text protocol

Firmata is the easiest way to start, but sometimes you want your own sketch: to read a sensor library, do filtering on the board, or use a microcontroller that can’t run Firmata. The custom text transport handles that. Your sketch prints simple text lines, and Serial Input Bridge turns them into Input System controls.

Each line is a list of name:value pairs separated by ;, and ends with a newline:

D2:1;D3:0;A0:512;A1:1023;
Part Rule
Digital input D<pin>:<0 or 1>, for example D2:1
Analog input A<channel>:<raw value>, for example A0:512. Unity divides by the profile’s Analog Max Value (1023 by default), so 512 becomes about 0.5.
Separator ; between pairs. A trailing ; is fine. Spaces around names and values are ignored.
End of line \n. Use Serial.println().
Baud rate Must match the profile’s Baud Rate (115200 for generated profiles).

You don’t need to send every pin on every line. Each pair updates only that control, so you can send A0:300; alone.

The profile’s Digital Active Low setting is on by default. With it on, 0 means pressed. That matches the usual wiring: a button between the pin and GND, with INPUT_PULLUP.

If your sketch sends 1 for pressed (for example a button wired to 5V with a pull-down resistor), turn Digital Active Low off in the profile.

If a line contains a name the profile doesn’t know, for example D7 when D7 isn’t an input in the profile, Unity logs a warning once for that name and ignores it. Pairs that aren’t in name:value form, or values that aren’t numbers, are skipped without a warning.

The package can write a matching sketch and profile for you:

  1. Open Tools > Serial Input Bridge > Advanced > Generate Custom Sketch + Profile.
  2. Fill in:
    • Device Name, for example MyController
    • Digital Inputs: how many buttons. They use pins from D2 upward (D0 and D1 are the USB serial link).
    • Analog Inputs: how many knobs or sensors. They use A0 upward.
  3. Click Generate and Save.

Under Assets/SerialInputBridge/GeneratedDevices/<DeviceName>/ you get:

File What it is
<DeviceName>.ino The real sketch. Open it in the Arduino IDE and upload it.
Tester/<DeviceName>Mock/<DeviceName>Mock.ino A test sketch that sends random values once a second, so you can try Unity with no wiring.
<DeviceName>Profile.asset The matching profile, set to the custom text transport.

Then generate Input Actions from the profile with Tools > Serial Input Bridge > Advanced > Input Actions Wizard, and assign the profile to a SerialManager as usual.

The generated sketch is a good starting point. A minimal version with one button and one potentiometer:

void setup() {
pinMode(2, INPUT_PULLUP); // button between D2 and GND
Serial.begin(115200); // must match the profile's Baud Rate
}
void loop() {
String line = "";
line += "D2:" + String(digitalRead(2)) + ";";
line += "A0:" + String(analogRead(A0)) + ";";
Serial.println(line);
delay(10);
}

Your profile needs D2 in its Digital Pins list and channel 0 in its Analog Channels list, with Transport set to Custom Text.

Any board that can print to a serial port works: ESP32, Raspberry Pi Pico, Teensy and others. Set the profile’s Analog Max Value to the board’s ADC range, for example 4095 for a 12-bit ADC like the ESP32’s, so values still run from 0 to 1 in Unity.

  • Open Tools > Serial Input Bridge > Input Monitor to see the raw lines arriving and the resulting control values.
  • On the SerialManager, turn on Log Incoming Lines to print each line to the Console.
  • Seeing garbled text? The baud rate in the sketch and the profile don’t match.