The raspberry Pi model railway has brought me hours of fun, both coding it, and using it! This is a supplementary blog post about how I got it all up and running again, I hope you find this useful!

How I re-setup the Raspberry Pi model railway

So I took up a friend’s offer to give their child a introduction to the Raspberry Pi, and so I searched for the model railway kit in the loft and found it, all nicely boxed up. Only thing I didn’t bargain for was that it just had a Raspberry Pi 1 large SD-Card in the pack – and I had bought a RPi 2 and 3 with me that take micro SD-Card’s doh.

So I begun by trying to take an image of the SD-Card which I was going to transfer to the micro SD-Card – luckily I’d bought my previous generation MacBookPro – which had an SD Card reader! Details on how to do this are here; http://kevswebuk.blogspot.co.uk/2013/09/cloning-raspberry-pi-sd-card-on-mac.html however this took a long time…

The step-by-step guide

So I decided to see if I could get it running a fresh from scratch! Here’s a quick guided tutorial!:

  1. I put the railway out, the train, and connected the Hornby controller (making sure to connect A to A and B to B)
  2. Then I turned on the controller and tested that the whole thing worked
  3. Next, I created the the traincontroller.py file from the origin guide here: http://www.kevingordon.org.uk/articles/raspberry-pi-railway-web-controlled.html.
  4. I had an error as there ‘impo’ from ‘import’ was missing, so I thought there ‘serial’ package was missing. If this had been the case sudo npm install python-serial would have sorted it.
  5. Then I downloaded the lights-test.py, and checked it was working, unfortunately there was a comms error over the USB, and it wasn’t that the USB wasn’t plugged in.
  6. I changed to the /dev directory and typed ‘ls’ and it gave a list of devices, I unplugged the USB controller, and did ‘ls’ again and it gave the same list with one thing removed. A quick looked showed me that the controller device name had changed from ttyUSB1 to something different, a search and replace in the traincontroller.py sorted that out.
  7. Then I was able to successfully run the lights-test.py, and get the lights to go on and off!
  8. Then I wrote two simple scripts, one to start the train and one to stop the controller:

    #start.py
    import traincontroller
    import time
    startDirection = traincontroller.FORWARD
    traincontroller.increaseSpeedFromToDirection(0, 75, startDirection)


    #stop.py
    import traincontroller
    import time
    startDirection = traincontroller.FORWARD
    traincontroller.decreaseSpeedFromToDirection(75, 0, startDirection)
  9. By running the python e.g. python start.py the train will start, and use the other script to stop it!

The Raspberry Pi Railway game

Fabulously we had therefore create a game! Which meant I would start the train, and once it was up to speed, someone would shout ‘STOP!’ and I would run the stop script at that instance. This would then bring the train to a stop. The game being to get the train to stop at the station. This proved to be quite a lot of fun and very competitive.

Conclusion

I hope this supplementary guide will give you a bit of additional help in getting the Raspberry Pi model railway demo up and running, if you haven’t done so already!