Learn

Arduino model railroad projects

Intermediate

Or see every lesson.

  • 261parts on the bench
  • 31boards running now
  • 1.00xreal time, on every board
Uno: a level crossinglive0.000 s 0.00x
Click to open it in the editor
Click the card into the left detector, then into the right one and out again. Watch the lamps and the gate.

An Arduino is one of the cheapest ways to bring a layout to life. A few dollars of servo throws a turnout slowly, like the prototype. A pair of detectors and two red LEDs make a crossing that follows a train. Three detectors and two signals step down from green to yellow to red behind it. The hard part is rarely the parts; it is the logic, and getting it right before the scenery goes in.

The circuit above is a level crossing on an Arduino Uno in Mokxi. Press Run and click the card into the left detector: the lamps start alternating and the bell rings, and three seconds later the gate comes down. Put the card in the right detector and take it out again, and once the train has cleared, the gate rises and everything stops.

Mokxi has no train and no track, so a car in a detector is a card in its slot. Nothing here is DCC. The four templates are the DC and accessory side of the hobby, the part an Arduino is usually doing, and the pages say plainly where DCC does a job differently.

Want the step-by-step version? The lesson "Start the model railroad course" walks through this with checkpoints.

Open the lesson

Servo turnouts with a panel

A hobby servo under the layout, with a stiff wire through the throwbar, is a popular slow-motion turnout motor because it is cheap and the throw is set in code. The template drives two from a panel: a button for each, and a green and a yellow lamp showing the route. The servo moves one degree every 30 ms so the points glide, and the lamp for the new route blinks until they arrive.

Two details make it last. The throw angles are set per turnout so the points just touch the stock rail, because a servo pushed past that hums and heats. And each position is saved in EEPROM, so at power-up the servo is sent where it already is instead of jumping. Typing 1 or 2 in the Serial Monitor throws a turnout the way a computer panel would send a command.

A level crossing that follows a train

The crossing uses two beam detectors, one each side. Whichever a train reaches first starts the warning; the gate waits a few seconds so a car already on the crossing can clear, then lowers slowly. The train trips the far detector as it leaves, and once that detector has been clear for two seconds the gate rises. The detector the train left by is ignored briefly, so the last car does not start it again.

The lamps alternate fifty times a minute each, inside the range U.S. rules set for real crossing lights, and many crossings stop the bell once the gates are down, which a single setting chooses.

Block signals without track current

In automatic block signaling, each signal guards the block in front of it: red when that block is occupied, yellow when the next one is, green when both are clear. Most layouts detect occupancy by current, a detector seeing a locomotive or a resistor wheelset anywhere in the block. Mokxi does not model track current, so the template puts detectors at the block boundaries and remembers which block the train is in.

Each signal is one bicolor LED, the way many modelers build a searchlight head: red, green, or both for yellow.

Each signal looks at its block and the next, from the block signals sketch
if (occupied[s]) setSignal(s, RED);
else if (occupied[s + 1]) setSignal(s, YELLOW);
else setSignal(s, GREEN);

Wiring it under a real layout

Every template draws the Uno below its panel, with each pin’s jumper on its own lane so the runs nest instead of crossing, and that is a habit worth taking under the benchwork. Label both ends of every wire, run the accessory supply and its ground as a bus along the layout, and keep the servo power separate from the board’s own 5 V, sharing only the ground. A servo that starts moving can draw a burst of current that resets a board fed from the same pin, which shows up as turnouts twitching at random.

Test the logic first, then the wiring. In the simulator you can throw a turnout a hundred times, run a train past a crossing from both directions and step a signal through every aspect before a single hole is drilled in the fascia. When something misbehaves on the layout afterward, you already know the code is not the cause.

DC throttle, and where DCC differs

The throttle drives a DC motor through a TB6612 from a 12 V supply with PWM, and adds momentum: the speed eases toward the knob, a brake button slows harder, and the direction only changes at a standstill. With DCC the rails instead carry constant power as a square wave that also carries digital packets, and a decoder in each locomotive does its own PWM and momentum. Mokxi does not simulate DCC.

On a real layout fit a fuse or a current limit: a derailed wheel across both rails is a dead short. The model railroad course walks through all four builds with a check at every step.

Questions

Can an Arduino control model railroad turnouts?

Yes. A hobby servo per turnout, moved slowly by the sketch, is a common way. Power the servos from their own 5 V supply and share its ground with the Arduino.

Does Mokxi simulate DCC?

No. The templates are DC and accessory circuits. The pages explain where DCC differs, such as the decoder in the locomotive doing the PWM.

How does the crossing know the train has passed?

It waits for the far detector to see the train and then stay clear for two seconds, which covers the short gaps between cars.

Why use detectors instead of track current for block signals?

Because Mokxi does not model track current. Real layouts often use current detection; the signal logic is the same either way.

Build this for real

Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.