Controlling your trains with an Arduino
A quick introduction to the Arduino
Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software. It’s intended for artists, designers, hobbyists, and anyone interested in creating interactive objects or environments.
Arduino can sense the environment by receiving input from a variety of sensors and can affect its surroundings by controlling lights, motors, and other actuators. The microcontroller on the board is programmed using the Arduino programming language (based on Wiring) and the Arduino development environment (based on Processing). Arduino projects can be stand-alone or they can communicate with software on running on a computer (e.g. Flash, Processing, MaxMSP).
The boards can be built by hand or purchased preassembled; the software can be downloaded for free. The hardware reference designs (CAD files) are available under an open-source license, you are free to adapt them to your needs
Using it on your Model Railway
So, I recently purchased an Arduino Mega Microcontroller with the intent to control a Model Railway with it. This article will be the first in a series to show you how to use an Arduino to control different areas of a layout. Our first goal will be to create a controller/throttle with very basic Acceleration/Braking and a 12v DC Pulse Width Modulated output.
Note that this will all be based on DC electronics; this has nothing to do with DCC.
First, here’s a list of web resources for controlling a 12v output with the Arduino:
- Basic 12v Output
- AdaFruit Motor Shield – Circuit that plugs directly onto your Arduino and provides outputs
- DIY H-Bridge Add-on
- DC Motor Control Using an H-Bridge and a PIC
- DC Motor Control Using an H-Bridge – Similar to above
- Dual Motor Driver with Arduino using a SN754410NE Quad Half H-Bridge
- L298 Hbridge meets Arduino mega
- PWM with l293/l298 – Provides great information on how to control a H-Bridge
- Controlling a DC motor with the Arduino and L293D
- DC Motor Driver v1.1
- 4A H bridge motor driver using the L298 IC
- Bi-directional Control Of Motors And The H-Bridge
What I created
In the end I chose to use the schematics and information from the guys at pyroelectro.com which uses the L298 H-Bridge integrated circuit. My main reasoning was that, although they used a PIC microcontroller, they correctly controlled the L298 with PWM on it’s input rather than it’s enabling pin. Either way, the PWM signal is still created…
Here’s the schematic. I’ve added a few extra buttons and LEDs and also added a potentiometer for speed control.
- The IC is facing towards the viewer (i.e. so that the text on the IC is visible.)
- It’s also recommended to use a heatsink!
- Ensure that you connect the ground(GND) on the L298.. It’ll overheat and fry if you don’t.
- You must use Ultra Fast Diodes for the flyback diodes. More information here
Right, what do you need to know?… The PWM pins are labelled on your Arduino board. By default they output a PWM signal when you feed an analogWrite(pin_number) to them. This creates a pulse that the H-Bridge will respond to. The frequency of this pulse (wave) will then govern the final output voltage to the tracks.
For direction control you either apply digitalWrite(HIGH) to PWM2 and digitalWrite(LOW) to PWM3 or vice versa for the opposite direction. Applying LOW to both pins will stop the output and HIGH to both will short circuit!
I’ve added S1 and S2 to control my direction. It starts off going ‘forward’, but pressing S2 will set the direction to backwards. The code is written to gradually stop and then accelerate in the opposite direction. Pressing S1 will then set the direction forwards again and the reverse will occur.
S3 is the emergency stop button. Resetting the throttle to zero will cancel the emergency stop flag.
The potentiometer is the throttle… I found a huge one at an antique store down the road and love it. It’s rated at 250ohm (no idea what current) and when at ‘100’ the analogRead reports just over 1000. The throttle is only for speed, I really should add a brake lever, as when you return to zero speed the train will only gradually stop. You need to then hit the reverse (or emergency stop) button to stop the train faster.
You’ll also require a 12v DC power supply. As your little engines may use up to 1A when starting, make sure this power supply is sufficient. Also, if you don’t intend to have the Arduino plugged in to a computer after programming, then this 12v can also supply it (connect all ground wires together!). Just make sure it’s a regulated and safe power supply. Note that different Arduinos can handle different voltages! Find your board listed here and then work out the power supply details, otherwise the Arduino Mega details are here (Input max 20v DC).
The source code can be downloaded here.
For more detail: Controlling your trains with an Arduino
EasyEDA: Ideas for Circuit Design, Innovation for Electronics Access
Free Circuit Design: Schematic – Simulation – PCB Layout – Gerber Viewer
Cheapest PCB Prototype: Only $2 for 10pcs 10×10cm PCBs, 24 hours Quick Turn, DHL Delivery in 3 Days