Introduction: Model Train Control on Arduino

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 arduinorailwaycontrol.com

Step 1: Simple Start

Picture of 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

Picture of Upload Sketch to Arduino

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

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

//// GPIO PINS ////

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

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


void setup() {
  
  // Initialize Serial
  Serial.begin(9600);
  inputString.reserve(16);
  
  // Initialize Motor Driver
  pinMode(ENA_PIN, OUTPUT);
  pinMode(IN1_PIN, OUTPUT);
  pinMode(IN2_PIN, OUTPUT);

}

void loop() {

  if (stringComplete) {

    // ----------- START COMMAND PARSING ----------- //
    
    //THROTTLE
    
    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);
      }
    }

    // DIRECTION

    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);
      }
    }

    //TEST

    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)Serial.read();
    inputString += inChar;
      if (inChar == 'z') {
      stringComplete = true;
    }
  }
}

Step 3: Install App ArduinoTrainBtControl on Your Android Device

Picture of 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.

Comments

Steve Massikker (author)2017-09-11

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

kksjunior (author)2017-09-07

Wow! well done..!

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

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

Steve Massikker (author)2017-09-06

I'm very flattered!

Swansong (author)2017-09-06

That's a neat control system :)

About This Instructable

2,777views

18favorites

License:

Bio: Hello. Main name is Steve. I'm designer and former engineer from Moscow.
More by Steve Massikker:Let's Add a Few New Parts to the Diagram Model Train Control on ArduinoSensor for Railway Layout AutomationModel Train Control on Arduino
Add instructable to: