Subscribe via RSS

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:


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.

Note:

  • 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).

Source Code

The source code can be downloaded here.

Photos

Here's a few shots of the current set up:



Please do ask any questions you have about this... I've probably skimmed over a lot and am more than happy to update this as necessary. I intend on getting on to more interesting things with this controller as I get the time.

avatar

About stevenh

Trains... trains... trains... + Electronics + Japan.
Comments (10) Trackbacks (0)
  1. Very nice! I’ve been thinking about doing this lately myself. I wonder where we could have custom levers made up…

    • I’d say a trip to the local hardware store would get you enough random bits and pieces to build something. Otherwise you could get your hands on a Densha De Go controller; of which I have for the Dreamcast, but am having troubles attempting to decode. That will be the next topic, if I can successfully communicate on the maple protocol :)

  2. Quick question: Does the L298 output a smooth DC signal, or a PWM signal? I’m thinking now of building one following your model, but I do not want PWM output to my tracks (because I’m going to also use it as a “jump throttle” with my Digitrax Zephyr, which requires smooth DC output).

    • As far as I am aware the output signal is totally dependent on the input signal. If you pulse the enable or directional pins (as is done to create a ‘pwm’ signal in most examples) then it will output PWM.

      Otherwise, a constant voltage applied to the correct pins will see a constant output voltage; Therefore creating clean DC power (afaik.)

  3. This is very interesting info. I’m considering using this method to control micro slot cars. If you have made any further improvements since your last post, please let us know. Once again thanks for posting this.

    Martin http://www.HeliumFrog.com

    • Martin, thanks for your interest! I am still using the exact same circuitry to control my Model Trains. Controlling slot cars wouldn’t require any changes to the project, but you’d have to be careful as to the final voltages on the tracks? I imagine they don’t use a full 12v?

      Either way, you’ll lose about 20% on the output, so make sure your input is provided accordingly.

  4. Steven,
    Great concept. I have need for a DC controller that will work with JMRI so that I could use the EngineDriver app on my Droid phone. If I was able to use JMRI I could use my Droid phone as a throttle and have engine sounds/Horn/Bell play through my laptop speakers. Will your design work with JMRI????

    • Ok, there’s probably a few ways to do this… but the most straight-forward could be to create an SRCP server either on the Arduino code itself, or as an application on the host computer that then communicates to the Arduino over USB/Serial.

      SRCP is the protocol used for DCC, so it may be overkill for JMRI and DC on this, but it should work. I had a quick look at the ‘hardware’ for JMRI but couldn’t see an interface for standard DC controllers.

      If you had an Arduino with the Ethernet sheild and wrote the SRCP server into the Arduino then you wouldn’t need a computer at all.

  5. Read your article withgreat interest. Love the application. Need an HO controller here, desparately.
    You did not identify the Arduino board nor provide a parts list, (circuit board, leds, pinout, code entry, etc).
    Can you help?
    Sincerely,
    Lansing

    • Frank, as stated in the first main paragraph, the Arduino I’d chosen and used was the ‘Mega’.

      As the circuit diagram shows, the parts list is as follows:
      – L298 Dual Full-Bridge Driver
      – 0.5 Ohm 5W Resistor
      – Potentiometer (This is the throttle and it’s ohm rating doesn’t exactly matter as you can fine-tune it with the Arduino’s analog input reading commands. Either way, 10k is a good number.)
      – 4x UF4003 diodes as ‘fly-back’ to prevent motor current from trashing the circuitry.

      Although simple, the circuit diagram does show everything you’ll need to power the throttle and therefore your trains! :)

      You could also ammend the diagram to add in an LM7805 from the 12v power to power the ‘logic’ side of the L298, but that’s not too much of a priority. If you do power it externally make sure you connect all the GNDs together.

      Steven.


Leave a comment


*

No trackbacks yet.