Model Railway Lighting Project

INTRODUCTION

In 2013, a new programme of development of the Model Railway began, with the work being carried out during the closed seasons over-winter. As part of the 2015/16 enhancements, it was decided that the lighting should be improved.

This page includes all the technical information regarding how the system was developed. It is aimed at anyone interested in the technology involved or who wishes to do something similar.

REQUIREMENTS

The fundamental requirement for the lighting is simple; simulate daylight, dim the lighting to simulate night-time and then bring on lights in the trackside houses and streets. However, it turned out that these were not the difficult goals; a bit more work was necessary because the finished system needed to be reliable, easy to use and maintain, and capable of being upgraded readily in the future.

DESIGN

Arrangement

There was a great deal of discussion about how best to realise the various requirements and eventually it was decided that a Raspberry Pi Zero would be utilised to dim and brighten LED 'Day' lights and switch the other lighting on and off at the right times. This allowed for future upgrades and was considered more accessible to the average volunteer than (say) using an Arduino. The wiringpi library was used to allow programming of a Pulse Width Modulation (PWM) signal to dim and brighten a 48 W LED Strip on GPIO pin 18 (the hardware PWM pin) to simulate the 'Day' lights. A MOSFET Driver was used to carry the current needed. The lighting in the houses and elsewhere was switched by four other GPIO pins via a Relay Board.

Development

Software

In order to meet the conflicting requirements of reliability, maintainability, usability and capability, a number of design compromises had to be incorporated. The simplest solution would have involved writing the software in Python using the RPi.GPIO library. Unfortunately, Rpi.GPIO, at the time that this development took place, did not support hardware PWM. An early prototype of the code used software PWM, but this was prone to flickering; not good for a public display where some visitors might be susceptible. The code was therefore rewritten to use the wiringpi2 Python wrapper.

All code development was carried out under the Raspbian Operating System, but it was decided that this would not be robust enough for the 'deployed' system, which would be subject to staff 'shutting down' by simply turning off the power (not to mention possible power cuts). To guard against SD Card corruption, it was therefore decided that the deployed software would run under the piCore Operating System. However, it proved impossible to run the Python Code under piCore because no wiringpi Python wrapper had been ported to the platform. The final compromise was to rewrite the code in C. Once compiled, the executable ran extremely well, with no discernible flickering.

Hardware

Another early design decision was to buy modular hardware items where possible, such as the Relay and MOSFET Driver boards. This is because the components on these are mounted on professionally produced printed circuit boards, thus reducing the amount of soldering needed by the team or by future maintainers or developers. The MOSFET Driver board was extremely cheap, but was a compromise too far because the MOSFET device used on these boards is the IRF520, which is designed to switch between 2 V and 4 V, but does not fully turn on until at least 10 V is applied. The Pi GPIO outputs use 3.3 V logic so the LED Strip was hardly turned on. The solution was to de-solder the IRF520 MOSFET and fit a IRLZ44NPBF logic-level MOSFET, which worked extremely well. (Warning: Desoldering these MOSFETs is not easy, because they soak up the heat. This should only be attempted if a hot, temperature controlled iron and good quality 'solder-sucker' is available.)

KEY RESOURCES USED

The Raspberry Pi

The Raspberry Pi is a small single-board computer that was developed by the Raspberry Pi Foundation to provide a cheap and capable computing resource. It was aimed at education, but has proved extremely popular. Details of the Raspberry Pi and the resources available for it may be found at The Raspberry Pi's Website.

Raspbian

Raspbian is a version of Debian Linux, designed to run on the Raspberry Pi. It allows the pi to be used simply by plugging in a mouse, a monitor (or TV set) and a keyboard. It's graphical desktop is easy to navigate and the Operating System includes all of the tools needed to develop the software created for the lighting system. It may be downloaded from the The Raspberry Pi's Website.

piCore Linux

This is a version of Tiny Core Linux designed to run on the Pi and has the advantage that 'out-of-the-box' it runs as a 'live' distribution. This means that at boot-up the operating system mounts the SD Card read-only, reads everything into RAM and then never touches the SD Card again. It can be configured to load a desktop, but by default it boots into the shell, whereupon, the lighting software may be run automatically.

The benefits of this are that it is lightning fast (even on a Zero), it runs very little ancillary code to interrupt the lighting routines and the users can simply switch on the power to the railway in the morning and off again at night. In this mode, no mouse keyboard or monitor is required, although a keyboard and monitor can be plugged into the Zero for debugging or future development.

Software and Documentation

Copies of the software and documentation produced during the development of the Lighting System may be found at Software & Documentation. This information is available for re-use under the licence conditions given.

Acknowledgements

The developers of the lighting system wish to thank the following people and organisations who assisted in this development, either by direct support or by making their code or advice freely available on the Internet: