Christmas Project (Internet controlled train set + lights)

Recently I engaged in a conversation about a website I used to enjoy alot during the mid 2000s, unfortunately now dead. It consisted of a webcam in some guy's basement and a whole load of electronic toys you could play with remotely including a model train, an RC mini and a crane. I remember hours of enjoyment using the site especially when I managed to pick up the car with the crane and lower it onto the railway track and ram it with the train.

It suddenly occured to me that I could re-live the madness in my own home and decided to trial it over the Christmas period from 1st December until 31st December 2017. So far I have dug out my old childhood train set and started building an internet connected controller to control the train, unfortunately budget probably won't stretch to a crane as well although I'll probably put up a Christmas tree within the circle of train track with some internet controlled lights to keep with the festive theme.

The train is a simple affair with a variable DC current passed through the track between 0 and 12v, initially I thought about cannibalising an existing model railway controller but I soon realised controlling the motor using the PWM output of an Arduino ATMega328PU, a Darlington pair and a H Bridge would probably achieve a decent serial controlled controller at a very low cost, and would be more favourable than buying a model railway controller for conversation as even a simple analog model railway controller retails for around £40.

I sourced a few parts from my electronics stuff drawer and ordered some other items from eBay, first I found a decent 20V AC powersupply capable of deliverying 1A with thermal cut out and some nice protection features in my storage box. Although more work than just getting a straight forward 12V DC powersupply I decided this was worth the effort rather than buying some cheap substandard supply from eBay. I hooked this up with some diodes forming a full wave bridge rectifier along with a smoothing capacitor and an L7812CV voltage regulator to give a nice smooth 12V supply to work with.

Next as the h-bridge ICs have not arrived from eBay yet I wanted to check I could use PWM succesfully to vary the voltage to the track using some transistor, of course with this configuration I would not be able to change direction, only vary speed. Based on the use case I chose a TIP120 Darlington pair on the ground return from the track. Due to the lack of h-bridges I only used a breadboard at this stage to test the circuit and I kept the ATMega328PU in an Arduino Uno boardpowered over the PC's USB for simplicity and ease of changing stuff around, although for the final project the micro controller will be broken out of the Uno and used directly within the circuit using an L7805CV 5V regulator for power along with a 16mhz crystal and a few 10pF capacitors.

Here is a very back of napkin style sketch of my setup for testing...

I constructed the above on a breadboard adding a heatsink to the 12V regulator and the TIP120 as I expect these will get pretty warm, then I got to work writing some basic Arduino code to allow setting the PWM duty cycle using serial input. Here is the result:

int mspeedpin = 9;
int mspeed = 0;
int integerValue;

void setup() {
  pinMode(mspeedpin, OUTPUT);

void loop() {
  integerValue = Serial.parseInt();
  if (integerValue > 150) {
    mspeed = integerValue;
    Serial.println("Set new speed");
  } else if (integerValue == 1) {
    mspeed = 0;
    Serial.println("Train stopped");
  } else {
    Serial.println("Invalid command / speed");
  analogWrite(mspeedpin, mspeed);

By default the serial gets parsed as 0 if no input is given so I simply ignore items with a 0 input and instead set an input of 1 to set the duty cycle to 0% to stop the tain as an input of 1 would not usually be used as the train only really starts moving at around 60% duty cycle. Below this amount the voltage is too low to get any movement hence adding the if statement ensuring that only values above 150 set the speed, anything below this would risk causing damage to the motor. The serial console in Arduino will now accept the following values:

  • 0 = No input, ignore and continue
  • 1 = Stop the train
  • 150 - 255 = Vary the speed of the train between ~58% - 100%

Then I put a train on the track and tried sending a few commands via serial, to my joy it ran around the track with varying speed, when the h-bridge arrives I'll fit it into the circuit to get bi-directional control working before moving on to some basic short circuit protection in case the train falls off the track and causes a short. Following this I'll get it all mounted on some prototyping board and build an API for interacting with the controller over the internet.

Anyway here is a short video of testing the above, you'll notice the train's speed doesnt vary greatly, unfortunately this model is very old and is a bit all or nothing, I may try and dig out a newer model from my collection as I suspect these would handle the lower voltages better. On the first lap it runs at around 60% duty cycle increasing by 10% on each subsequent lap until full speed and then ramping back down again, the stuttering over on the left hand side is caused by some dirt on the track and is not related to the controller.

I will update once the h-bridge arrives and the project is progressing, closer to the go live date I'll link to the control page so you can all have a play.

By @Robert Putt in
Tags : #internet, #iot, #electronics, #technology,