Files
website/content/Articles/smart-train/index.md
T
2026-04-04 12:36:54 +01:00

2.9 KiB

title, date, draft, image, summary, tags
title date draft image summary tags
Smart Train 2023-04-20T12:27:11Z false /projects/smart-train/featured.png EVERYTHING MUST BE SMART! I take an old Hornby train and make it controllable from Home Assistant
Smart-Home
ESP32
<style> section img, .content img, article img { max-width: 70% !important; height: auto !important; display: block; margin: 2rem auto; border-radius: 8px; } </style>

Featured Image

📋 Project Highlights

  • Learning how to integrate electronics with IOT
  • Experiment with basic motor control
  • Setup an automated train that will eventually go around a Christmas tree based on smart home parameters.

During Covid I purchased a cheap second hand Hornby train set from Facebook Marketplace. After spending a surprisingly long time sanding rust off the tops of the tracks I was able to get the old train running again. These old analogue train sets work by connecting a power supply to the tracks and varying the PWM. This varied power causes the motor in the train to run faster or slower. This made me wonder how difficult it would be to setup a WIFI enabled micro controller to control the power to the tracks, thus enabling me to control the train both from my phone and to even become a part of my smart home.

Motor Control

The microcontroller chosen was the ESP8266 due to its low cost, simple integration into home assistant and built in WIFI. This was connected to a L298N motor controller, an IR sensor gate and a Neopixel light strip. Everything was then mounted underneath the train tunnel to hide the wiring from view.

Description

ESPHome

The ESP microcontroller was then flashed with ESPHome and added to home assistant. The code was written in yaml to utilize the IR gate to detect when the train had passed through. This allowed me to count the number of laps and with some careful timing meant I could make the train automatically stop at the station. The motor controllers were set as a gradual 1-10 scale going forwards and a simple on/off reverse button. Finally control of the Neopixel strip was added. This was further synced with the IR gate to allow the lights to be turned on as the train enters the tunnel.

Smart Home Integration

With the train now fully controllable from within home assistant the control was expanded to google assistant. Since I already had google connected to my home assistant server I simply needed to add the configuration to expose the new controls. This allowed me to turn the train on/off with voice commands through my google home mini speakers.

Description

🏁 Future

The core functionality of this project is completed with everything I wanted to achieve being successful. Future improvements would be expanding the train control functionality and adding another sensor to make stopping at the station simpler and more reliable at different speeds.