Documentation > nControl > Getting Started
Controlling Track Switches with Your Own Arduino Based Controller
For nControl™ 2017.4 or higher
This page shows how to link a DIY Arduino based system to nControl and control track switches.
Step 1: Get a nControl Subscription
Using generic controllers with nControl requires a subscription. To verify whether you have an active subscription choose Help > About in the main menu to open the About dialog box and then click the Subscription button. If you don't have a subscription, you can contact us and we gladly provide you a limited time trial subscription to evaluate the generic controller features of nControl! Please provide the system ID shown on the subscription page of the About dialog box as we need this to active your trial subscription.
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. To active switches you need to add code that can accept commands over the serial connection and positions the track switch motor accordingly.
Step 3: Create custom tile code
Now you need to replace the default behavior of the switch tiles so that is sends the appropriate commands to your Arduino system. Let's assume that you programmed your Arduino so that it recognizes the commands 'TURN' and 'STRA' to position the track switch motor. In the settings dialog box of the track switch tile choose your custom Arduino as Controller. Next, check the Use a custom action box and click the Code button create the custom Python code.
In the custom tile code, start by executing the flipSwitch() function to flip the switch in the track layout of nControl. Then check the new state of the switch and send the appropriate command to your Arduino using the sendMessage() function.
self.flipSwitch()
if self.getState() == nConst.ST_STRAIGHT:
   self.sendMessage('STRA')
else:
   self.sendMessage('TURN')