Project showcase

Arduino Train Demo © CC BY

Simple and faster solution for start control your model train via mobile app. Only Arduino details.

  • 2,966 views
  • 0 comments
  • 15 respects

Components and supplies

Ard nano
Arduino Nano R3
×1
09670 01
SparkFun Dual H-Bridge motor drivers L298
Or any other Motor-Driver based on chip L298N
×1
Bluetooth Low Energy (BLE) Module (Generic)
I recommended use HC-06 Bluetooth Module
×1

Apps and online services

About this project

This is an introduction to the big URB project to fully control your railway models. The mission of the project is to give you the pleasure of controlling your world in an understandable and convenient way using only the Arduino platform, without computers.

The Arduino Train DEMO is fully compatible with other versions of Arduino Train Apps, and you can use it even to control this layout, as in the figure above.

The minimum Arduino equipment necessary for this experiment consists of only four components shown in the figure. And also you need 12V Power Supply with current near 0.5A or more.

Code

Sketch for Control unitArduino
// L298
#define IN1_PIN 4
#define IN2_PIN 5
#define ENA_PIN 6

// VARIABLES //
bool flag_LED = false;
bool stringComplete = false;
String inputString = ""; 

void setup() {

// Initialize Serial
  Serial.begin(9600);
  inputString.reserve(4); 

// Initialize Motor Driver
  pinMode(ENA_PIN, OUTPUT); 
  pinMode(IN1_PIN, OUTPUT); 
  pinMode(IN2_PIN, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);

}

void loop() {

  if (stringComplete) {

    if (inputString.charAt(0) =='a') {

      //THROTTLE 
      if (inputString.charAt(1) =='0') {
        if (inputString.charAt(2) =='0') {
          analogWrite(ENA_PIN, 0);
        }
        if (inputString.charAt(2) =='2') {
          analogWrite(ENA_PIN, 80);
        }
        if (inputString.charAt(2) =='4') {
          analogWrite(ENA_PIN, 100);  // SET PWM LEVEL FOR STARTING YOUR LOCO
        }
        if (inputString.charAt(2) =='6') {
          analogWrite(ENA_PIN, 150);
        }
        if (inputString.charAt(2) =='8') {
          analogWrite(ENA_PIN, 200);
        }
      }
      if (inputString.charAt(1) =='1') {
        if (inputString.charAt(2) =='0') {
          analogWrite(ENA_PIN, 255);
        }
      }

      // DIRECTION
      if (inputString.charAt(1) =='d') {
        if (inputString.charAt(2) =='f') { // (f) Forward
          digitalWrite(IN1_PIN, HIGH);
          digitalWrite(IN2_PIN, LOW);
        }
        if (inputString.charAt(2) =='b') { // (b) Backward
          digitalWrite(IN1_PIN, LOW);
          digitalWrite(IN2_PIN, HIGH);
        }
        if (inputString.charAt(2) =='s') { // (s) Stop button
          digitalWrite(IN1_PIN, LOW);
          digitalWrite(IN2_PIN, LOW);
          analogWrite(ENA_PIN, 0);
          flag_LED = !flag_LED;
        }
      }
    }

    inputString = "";
    stringComplete = false;
  }

  if (flag_LED) digitalWrite(LED_BUILTIN, HIGH);
  else digitalWrite(LED_BUILTIN, LOW);
}

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

Schematics

Connections
Circuit beginners clelpkqcfg

Comments

Similar projects you might like

Using IR & Hall Type Sensors for Train Detection

Project showcase by Steve Massikker

  • 5,720 views
  • 5 comments
  • 29 respects

Arduino Train for Kids 2

Project showcase by Steve Massikker

  • 10,677 views
  • 7 comments
  • 44 respects

The Train of the Future

Project showcase by Team STT

  • 14,743 views
  • 5 comments
  • 21 respects

Quadrature Encoder Demo

Project tutorial by Shawn hymel

  • 4,081 views
  • 3 comments
  • 12 respects

Arduino Bluetooth Boat

Project tutorial by CrazySuspct

  • 2,982 views
  • 4 comments
  • 20 respects

Alexa Controlled Door Sign Demo

Project tutorial by 3magku

  • 1,707 views
  • 0 comments
  • 5 respects
Add projectSign up / Login