Train Controller: Software

← Back to Train Controller

The controller runs as one Python program on the Raspberry Pi. It uses the pigpio library for accurate hardware PWM and serves its own web dashboard.

What it does

The program runs a loop: the train accelerates, runs at full speed, slows down on approach, stops at the platform, the turnout switches, and the cycle repeats. Speed changes in small steps every 0.05 seconds, so the train accelerates and brakes smoothly.

At a platform the train waits 8 seconds. That is the time the mechanical turnout needs: a one-second pulse, and then seven seconds to finish moving. Then the train accelerates to full speed again.

Recognising each train

A background routine reads the code on the train (a barcode or a QR code; the scanner reads both). Each train has its own maximum full speed and slow speed in a small SQLite database, and these are loaded automatically when the train is identified. That way every train behaves realistically.

Safety

An emergency stop halts the train immediately. A separate shutdown button waits until the train has stopped, tidies up, and then powers off the Raspberry Pi. Both buttons use double-press detection, so an accidental touch does nothing. Overcurrent protection is done in hardware (see the Hardware page).

The web dashboard

A dashboard built into the program shows live status and lets me control the speed, trigger the emergency stop, enter a train’s identity and see the current readings. It can be opened in a browser on any device in the home network.

Status

This is a work in progress, developed and tested step by step on the bench. At the moment I am checking the slowest running speed against the motor’s starting threshold on the rebuilt two-board hardware.

Source code on GitHub

Scroll to Top