Growing up I had an awesome model train set. After some trade show my dad bought us a 3 rail O gauge Lionel train set that came with enough track for a figure 8. It was fairly basic, but I fell in love with it quickly. I was about 7-8 at that time, and over the next couple of years my dad started to buy me more tracks, accessories, and started buying a line of New Haven passenger cars. Then one Christmas he surprised me by buying a matching New Haven engine, a MTH EP-5. Unfortunately I didn’t use it as much as I should have because it had an on board computer dubbed “Protosound” that was basically a micro controller that would control the train’s motors, couplers, and speaker. You could activate the features by pressing a series of buttons on the controller to form a command. Most of the time this was a pain in the butt and my 10-11 year old mind got frustrated so the entire set got boxed and forgotten.

Fast forward a decade or so and I decided that I’m 22 and I should be able to figure it out. I replaced the battery in the engine, but that only helped a bit. Most of the features were still a complicated mess to try to activate. I could get it going back and forth, but that’s about it. When I took the top off to make sure everything was still connected, I got an /r/perfectfit moment of clarity and tried sliding a Raspberry Pi into the case. It didn’t fit, but it would if I took off the composite socket and HDMI port. And audio port. And MicroUSB port. And made the SD card smaller. Okay so it was a bit of a lost cause until I remembered that the Raspberry Pi Model A+ was just released. I did some measurements, and sure enough it was just small enough to fit in.


There were a few things I wanted to accomplish with this project that I have done either very poorly or not at all for past projects:

  • Create a detailed write up with photos along the way. That’s this post by the way. The project is not complete but I will be updating this post as I go along.
  • Work with and understand AC voltage a bit better.
  • Keep the stock body and wiring of the engine intact. Model railroading is not an inexpensive hobby. This engine cost a bit of money and I wanted to be able to drop the old board back in if I wanted to.
  • Write the software in NodeJS. I’ve been using JavaScript a lot more at work and I thought this would be the perfect project to dive into Node.
  • Control the train through another Raspberry Pi “station”. In theory the station should be directing the trains. If I were to expand this, I would not want to hop connections from train to train.
  • Utilize sockets for Pi to Pi communication. I should be able to accomplish this with NodeJS. Sockets are another one of those things that I have neglected to mess with. When they are implemented I should have more control and better performance in network communication.
  • Controllable via any device with a web browser. This means a responsive front end for the controller.

First Hurdle – Power

I had another issue. How do I power this train? I could run the Pi off a battery, but that wouldn’t last very long. How would I even control the motors? I knew the track was powered by an AC transformer. It’s a big brick that outputs 21VAC @ 3.75A. It needs to have enough power to drive the train at the same speed on a large track, potentially with other engines. The tracks are already powered. Why am I involving batteries when I could just power everything off the power from the tracks?

Easier said than done? Actually, no. It’s pretty easy. I just had to tackle it in three parts. I needed to convert the track power to DC, drop that down to 5VDC for the Pi, and somehow drive the motors. Converting the track’s AC power to DC was fairly easy. All I needed was a full bridge rectifier and a large capacitor to smooth out the voltage a bit. I chose a 4700uf 50V capacitor because I knew it might power the Pi for a second or two before draining. This would prove useful as when the train runs along the tracks, there can be some intermittent power failures. I tried it out and sure enough I get about two seconds of power. More than enough.

Initial power test. It works!Initial power test. It works!

The DC power I got out of the rectifier / capacitor tag team floated around 30V with the cap fully charged. At first I hooked up the infamous 7805 regulator (used on many hobby projects and old electronics. The Super Nintendo used one!) but they are super inefficient and basically take the extra 25V and convert it to heat. 25V doesn’t sound like a lot, but trust me it’s hot enough to burn you. I’ve got the scars to prove it. All attempts at putting a heatsink on it failed. I decided to go the switching power supply route and got a nice DC to DC buck converter off Amazon for around $7. Boom, problem solved.

Driving the motors was probably the largest issue. I personally know nothing about AC electricity and really felt I was digging into some complicated electronics. Luckily after poking around with a multimeter I found out the motors in the engine are actually DC motors. After some tips from people on Reddit I decided to use a L298N motor controller. Again I found a nice prebuilt solution on Amazon for less than $10. I hooked it all up to the Pi and toggled the correct GPIO pin and the train took off.

Crash debugging

No, not software crashes. The motor controller is a simple H-bridge which means you basically give it 01 to go forward, 10 to go reverse, and 00 for neutral. It’s binary and there’s no inbetween. This makes the train run too fast and it crashes (literally) by flying off the tracks. After reading some forum posts I learned that you can use PWM on the controller’s enable pin to control the speed of the motor. I did some quick wiring and used the WiringPi’s GPIO utility to test this theory out. Sure enough, it works perfectly. One more obstacle down!

Here’s a video I did a while back of the train being controlled via SSH. I like to call this the Frankenstein stage

Fitting the hardware together

After I wired everything up and the electronics seemed to work properly, I decided to close up the engine. I ran into a couple problems. First, the audio and HDMI connector on the A+ stuck out just a little too far. I fixed this by desoldering both connectors. I added some male jumper pins in place of the audio connector as I would like to utilize the DAC at some point. I also had to sand down part of the A+’s circuit board to make it fit inside the lips of the chassis a bit better. I did this on the side where all the GPIO pins are.

Initial fitting of all of the components. From left to right: Raspberry Pi model A+, LN298 motor controller, custom AC->DC conversion board with smoothing cap.Initial fitting of all of the components. From left to right: Raspberry Pi model A+, LN298 motor controller, custom AC->DC conversion board with smoothing cap.

I also realized that the terminal blocks on the motor controller made me route the wires in a weird way and just created more cable mess. I noticed that the spacing on the terminal pins was fairly standard, so I desoldered them and soldered some  male header pins to the perf board where I was doing the AC/DC conversion. With the holes cleared on the motor controller, the controller slid right onto the pins and ended up saving me about half an inch on the electronics. This allowed me to put the motor controller and AC/DC converter right next to the Pi without any stacking or overlapping.

Where to go from here

Unfortunately I started this project back in January and haven’t dedicated much time to it since then. I got a basic web interface running that was basically a front end for the many commands over SSH I had to manually type in to set the speed and direction. I would like to rewrite what I have into an Angular app with a NodeJS application running on the train itself to control the components. Some of the basic code is there, but I have to spend some more time learning Angular and Node to be more comfortable with it. Right now the train is all closed up and actually works really well. The only complaint I have is if there is a dead spot in the track (and in my current layout there’s a pretty big one) the Pi will shut off in a couple of seconds. My current plan is to put a battery on board that is charged as the train goes around the track, and then to power the Pi when power is lost or removed so I can take the engine off the track without waiting 30+ seconds for the OS to load up. So until the, enjoy the latest video I have of the engine and a very basic web interface!