Project showcase

Arduino Train Demo 2 © CC BY

Simple and faster solution for start control your model train via mobile app. Updated in April 2019 to 9 speeds and 122Hz PWM!

  • 5,978 views
  • 3 comments
  • 24 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 9

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

void setup() {

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

// Initializing Motor-Driver
  pinMode(ENA_PIN, OUTPUT); 
  pinMode(IN1_PIN, OUTPUT); 
  pinMode(IN2_PIN, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  
// Set PWM frequency for D9 & D10
// Timer 1 divisor to 256 for PWM frequency of 122.55 Hz
  TCCR1B = TCCR1B & B11111000 | B00000100;    

}

void loop() {

  if (stringComplete) {

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

      //THROTTLE 
      // Speed 
      if (inputString.charAt(1) =='0') {
        if (inputString.charAt(2) =='0') analogWrite(ENA_PIN, 0);
        if (inputString.charAt(2) =='2') analogWrite(ENA_PIN, 60);
        if (inputString.charAt(2) =='4') analogWrite(ENA_PIN, 80);
        if (inputString.charAt(2) =='6') analogWrite(ENA_PIN, 100);
        if (inputString.charAt(2) =='8') analogWrite(ENA_PIN, 120);
      } 
      if (inputString.charAt(1) =='1') {
        if (inputString.charAt(2) =='0') analogWrite(ENA_PIN, 140);
        if (inputString.charAt(2) =='2') analogWrite(ENA_PIN, 170);
        if (inputString.charAt(2) =='4') analogWrite(ENA_PIN, 200);
        if (inputString.charAt(2) =='6') analogWrite(ENA_PIN, 230);
        if (inputString.charAt(2) =='8') 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 demo 2 vxiha81fsz

Comments

Similar projects you might like

Using IR & Hall Type Sensors for Train Detection

Project showcase by Steve Massikker

  • 3,586 views
  • 4 comments
  • 24 respects

Arduino Train for Kids 2

Project showcase by Steve Massikker

  • 9,329 views
  • 6 comments
  • 42 respects

The Train of the Future

Project showcase by Team STT

  • 14,074 views
  • 5 comments
  • 19 respects

Quadrature Encoder Demo

Project tutorial by Shawn hymel

  • 4,188 views
  • 3 comments
  • 12 respects

Arduino Bluetooth Boat

Project tutorial by CrazySuspct

  • 2,286 views
  • 3 comments
  • 20 respects

Alexa Controlled Door Sign Demo

Project tutorial by 3magku

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