Jump to content

Layout control with an Arduino


Recommended Posts

For a long time i've been thinking about collecting all the information i've found about layout automation using the arduino microcontroller. There have been some serious projects using this platform and imho it would be good if someone who is interested in the topic could find everything in one place.

 

Many years ago, i've seen the tram controller article by Ken Shores, but don't know if the project was finished:

http://sumidacrossing.org/MyOtherLayouts/SC/ElectricalSystems/TramController/

 

Since then, i've been doing some experimentation with both the Tomix TCS sensors and the Arduino. As a motor driver i've been using the L293D driver ic, but as i see the L298 is also popular for the same task.

 

For automation, you'll need 3 basic things:

-a way to control power to the motors (either analog or DCC)

-a way to detect trains at various points or in various blocks

-a way to change turnouts

 

A nice optional feature is to have user controls, like analog throttles and input buttons.

 

I plan to collect all the information and simple circuits i know about and maybe even get all the parts together for a complete system.

Link to comment

This is the Arduino Mega 2560 board:

post-1969-0-67083200-1429000876.jpg

It has 16 analog inputs, 11 pwm outputs (which 9 is usable for speed control) and 32 digital inputs/outputs.

 

The PWM outputs are grouped on the Arduino Mega 2560 around the following timers:

   timer 0 (controls pin 13, 4)
   timer 1 (controls pin 12, 11)
   timer 2 (controls pin 10, 9)
   timer 3 (controls pin 5, 3, 2)
   timer 4 (controls pin 8, 7, 6)

Timer 0 is used for delays and timekeeping, so this means pins 13 and 4 are not usable for motor control. The small green led on the board is controlled by pin13, so it's only usable for information feedback anyway. This leaves us with 9 usable pulse width modulation (pwm) outputs. These ports can be used to control the speed of a train with a simple command called analogWrite(). For controlling the direction of the trains additional digital outputs are required. More on this later.

 

For detecting the location of a train, a Tomix TCS sensor or an equivalent detector can be used. These are really simple but effective sensors that have 3 pin connectors. The middle wire is ground, while the two side wires correspond to one of the directions of travel in analog operation and are triggered together when using DCC. The side wires need pullup resistors to 5V to work and the sensors connect them to the middle ground when triggered. This is accidentally how you hook up two simple manual pushbuttons to the Arduino. The circuit is called open collector wiring, which allows multiple buttons/sensors to be connected parallel. The TCS system allows daisy chaining multiple sensors, so one common sensor line can be triggered by any of them. The function to read these inputs are: pinMode() with INPUT_PULLUP for configuring the pin and digitalRead() for reading the value. Thanks to the pullups, they will be low (0) when active. Another type is diode block occupancy detectors, that detect the motors or lights of a train in a block. If these detectors have a relay or a TTL level open collector optocoupler output, then they can be used instead of the Tomix TCS sensors without any software modifications. The same is true for infrared gate detectors, but they are slightly more complex to set up reliably.

 

For turnout control, a bipolar coil driver is needed for both Kato and Tomix turnouts. They work like DC motors that must be turned on for a very small time to avoid burning the coils. This means the motor driver IC-s used for controlling the trains can be used to drive the turnouts, but they have to be connected in a different way and driven with digital signals using digitalWrite(). A safe way to avoid burnt turnouts in case of software failiures is to use capacitor discharge circuits (essentially capacitors connected in series with the turnout coils). This means the output of a digital port always indicates the direction of the turnout. Also you can drive up to 4 turnouts this way from a dual motor driver ic with 2 pairs of motor output ports.

 

For manual throttles the most simple way is to use a potmeter and read its value on one of the analog input pins using analogRead(). The most simple circuit is shown on the Sumida Crossing site, using a 10 kiloohm potmeter connected between gnd (0) and 5V with its middle tap giving an analog value. It's a low current circuit, so the Arduino needs some time to read it. This means when more than one potmeter (or analog input) is used, then the port must be selected with analogRead(), some time (a few millisecs) must elapse, then the port has to be read again with analogRead() to get the correct value. High current circuits are faster, but can overload the Arduino, so this is a safer solution. The value read with analogRead() can be used for setting slow start/slow stop delays, station dwell times or simply used to set the speed of a train. The latter gives us a simple home made PWM throttle.

 

