Project showcase

Arduino Train Demo © CC BY

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

  • 2,113 views
  • 0 comments
  • 11 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

Arduino Bluetooth Basic Tutorial

by Mayoogh Girish

  • 454,633 views
  • 42 comments
  • 236 respects

Home Automation Using Raspberry Pi 2 And Windows 10 IoT

Project tutorial by Anurag S. Vasanwala

  • 285,473 views
  • 95 comments
  • 671 respects

Security Access Using RFID Reader

by Aritro Mukherjee

  • 229,311 views
  • 38 comments
  • 236 respects

OpenCat

Project in progress by Team Petoi

  • 195,898 views
  • 154 comments
  • 1,361 respects
Add projectSign up / Login