A two-terminus track with a station midway, controlled with Arduino: auto-reverse employing IR proximity sensors.
Not a DCC controller.
This is a fork of DIY Jimmy's (find him at DIY and Digital Railroad) "Three Station Railroad Arduino Demo", however, that project is setup as ArduinoIDE and I prefer vanilla C/C++ or JavaScript with Microsoft VSCode etc. Plus, that code can be refactored for me, a n00b to Arduinios and very rusty with C++. That said, this compiles fine using Arduino IDE 2.x (Arduino Automated Railroad.ino
becomes src/main.cpp
).
Original GitHub Source and his incredibly helpful YouTube tutorial.
Pseudo-code of how I want my main loop to read:
void loop() {
updateSpeed(); // get potentiometer setting and send that to motor controller
reportStatus(); // echo state of the train, direction and speed
if (isAtTerminus()) {
// applies to any terminating station: end of the line
reverseDirection();
} else (isAtMiddleStation()) {
// any station "in the middle" we'll just pause momentarily before
// continuing in same direction of travel
pauseAndResume();
}
}
- my final build uses a ease-in stop at the middle station rather than merely cutting the power, but I wanted to leave this "pure" and of a piece with Jimmy's original example.
- any guage/scale layout: O, HO, N -- code should be scale agnostic
- the motor driver used here can control TWO DC tracks... makes ya think, don't it?
- 3 IR proximity sensors, detect when train "arrives" at a station
- potentiometer, control train speed
- Motor Driver, applying current allows us to electrify either track (forward & reverse), as well as set the speed (using potentiometer's value)
- Arduino Uno & breadboard (this probably fits easily on smaller mini, but I had an Uno handy)
Direction of travel -- "Left" or "Right", "Forward" or "Reverse" -- are completely arbitrary, so pick and move on.
(check the "docs/fritzing" directory for the Wiring Diagram and additional part definition (found on very helpful Fritzing forums)
- solder parts, no jumpers
- mount IR sensors beneath the flextrack, drill small holes in ties for LED & reciever
I'll be using an n-scale Bachmann Trolley (trolleys can be found for as little as $35-$60)
Everything is free except Copilot, which is optional, but helpful (even explains pin-outs)
- Visual Studio Code, with extensions:
- PlatformIO
- Copilot
- Arduino CLI
- Gnu Compiler Collection C++ Compiler
- Fitzring circuit boarding & layout software 😍