Pico-Controlled Automated Model Railroad

By Rosemary Hattersley. Posted

Who doesn’t love a model railway? Mechatronics student Kushagra Keshari is a big fan, and often combines his love of locomotion with his interest in electronics. Having spotted a YouTube video introducing Raspberry Pi Pico, he decided it was high time he investigated the microcontroller and explored how it might be incorporated into his model railway setup.

The Pico-Controlled Automated Model Railroad builds on his ‘sensored’ track concept that detects a train passing and instructs it to speed up, slow down, or come to a halt. “Electric motors are among my most favourite devices because they connect the fields of electronics and mechanics,” he reveals. “I try to add motors wherever possible.” 

As someone with experience of Arduino, Kushagra believes Pico has great crossover appeal, lauding its ease of use and functionality: “One of the best reasons to [use Pico] is the availability of libraries to allow the integration of modules, sensors, and other complex stuff without having to write the entire code from scratch,” he explains in his guide to programming Raspberry Pi Pico with the Arduino IDE.

The project joins Kushagra’s sizable collection of Instructables focused on using microcontrollers and stepper motors to augment a model railway layout. His projects work well together, with this first foray into Pico territory building on Kushagra’s existing ideas for using Arduino to add smart features to his train tracks. “Raspberry Pi Pico helped make it more budget-friendly and enabled several options for expansion in the future,” he says. 

Connect the wires of the track power block to the outputs of the motor driver, then connect the female DC jack positive wire to the +12 V terminal. Connect a male jumper and the DC jack’s negative wire to the GND (centre) terminal, and a jumper wire to the +5 V (right) terminal of the motor driver.

Automating a model railroad with Raspberry Pi

Kushagra’s approach to automating a model railway starts with a basic oval track layout, Raspberry Pi Pico, and an L298N motor driver, which he favours because of its +/- 5 V on-board voltage regulator, which can be used to supply power to any attached microcontroller and other modules. “The voltage level is quite important as a lot of sensors and modules use 3.3 volts for communication, and having a 3.3-volt microcontroller eliminates the need of using voltage level shifters and optocouplers in the circuit,” he explains. 

Raspberry Pi Pico is programmed using the Arduino IDE and becomes the brains of the model railway, using sensors to instruct each locomotive that passes over the smart track what to do next. As the layout grows, additional sensors can be added and used to alter the locomotive’s speed or get it to stop for a preset time, as if it were at a red signal or at a station. Kushagra says, “I am planning to add yard shunting operation which will allow the locomotive to couple, and decouple, with the rolling stock to allow it to pick them up from one point and drop them off at another.”

Each time the train passes the sensor on the track, its speed is adjusted by the Pico program

Setup Pico to control a model railroad

The setup instructions given on his Instructable are admirably clear, but Kushagra also provides troubleshooting tips for this and his other projects, such as what to do if the train unexpectedly starts moving in the wrong direction (switch the polarity of the track’s power). For example, the sensor’s LED should only light up when a train passes over the sensor-enabled track. If, however, the LED on the sensor is always on, you should adjust the sensitivity of the potentiometer.

Although there are lots of other Raspberry Pi- and Arduino-controlled layouts out there, we really like Kushagra’s low-cost approach to automating his model railway. His modular approach makes it clear enough for newbie makers to take on, while also being ideal for established railway modellers to use as a template to retrofit their layout. “Given enough time and effort, you could automate switching operations, automate the operation of multiple trains at the same time, control the lighting, or use WiFi or Bluetooth to control your trains and turnouts and a lot more,” Kushagra enthuses. 

There’s plenty of potential in the ‘sensored’ track approach, too: anyone up for combining the track sensor with a station announcement board to indicate an approaching service (or delay)?  

From The MagPi store


Subscribe to the newsletter

Get every issue delivered directly to your inbox and keep up to date with the latest news, offers, events, and more.