Project showcase
Arduino railroad

Arduino railroad © CC BY

Control your H0 scale train with Arduino and a motor shield!

  • 7,425 views
  • 3 comments
  • 16 respects

Components and supplies

A000066 iso both
Arduino UNO & Genuino UNO
×1
Sainsmart motor shield
I'm using the analog pinouts from the shield as buttons (endstops)
×1
End stops
Can add more depending on your track if you like
×2
12v 1amp Powersupply
Will need a minimum of 12volts and 1amp (1amp so the Arduino comes on). 12volts seems to be the magic number to get my train moving enough. With the max PWM to the shield (of 255) it seems to send about 10.5 volts to the track. This may change depending on your components but you will probably want 10 to 13 volts going to the track to get the train to move decent enough.
×1
12002 04
Breadboard (generic)
×1
11026 02
Jumper wires (generic)
tons if you have a long track
×10
Mfr 25frf52 10k sml
Resistor 10k ohm
You need one for each endstop
×2

About this project

Wanted a simple solution to make the train go back and forth on a single short track. Of course, this could be expanded to do some more sophisticated routes.

See it in action here

Custom stops can be found on thingiverse (http://www.thingiverse.com/thing:1187007).

Code

Loop logicC/C++
Basic logic depending on the shield you're using.
//....
// Include any motor/additional libraries you want here
// I basically just plugged in the below to my Sainsmart L293D shield code
//....
//....

// Button setup
int buttonOnePin = 14;
int lastButtonOneRead = 0;
int buttonOneRead = 0;
int buttonTwoPin = 16;
int lastButtonTwoRead = 0;
int buttonTwoRead = 0;

int maxSpeed = 255; // define the max speed of the train - 255 is the highest value we can put here as it is using PWM frequency...voltage out will change depending on the power supply you're using

bool runFlag = true;

void setup()
{
  Serial.begin(9600);
  Serial.println("Railroad Switch start");

  Serial.print("ButtonOne value: ");
  buttonOneRead = digitalRead(buttonOnePin);
  Serial.println(buttonOneRead);

  Serial.print("ButtonTwo value: ");
  buttonTwoRead = digitalRead(buttonTwoPin);
  Serial.println(buttonTwoRead);

  if (buttonOneRead == 0 && buttonTwoRead == 0) {
    motor(1, FORWARD, maxSpeed);
  }
}

void loop()
{
  
  if (runFlag) { // might use this later
    Serial.print("ButtonOne value: ");
    buttonOneRead = digitalRead(buttonOnePin);
    Serial.println(buttonOneRead);
    
    Serial.print("ButtonTwo value: ");
    buttonTwoRead = digitalRead(buttonTwoPin);
    Serial.println(buttonTwoRead);
  
    if (buttonOneRead != lastButtonOneRead) { // button one changed
      Serial.print("ButtonOne value changed!");
      if (buttonOneRead == 1 && lastButtonOneRead == 0) { // it was pressed...otherwise, we don't care
        Serial.println(" // It was pressed!");
        Serial.println("***Going forwards");
        motor(1, FORWARD, 0);
        motor(1, FORWARD, maxSpeed);
      } else {
        Serial.println(" // it was let go...so I don't care.");
      }
    }
  
    if (buttonTwoRead != lastButtonTwoRead) { // button two changed
      Serial.print("ButtonTwo value changed!");
      if (buttonTwoRead == 1 && lastButtonTwoRead == 0) { // it was pressed...otherwise, we don't care
        Serial.println(" // It was pressed!");
        Serial.println("***Going backwards");
        motor(1, BACKWARD, 0);
        motor(1, BACKWARD, maxSpeed);
      } else {
        Serial.println(" // it was let go...so I don't care.");
      }
    }
  }
  
  lastButtonOneRead = buttonOneRead;
  lastButtonTwoRead = buttonTwoRead;
  delay(200);

}

Schematics

Pic of the wiring
04
Fritzing Schematic
Image is using the sparkfun board but i'm actually using the sainsmart L293D motor shield...wiring is pretty darn similar if not the same though.
Schematics bb

Comments

Similar projects you might like

Railroad Crossing Lights

Project showcase by eldo85

  • 3,031 views
  • 4 comments
  • 7 respects

Simple Programmable Robotic Arm

Project showcase by Ryan Chan

  • 49,017 views
  • 63 comments
  • 145 respects

Motor controlled with Arduino

by Benjamin Larralde

  • 18,249 views
  • 20 comments
  • 48 respects

Food Detector

Project tutorial by crispylel

  • 35,426 views
  • 7 comments
  • 91 respects

Room temperature sensor with display on Mobile

Project showcase by Kaustubh Agarwal

  • 23,926 views
  • 8 comments
  • 54 respects

Temperature-controlled RGB LED

by Benjamin Larralde

  • 19,548 views
  • 7 comments
  • 30 respects
Add projectSign up / Login