Documentation > nControl > Getting Started
Using Sensors With Your Own Arduino Based Controller
For nControl™ 2020.0 or higher
This page shows how to link a DIY Arduino based system to nControl and process sensor feedback.
Step 1: Get a nControl subscription
Using generic controllers with nControl requires a subscription. If you don't have a subscription, log in into your Control Panel, select the Licenses tile and click on the Start Trial Subscription button. This will give you a free subscription for 30 days.
Step 2: Setting up your Aruidino
When your Arduino boots it needs to provide the information required by nControl to recognize it as a generic controller. Start by initializing the serial communication with a baud rate of 38400.
Serial.begin(38400);
Next, send the text 'My Own Controller' over the serial connection; this string is case-sensitive, so make sure you use the correct case. This informs nControl that this is a generic controller that wants to connect.
Serial.println("My Own Controller");
Then print the number 3 to inform nControl that there are 3 information sets following.
Serial.println("3");
Provide the serial number of your controller. nControl will use this number the identify the controller. This number has to be unique, only one controller with this serial number can be connected.
Serial.println("[Serial]");
Serial.println("ABCD-EFGH");
You can provide a description for the controller. This description will be shown in the Manage Controllers dialog box when connecting to the controller.
Serial.println("[Description]");
Serial.println("Custom Switch Controller");
Finally, you can also provide a version number of the 'firmware' of your custom controller.
Serial.println("[Firmware]");
Serial.println("1");

Combined, this gives the following initialization code for your Arduino:
void setup()
{  // Start the serial communication
   Serial.begin(38400);                            // The default baud rate of nControl is 38400

   // Required information to connect with nControl
   Serial.println("My Own Controller");            // Specifies this is a generic controller
   Serial.println("3");                            // Specifies that there are 3 data sets following
   Serial.println("[Serial]");                     // Specifies that the next line is the serial number
   Serial.println("ABCD-EFGH");                    // Specifies that the serial number
   Serial.println("[Description]");                // Specifies that the next line is the controller description
   Serial.println("Custom Switch Controller");     // Specifies that the description
   Serial.println("[Firmware]");                   // Specifies that the next line is the firmware version
   Serial.println("1");                            // Specifies that the firmware version 
}
This is just the code needed to make sure nControl recognized your Arduino board. To get sensor feedback into nControl, you need to add code that read the analog pin to which the sensor is connected and send this value over the serial bus.
Step 3: Sending senor feedback to nControl
The way your Arduino board read the sensor feedback will depend on the type of sensor you're using. Please refer to the your sensor documentation for that. To get a sensor independent example, we're going to simulate the sensor data with a simple counter. We're going to send the counter value over the USB bus every 50 milliseconds.
void loop()
{  // Simulate a sensor
   value++;
   if (value > 100)
   {  value = 0;
   }
   Serial.println(value);
   
   // Delay
   delay(50);
}
Note that you need to declare value as a global integer variable to make this code work.
Step 4: Processing the sensor feedback in nControl
Now you need to replace the default behavior of the train detection tile so that it processes the sensor feedback from our Arduino board.
  • Select your generic controller in the Device drop down list of the Edit Train Sensor Properties dialog window.
  • Next we have to make sure the tile receives the feedback from the Arduino. For that, we need to enroll the tile for sensor feedback. To do that, click the Code button and select Activate Tile in the Event drop down list. This script will be execute when the tile is activate when we go to Operational Mode. Add the following code:
    self.enroll()
  • The only thing left to do is processing the sensor feedback. To do so, select Serial Data in the Event drop down list and add the following code:
    value = int(self.getSerialData())
    self.setValue(value)
    This event script will be executed each time the tile receives data from the Arduino board. The function .getSerialData() provides the message the board received. In our case, that the counter of the Arduino board. We convert it, it's a string, to an integer value and then set it a new value for the sensor with the .setValue() function.
The sensor tile will now receive the feedback from your custom sensor / controller and process it, i.e. it will show it in the feedback bar of the tile and the tile will trigger when the value exceeds the threshold.
Step 5: Using multiple sensors on the same controller
If you want to connect multiple sensors to your controller you will have to add an identifier so you can trace back which value comes from which sensor. You can, for example, put a letter in front of the value sent by the Arduino board: A = sensor 1, B = sensor 2, etc. You have to enroll all your sensor tiles to the same controller, so each tile will received the feedback from each sensor. In the Serial Data event script you have to add code to only store the value in the tile if it comes from the corresponding sensor and ignore the data from all other sensors.