In later posts, i'll try to explain everything above with circuit diagrams and give exact 'how to'-s about wring all this up and writing simple programs that can be uploaded to the Arduino and run with no computer connected. It's even possible to have multiple home made programs on the Arduino, in a very similar fashion to the Tomix TCS control box, except up to 8 trains and up to 32 sensors or turnouts can be controlled at the same time.

post-1969-0-67083200-1429000876_thumb.jpg

  • Like 1
Link to comment

This is the Arduino Mega 2560 board:

post-1969-0-67083200-1429000876.jpg

It has 16 analog inputs, 11 pwm outputs (which 9 is usable for speed control) and 32 digital inputs/outputs.

 

The PWM outputs are grouped on the Arduino Mega 2560 around the following timers:

   timer 0 (controls pin 13, 4)

   timer 1 (controls pin 12, 11)

   timer 2 (controls pin 10, 9)

   timer 3 (controls pin 5, 3, 2)

   timer 4 (controls pin 8, 7, 6)

Timer 0 is used for delays and timekeeping, so this means pins 13 and 4 are not usable for motor control. The small green led on the board is controlled by pin13, so it's only usable for information feedback anyway. This leaves us with 9 usable pulse width modulation (pwm) outputs.

 

I count 10 left, so wonder if the L.E.D. is actually controlled by 12, 11, or some other pin?

You've assigned double duty to pin 13.

Link to comment

The led is controlled by the pwm output on pin 13 and its timer is also chained to the internal clock, which means the pwm phase is slightly incorrect (both for pin 13 and 4). We have 12 pwm outputs and you are right, 10 remain after removing 13 and 4. Pin 4 can be used as a digital i/o though. So usable pwm output pins are: 2,3,5,6,7,8,9,10,11,12. (personally i use 11/12 as DCC outputs in another project) The dual motor driver ic-s need 3 inputs for each half, so direction 1, direction 2 and enable. The PWM signal can be connected many ways, but generally 1 or 2 digital pins are needed for the direction control besides the PWM.

 

For turnout control, the enable pins can be fixed and no pwm is required, so it's possible to drive 4 bipolar (Tomix/Kato) turnouts from 4 digital i/o pins used in output mode with a dual motor driver ic and 4 capacitors connected in series with the turnout coils and tied to ground on the other end. I'll draw up some circuit diagrams for this. Any pin can be a digital i/o pin, so 14 to 21 is also available besides 22-53. Personally i would avoid using analog input pins as digital outputs if other analog pins are in use. Pins 0 and 1 are reserved for the serial line going to the USB connector, so it's a good idea to leave those alone. Also, always power the Arduino board from the pulg, since only that input has any protection and the board itself is too weak to drive non TTL loads directly and needs driver chips for that (like the two motor driver ic-s i mentioned), which also need external power.

Link to comment

For automation, you'll need 3 basic things:

1) a way to control power to the motors (either analog or DCC)

2) a way to detect trains at various points or in various blocks

3) a way to change turnouts

 

A few old posts from my blog:

1) Controlling motors: http://modelrail.otenko.com/arduino/controlling-your-trains-with-an-arduino

2) Detecting trains:

   RFID: http://modelrail.otenko.com/arduino/tracking-trains-with-an-arduino-and-rfid

   Infrared: http://modelrail.otenko.com/arduino/infrared-arduino-revisited

   More Infrared: http://modelrail.otenko.com/arduino/multiplexing-photodetectors-to-detect-train-occupancy

3) Turnouts: http://modelrail.otenko.com/arduino/multiplexing-controlling-points-or-other-high-current-devices-with-an-arduino

 

All arduino posts are under: http://modelrail.otenko.com/category/arduino

 

They may well be of use.

Link to comment
ToniBabelony

This topic is poking my interest, as it seems a lot more inviting than PIC programming and soldering, as well as investing in Tomix' TCS automator box and a controller. The programming also doesn't look too complicated... I'm smelling possibilities.

Edited by Toni Babelony
Link to comment

Yes, the Arduino family is very flexible. I still plan to make some circuit diagrams and maybe a demo with a simple turnout and motor controller, but i have to find the time first. The TCS sensors are very digital circuit friendly, so it's easier to connect them to the arduino than a motor controller. (but there are already examples for that amongst the links above)

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...