Sean Dutton
Published © CC BY

Arduino railroad

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

BeginnerShowcase (no instructions)10,839
Arduino railroad

Things used in this project

Hardware components

Arduino UNO
Arduino 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
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
tons if you have a long track
×10
Resistor 10k ohm
Resistor 10k ohm
You need one for each endstop
×2

Story

Read more

Schematics

Pic of the wiring

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.

Code

Loop logic

C/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);

}

Credits

Sean Dutton

Sean Dutton

1 project • 3 followers

Comments