Introduction: Smartphone Controlled Model Railroad With an ESP8266
A while back, I made a project where a stepper motor's position is controlled using a smartphone with the help of an ESP8266 microcontroller. After doing that project, I thought why not try controlling a model railway layout with the same concept, so here we are with this project.
Today, let's learn how to control a model railroad layout using a smartphone through Wi-Fi with the help of an ESP8266 microcontroller.
Let's get started!
For this project, you will need:
- An ESP8266 microcontroller(With least 4 digital outputs, including 2 PWM outputs)
- A dual-channel motor driver(One channel for track power, other for turnout)
- A breadboard
- Male to male jumper wires
- A 5-volt output voltage regulator(Example: L7805CV)
- A small heatsink(Screwing a small piece of aluminium to the voltage regulator can also work)
- *A DC power source
*The voltage of the power source depends on the operating voltage of the locomotive and the turnout, make sure the voltage regulator and the motor driver are rated for the supply voltage.
Step 1: Watch It Working
Watch the above video to know how it works and understand the controls.
Step 2: Program the Microcontroller
You can get the Arduino code folder from here. Extract the contents of the zip folder and copy the folder named "ESP8266_Model_Railroad_Layout_Control" to the sketch folder of the Arduino IDE. Generally, it is in the Documents folder of the computer.
Before uploading, make sure to put in the name(SSID) and password of the Wi-Fo router to which you intend to connect the ESP.
Step 3: Check the Web Controller
After uploading, open the serial monitor and make sure the baud rate is set to 115200. The blue LED on the ESP will blink until it is connected to the router, after that, it will display an IP address on the serial monitor. Note or copy it and enter it in the address bar of the web browser of a device that is connected to the same Wi-Fi router. You should see a page as in the second pic.
On sliding the slider or tapping on the buttons, you should see the ESP displaying some text on the serial monitor.
Step 4: Set Up the Layout
Step 5: Put the Microcontroller and Motor Driver on the Breadboard
You can also use a PCB with soldered female headers to make a more permanent setup but a breadboard is good for testing the setup for the first time and setting it all up quickly.
Step 6: Connect the Motor Driver to the Microcontroller
Connect the +ve input pin of the motor driver(Usually marked 'VIN', 'VMOT', etc.) to the +12-volt supply and connect its GND pin to the -ve of the supply.
Make the following connections from the microcontroller to the motor driver:
D5 - > IN1
D6 - > IN2
D7 - > IN3
D8 - > IN4
A lot of motor drivers, like the one which I use, require an enable pin to be connected to the +ve of the logic voltage, so connect it to the 3.3-volt pin of the microcontroller.
Step 7: Connect Track Feeder and Turnout to the Setup
Connect the track power feeder's wires to the pins of the motor driver marked OUT1 and OUT2 as they correspond to the pins IN1 and IN2.
Connect the turnout's wires to the pins of the motor driver marked OUT3 and OUT4 as they correspond to the pins IN3 and IN4.
Step 8: Connect the Voltage Regulator to the Setup
Mount a small heatsink to the regulator to keep its temperature in a safe range.
Connect the regulator's left pin to the +12-volt supply, its middle pin to the GND of the circuit, and its right pin to the VIN pin of the ESP board.
Step 9: Connect the Setup to Power
You can use a barrel-type connecter to connect the setup to a 12-volt DC adapter easily.
Step 10: Place the Locomotive on the Tracks
Make sure the locomotive is sitting properly on the tracks to prevent derailments and short-circuit of track power.
Step 11: Power Up the Setup and Open the Controller Page
After powering up the setup, the LED on the ESP board should blink until it gets connected to the Wi-Fi router. After that open the control page on the browser of the device from which you want to control the layout by entering the IP address you noted from Step3. Make sure that device is connected to the same Wi-Fi router.
Step 12: Test the Controls
Check if your controls are working as shown in the video.
If the locomotive is moving in the wrong direction, just reverse the polarity of track power by swapping the track feeder wires with each other. You can do the same with the turnout wires if you want to switch it in the opposite direction.
Step 13: Add More to the Project
The example provides here can only control a single locomotive and a turnout and I am sure that it is not enough for a regular model railroad layout. So why don't you try to add another turnout or maybe a locomotive control?
It might be but difficult to understand the code and modify it, but when you start to understand it, you can do a lot of things to improve this project and suit it to your liking.
Whatever you do, try to share your creation with the community to inspire more people to try this project.
All the best!