ESP32 N-Scale Controller Project
I would like to document a project that I’ve been working on for a few weeks now - creating an ESP32-based control board for N-scale model locomotives. Ultimately, my goal is to incorporate wifi control with a few other features that would be possible using the ESP32 microcontroller.
Motivation
Model trains are cool, and I think that the technology has advanced enough to allow for a cheap (compared to more traditional DCC equipment) open-source control board to be made using ESP32s that could easily exceed the functionality that is currently available for model trains.
The main feature request that motivated this project is to implement a version of CBTC for model locomotives. The choice of N-scale was made since I’m in a one bedroom apartment and don’t (yet) have space for a larger scale setup.
The base
The model locomotive (only one for now) that I have is a Kato Amtrak ALC-42. Variants of this loco with a DCC board exist, but the version I have is only ‘DCC ready’. Doing some research, I found MT40, a group focused on producing model railway control equipment. On their wiki, they have several open source DCC control boards, including a board that is compatible with the locomotive that I have. I will be using this as the base to work from, largely from the hardware side. The firmware is a useful reference, but considering I will be using a very different MCU with a different communication method, new firmware is probably going to be a requirement.
Communications
My goal is to use wifi to communicate with the locomotives. For all its capabilities, DCC isn’t built for the type of high-ish frequency, bi-directional communication that I want to do. Wifi is also cheaper to use since most people have access to a wifi hotspot.
Odometry
This is the biggest hurdle that I have to solve. My goal is to determine the live position of each locomotive with an acceptable accuracy and to use that position to control the speed and limit of authority of the locomotives. This position can be recalibrated periodically through traditional methods (block power detection, IR sensors, etc.) Originally, my plan was to use a magnetometer to detect the changes in the magnetic field around the motor (or some other added magnet), but I think I have a better option:
Ripple counting
Enter the TI DRV8234, a motor driver IC with an integrated current ripple counting circuit. By detecting and counting the ripples caused by the motor brushes moving between commutators in the DC motor, it is possible to determine the position (and speed) of a brushed DC motor without needing additional sensors. Looking through the datasheet, it seems like this should work, at least at low speeds for model locomotives.
The plan
- First, get a basic board together that uses an ESP32 and a DRV8234 to provide basic speed control over wifi.
- Second, calibrate and try to get ripple counting working.
- Third, figure out if a ripple counting control board can be accurate enough to do something like CBTC at N-scale.