Model Train Control on Arduino





Introduction: Model Train Control on Arduino

About: Hello. Main name is Steve. I'm designer and former engineer from Moscow.

This instruction illustrate principal create railway classical model layout control unit. A communicate through bluetooth between Arduino and Android (or computer, or minicomputer) allows you to switch junctions, run trains.

More complicated project for railway layout control see on

Step 1: Simple Start

An example in this figure is a typical version of robot control based on Arduino. Similar schemes you will easily find on the Internet. The only difference is that the output of the motor driver is connected to the rails, and not directly to the motor. Of course, you can use your combinations of Arduinos (UNO, NANO etc.), bluetooth modules and motor-drivers, as well as ready shields. I recommend the L298N Dual H-Bridge Motor Controller and HC-06 bluetooth modules.

You can directly connect RX to TXD and TX to RXD, but some bluetooth modules sometimes have problems with signal transmission due to different signal levels between Arduino and Bluetooth module. Therefore, there is a resistive divider in the circuit. The nominal value of R1 is 2 kOhm, R2 is 1 kOhm.

Step 2: Upload Sketch to Arduino

Install on your computer Arduino IDE from Open new file and insert code:

// Simple–°mdStation.ino
// 05.02.2017
// Author: Steve Massikker

//// GPIO PINS ////

// L298
#define ENA_PIN 3
#define IN1_PIN 4
#define IN2_PIN 5

//// VARIABLES ////
boolean stringComplete = false;
String inputString = "";

void setup() {
  // Initialize Serial
  // Initialize Motor Driver
  pinMode(ENA_PIN, OUTPUT);
  pinMode(IN1_PIN, OUTPUT);
  pinMode(IN2_PIN, OUTPUT);


void loop() {

  if (stringComplete) {

    // ----------- START COMMAND PARSING ----------- //
    if (inputString.charAt(0) =='t') {
      if (inputString.charAt(1) =='0') {
        analogWrite(ENA_PIN, 0);
      if (inputString.charAt(1) =='1') {
        analogWrite(ENA_PIN, 80);
      if (inputString.charAt(1) =='2') {
        analogWrite(ENA_PIN, 100);
      if (inputString.charAt(1) =='3') {
        analogWrite(ENA_PIN, 150);
      if (inputString.charAt(1) =='4') {
        analogWrite(ENA_PIN, 200);
      if (inputString.charAt(1) =='5') {
        analogWrite(ENA_PIN, 255);


    if (inputString.charAt(0) =='d') {
      if (inputString.charAt(1) =='r') {
        digitalWrite(IN1_PIN, HIGH);
        digitalWrite(IN2_PIN, LOW);
      if (inputString.charAt(1) =='f') {
        digitalWrite(IN1_PIN, LOW);
        digitalWrite(IN2_PIN, HIGH);
      if (inputString.charAt(1) =='s') {
        digitalWrite(IN1_PIN, LOW);
        digitalWrite(IN2_PIN, LOW);
        analogWrite(ENA_PIN, 0);


    if (inputString.charAt(0) =='j') {
      if (inputString.charAt(1) =='a') {
        digitalWrite(LED_BUILTIN, HIGH);
      if (inputString.charAt(1) =='b') {
        digitalWrite(LED_BUILTIN, LOW);

// ----------- END COMMAND PARSING ----------- //

inputString = "";
stringComplete = false;


// ----------- FUNCTIONS ----------- //

void serialEvent() {
  while (Serial.available() ) {
    char inChar = (char);
    inputString += inChar;
      if (inChar == 'z') {
      stringComplete = true;

Step 3: Install App ArduinoTrainBtControl on Your Android Device

Download free software, link Google Play on my site.

Step 4: Connect Via Bluetooth

Connect Android with HC-06.

Step 5: Go Your Train!

In this example, it's hard to make a mistake. But shit happens :) If you have collected everything, have included, but it does not work we will look for the reason. At the same time we will understand how to act in such situations.

First, if the indicate LEDs on all devices do not glow, then most likely you have wrong the polarity or problem with your power adapter. If the power is OK, the program is running on your smartphone, but you can not connect to the Bluetooth channel, the most common mistake is the wrong device selection. Make sure that your choise HC-06 or your BT adapter (see video on page Android Application). The connection is established (in the case of HC-06, the indicator glows, not blinking), but the locomotive does not move over the commands of the smartphone. First we will check our design. If you toggle Switch1 on application, but the Arduino Nano LED does not light up, then problem with transferring data through the serial port from bluetooth to arduino. Try to put the resistors or level converter or... Incorrect wiring again. If all the previous steps are completed, but the locomotive still does not move, then with digital multimeter checking the condition motor-driver's and the locomotive's.



  • Stick It! Contest

    Stick It! Contest
  • Oil Contest

    Oil Contest
  • Casting Contest

    Casting Contest

We have a be nice policy.
Please be positive and constructive.



Hi! Can you control with this device several loks at the time? Like you can do with the dcc command center of thd main brands. If so, what kind of deco do you put in every lok? Thanks!


FYI, the code shows pins 3,2 & 4 but the circuit diagram in step 1 shows 3,4 & 5

1 reply

I built it and it works! (see my post in I made it category). My first attempt failed because I connected Bluetooth RX to Arduino RX and TX to TX. That is how the circuit is wired in the beginner photo on your website ( but on this page you pointed out that RX goes to TX and TX to RX. I swapped the wires and everything worked!

My DC locos whine pretty noticeably. Why is that? My stock MRC power supply doesn't do that and it is based on PWM technology. s it the L298?

1 reply

Hi everyone! I released the update ArduinoTrain BtControl Lite, so the download link has changed.

Thank you! Your comment for me like light at the end of the tunnel :)


Haha...! That's nice...!

That's a neat control system :)