Model railroad electronics

Servo turnouts, a level crossing, block signals and a DC throttle on an Arduino Uno, and what each one models.

Mokxi has four templates for model railroaders, and the Model railroad electronics course walks through them in order. They are the projects people build with an Arduino on their layouts, wired the way a layout's control panel would be, with the Uno below the panel.

What is simulated, and what is not

There is no train and no track. The detectors are slotted infrared beam sensors, the break-beam kind that mounts across the track, and a car in the beam is the card clicked into the sensor's slot. Nothing here is DCC. The throttle is DC with PWM; the course explains where DCC does the same job differently.

Servo turnouts with a panel

Two servos on pins 9 and 10, a panel button for each on pins 5 and 2, and a green (through) and yellow (diverging) lamp for each. The servo moves one degree every 30 ms, so the points glide; the lamp for the new route blinks while they travel. Positions are kept in EEPROM, so nothing jumps at power-up, and typing 1 or 2 in the Serial Monitor throws a turnout the way a computer panel would.

Set CLOSED_ANGLE and THROWN_ANGLE on the layout so the points just touch the stock rail. Past that the servo pushes forever and hums. Power servos from their own 5 V supply with its ground shared with the Uno's, as the template does.

Level crossing

Two beam detectors (west on pin 3, east on pin 2), two red lamps that alternate fifty times a minute each, a piezo bell on pin 8 and a gate servo on pin 9. A train at either detector starts the lamps and bell at once; the gate waits three seconds, then lowers slowly. When the far detector has been clear for two seconds the gate rises, and the lamps and bell stop.

Block signals

Three detectors at the block boundaries (pins 8, 7 and 6) and two signals, each one bicolor LED as many modelers build a searchlight head: red, green, or both for yellow. Each signal shows red if its block is occupied, yellow if the next one is, and green if both are clear.

Real layouts usually detect occupancy by current: a detector sees a locomotive or a resistor wheelset drawing current anywhere in the block. Mokxi does not model track current, so the sketch counts a block occupied from its entrance detector until the whole train has passed the next one.

DC throttle with momentum

A knob on A0, a direction switch on pin 2 and a brake button on pin 3 drive a DC motor through a TB6612 on a 12 V supply. The speed eases toward the knob's setting, the brake slows four times faster, and the direction only changes at a standstill.

About DCC. With DCC the rails 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.

Safety. Fit a fuse or a current limit on a real layout: a derailed wheel across both rails is a dead short.

Where to go next

Open the templates from /templates, take the course from /learn, or read the guide at /learn/arduino-model-railroad.