simond

Western Thunderer
As promised, a brief history of what happened since the Arduino thread began, in the hope that it will be a useful reference for anyone else doing this sort of thing;

The aim of the project was to provide a remote control panel for my 7mm Porth Dinllaen "might have been" loco shed model, with the following control functions;

Points - there are 7 points and a catch on the layout, and there would be one more to separate the shed road to the carriage sidings. Two of the points form a crossover, the catch is on the coal road ramp, and is operated by the same lever as the point it protects, thus 6 levers are currently required and I built in 8. The points are driven by Tortoises, with live frogs switched by the built-in switches in the motors.

Turntable - the turntable has 3 accessible roads, thus 6 logical positions

Lighting - I wanted to be able to operate the lights, preset the brightness and then switch on and off from the panel

DCC Program road - the front siding/headshunt is set up as a program track - there is a DPDT relay that disconnects the DCC operational supply and connects the track to the programming outputs. This relay is interlocked with the Tortoise so it can only be thrown if the points are set so that the siding and headshunt prevent a loco crossing onto the operational track.

Control panel - the panel is an old RS box from some test equipment that was being thrown out at work - it was rescued from the bin, and used on the old Greater Windowledge Railway prior to PD loco. It's a good argument for recycling, I don't know how old it was when I got it but it must be at least 15 years older now. It came with a 25-way D-connector on the back panel, and a 2m 25-way lead.

On the GWR, there were some points and a couple of ground signals, and the whole ensemble could easily be controlled using individual cores in the 25 way lead. I also arranged for a couple of 4mm sockets on the back of the panel, to have track or programming supply to them, so I can put the rolling road on the workbench and simply plug it into the panel. I also connected 4 cores to a DIN 5-way socket so the DCC controller would plug in as well. That worked on the GWR, but won't, with the t/t and lights, and programming, and more points, on PD loco, so something had to be done.

What I did was, in hindsight, not ideal, but was a step on the way - I put an Arduino Mega inside the panel, and created a couple of modules to be mounted under the baseboard to control points and turntable. Each of these had an Arduino Nano as a slave processor, and I connected them together using the I2C protocol which is built-in to the Arduinos. Someone on RMW told me that it wouldn't work, and he was right-ish. It did work quite well, but it was far from reliable, and became more and more of a frustration, as it was necessary to reset the control panel Arduino almost every five minutes. I never got around to getting the programming track control or lighting to work as it was evident that changes were necessary.

I then found a convenient CAN (Controller Area Network) interface for the Arduino, and started playing with that, and getting very frustrated because it never quite worked. I think programming can be like that, there is a (usually) simple error or step that is needed, and despite searching and searching, it sometimes simply doesn't make itself obvious, until it does, and then you wonder why it took so long to do something so simple...

CAN is very flexible, robust and capable of being far faster than would be needed in most model applications - it is also able to run an extended network so I doubt there is any issue with even a large garden railway. CAN interfaces are dead cheap, I think I paid £11 for four, and clone Arduinos are a similar price, so you could imagine making a slave "node" to control, for example, a junction on the other side of a garden railway, with a couple of point motors and maybe four signals, and connecting that back to the control panel using the network.

A diagram of the network hopefully clarifies what's going on. Each of the four slaves has a CAN interface and a Nano, the control panel has an interface and a Mega, simply because it has more inputs and outputs.

CAN Net.JPG

I've added most of the sketches to this thread, there is one missing, it's on another laptop, so I'll copy it over and add it. I also have an Excel file with the stripboard layouts for each of the slaves if anyone would like a copy.

There's probably lots more I could say, but it's probably easier to answer questions if there are any.

As I said three years ago at the top of this thread, I have some programming experience but am not at all a pro - and I am not an electronics engineer either, nearly everything I've done is just adding commercially available modules to Arduinos, I guess I pushed that boundary a bit making the points controller board, but there is nothing here that anyone who can solder and follow a diagram can't make.

There is also MERG, who have their own C-Bus system. This involves more soldering of components, and more opportunities for Cap'n Cockup to visit... so I avoided it. It's very definitely an alternative approach with all the same advantages. I guess it's whatever floats your boat.

Any questions, please ask away, I'll try to give useful answers!

cheers
Simon
 

Attachments

  • Control_Panel_Master_CAN_30Oct22.ino
    10 KB · Views: 6
  • turntable_30oct22_CAN.ino
    17.4 KB · Views: 7
  • point_control_slave_CAN_19oct22.ino
    2.4 KB · Views: 4
  • can_lights_controller_9nov22.ino
    2.3 KB · Views: 3
Last edited:

Stuart Birks

Active Member
As a newcomer to this forum I have only just found this thread. It is interesting as I have used a couple of Arduinos for controlling my layout. There is a standard Arduino UNO for the train control and an Arduino Mega for the points and signals.
This is a picture of my control box. The signals and points are operated with levers a bit like a signal box and the train control looks a bit like a steam engine cab.

panel1.jpg

panel3.jpg

Inside the box there are the electronics and the lever locking frame.

panel4.jpg

Using the Arduino for the train control opens up lots of interesting control options.
1. The power LED is dual colour and shows red if power is not going to the track and green if it is. It can be red if a short has occured or if on powering up the layout the speed control is not at zero so a train will not shoot off unexpectedly.
2. The speed control potentiometer only operates through 90 degrees of travel but the code can allow for this and give the full range. Additionally since no loco actually moves at very low voltage the first few degrees are zero then it immediatly jumps to a level where the loco is just about to start. I only have 5 locos so this is set at the one that starts at the lowest level.
3. The direction control is a potentiometer just to give something that mechanically looks like a reverser but is actually coded to act like a switch, changing direction approximatly half way up its travel. If the direction is changed while the train is in motion is slows down, quite quickly, then speeds back up in the opposite direction.
4. Inertia can be switched on or off. Even when off there is a small amount of inertia the same as on the reversing switch. When on the inertia is quite high and the brake can be used.
5. The brake control does what it says with the one additional feature that if you leave the brake on and open the regulator the train will not accelerate, just stay at the same speed as it is currently at.
6. If something causes the current limiter to trip the track power LED goes red and power is cut. Simply returning the regulator to zero will reset it. If the short is still there when you open the regulator it immediatly trips out again.
7. In a steam loco cab there is the pressure gauge. I have no pressure to measure so this is a speed indicator. It should be read as a percentage of speed not MPH so 100 is just full speed whatever that might be. As the speed control jumps to a level where the loco is about to start, this is the same but more so. It wont show anything until a higher level when the loco has probably started moving. So no showing 20% speed when it is not moving.

There are other switches on the panel but they are not controlled by the Arduino.
Here is a Schematic of the control circuitry.

Control Schematic.jpg
The layout is powered by a Micro PC ATX power supply which gives 5v and 12v at more power that I can ever use. The 12v supply goes through a relay which the Arduino controls for allowing power to the track. The speed is controlled by a PWM output from the Arduino going through an H Bridge motor driver which controlls speed and direction. A couple of volts are dropped accross this so I have put an adjustable boost circuit in. This is currently set by running a loco on full power and adjusting it until there is 12v on the track. It can easilly be tweaked to give a bit more if I want it. There is a current sensor which could go anywhere along this line. It again is adjustable and is currently set at about 1.8A. This curcuit pulls the interrupt pin on the ARduino low. The reason for using an interrupt is that it will instantly override anything the program is doing and go immediatly to the interrupt routine. This just turns off the track relay to cut power to the track
and turns the track power LED red.
The speed, direction and brake are all potentiometers and the inertia is just a switch.

All the points and signals are operated by radio control servos. There is a switch on each lever going to the Arduino Mega. The operation is much simpler here, just a procedure for point movement, one for the signal up and one for the signal down. This means that signal arm movement like bounce can be replcated.
This is the schematic for the circuit. It is basically this repeated 21 times.

Control Schematic 2.jpg
When first powered on the progran checks that each servo is in the correct orientation to match the lever. In theory this could mean all the servos moving a bit on startup but in reality this doesnt happen. Even so I have allowed for it buy starting them in three groups.
I run a seperate program for calibrating the servo values for each end of its travel and then put these into the main control program. I also have a seperate program for measuring the potentiometer values at the ends of their travel and these values are put in the control program.

This ia a link to a video I made when building the signals.

Stuart
 

Attachments

  • panel2.jpg
    panel2.jpg
    139.9 KB · Views: 7

John Baker

Western Thunderer
Oooft...... I've just started playing around with Ardiunos, but this is on another level! Love the layout of the control panel. I suspect you given a lot of people a lot of food for thought here! Inspirational.
 

ovener

Active Member
That's very nice! I really like the controls - you've taken it to another level there. I also use Arduinos to control various things - they are great little things.
 

simond

Western Thunderer
I hope I am not hijacking simond's thread. I can start a new one if it would be better.
Stuart,

I started the thread so any relevant topics could be kept together, and this thread would then be a reference source going forwards.

I’d be delighted for you to add as much detail as you’d like here, but if you think it would be better in a separate thread, please simply cross link the two so it’s easy to find and refer in future.

you’ll also find that Arduino sketch files are a permitted attachment, so “fill yer boots!” :)

cheers
Simon
 

J_F_S

Western Thunderer
...
Using the Arduino for the train control opens up lots of interesting control options.
...

Stuart

Many thanks for posting Stuart - great work!

I wonder if you have considered or looked into using the Arduino as a feedback controller, rather than 'just' to provide an output pre-determined by the controls? 'In principle', it would appear simple to sense the back EMF during the pulse-space and adjust the pulse width accordingly (I think this is the principle used in the PICtroller - which I use and which is quite good) but I don't think I have seen examples of code for this anywhere and I don't know how you could sense the voltage via a pin defined as PWM. I imagine you have to somehow create a bi-directional pin and manage the pulse width from the code using a frequency to suit the motor characteristics.

A second question concerns robust operation - I built an Arduino interface unit to go between my PC signalbox simulation and real signalbox equipment - complete with lots of copper and iron. It works fine; for 10 - 30 minutes or so; then the Arduino locks up and needs rebooting. This despite being isolated via opto isolators, two sets of low-power relays, lots of snubber diodes, decoupling capacitors...
Someone else with a similar set-up traced the issue to poor quality power over the USB line - which would not affect your case. But I would be interested to hear if you (or Simon) have had lock-ups.

I am working on the design for an NX panel (based on a Teensy, but same principle) and it needs to be dependable!

Love the holes in the driver's brake valve - almost hear the air flowing in ...

Many thanks again,
Howard
 

Stuart Birks

Active Member
Hi Howard
The Arduino has several output pins which are configured within the hardware to provide a PWM outout. This makes the code very easy as all it takes is a simple line of code :-
analogWrite(outPin, outValue);
In this way there is no access to the EMF voltage during the space cycle. I have left the PWM frequency at the Arduino default of 490Hz and it works fine. There is a way of changing the frequency using the PWM.h library if required. It is possible to code your own PWM by turning a pin on and off in code and then you would be able in theory to read the back EMF but it would be no use as the moment you tried to do anything with any other code you would temporaily delay the code turning the pin on and off. If you really want a feedback controller then the easiest way is to get the Arduino to drive an external bit of electronics. You need something externally anyway to provide the paower to the motor as the Arduino can only source or sink about 20mA. I have used an H Bridge driver rated at 2A.
For your second question, I have not experienced any form of lockup at any time. Part of the reason for using an ex PC power supply is to give a reasonable 5V supply. If PCs dont crash then I assume that it is OK for the Arduino. An alternative would be to use the Arduino's built in regulator and give it around 12V.
Hope that helps
Stuart
 

J_F_S

Western Thunderer
Many thanks for your prompt response Suart.

I fully agree that a feedback controller would not be able to use the built-in PMW features and would necessarily have to run real-time closed-loop code, setting the pulse width using software timers but since the Arduino is capable of running a code loop much faster than the H bridge would be capable of responding to, that should not be an issue and gives a bonus of being able to adjust the frequency as well as the pulse width - though I suspect you are near the ceiling of your H-bridge already.

The reason I ask about this is that my experience over decades with DC operation - dating back to Pentrollers - is that feedback controllers are excellent when well-matched to motor characteristics (and a nighmare when not!) and so it is something well worth looking at. The DCC bods will point out that modern chips have this feature on board of course!!

BUT then everything is easy at the concept level ;) and if I am so enthusiastic, I might need to get my hands dirty ...

BTW - you mentioned external electronics - do you have anything particular in mind?

Looking forward to further updates!,
Howard
 

Stuart Birks

Active Member
Maybe off topic for this thread but that mechanically interlocked lever frame could be a subject of interest. :)

Jim.
I'll do a seperate thread on that sometime. As I am new to this forum I just need to decide where to put it (I mean what forum to put it in)
 

Stuart Birks

Active Member
BTW - you mentioned external electronics - do you have anything particular in mind?
Basically any motor controller that has a potentiometer as its control can have the potentiometer replaced with the output from an Arduino or anything else that creates a variable voltage. Just remember that the Arduino will only give 0V to 5V where most railway potentiometers will give a variation of 0V to 12V. This can be overcome by using an OP Amp to increase the voltage. A CA3140 works well. A 741 doesn't but I cant remenber why.
This is the external electronics for my controller while under development
IMG_3611.JPG
Stuart
 

simond

Western Thunderer
thanks for this. The link in post #56 unfortunately doesn’t work, this does:


I’ve just skim-read the instructions and it seems like a really well put-together project, with a very wide range of control options, connectivity and potential for expansion. I haven’t priced it, but I expect it’s cheaper than a similarly-capable commercial set-up, with the added benefit of learning as you build it, and everything is open source. What’s not to like?
 
Last edited:

Richard Gawler

Western Thunderer
What’s not to like?
Some years ago I bought into the Roco z21 Start system for my H0, and soon afterwards I bought their wireless handset and the associated router needed to make this work.

I do like a tactile, physical throttle control, so I can look at the trains not the controller. While there is little benefit in me building this system to replace my z21 (unless it breaks), I would want to implement some kind of mechanical throttle and reverse mechanism using physical controls in place of the provided "touch screen" options.

Adding this functionality would take the project up to the notional 'engineer' level and I know I would find it quite difficult to get working. Perhaps a second Arduino to manage the throttle and reverse, with an off-the-shelf wireless connection.
 

Richard Gawler

Western Thunderer
What I am trying to say is, when I wanted to add some new functionality for my own Arduino projects, and found that I had to write an interrupt handler and alter the PWM frequency to drive a motor, I met up with some cerebral end stops. I simply could not work out what on earth I needed to do, and while I found some examples online, the sheer complexity of these examples told me I had absolutely no chance of building something worthwhile from scratch. I found the Arduino to be beguilingly straightforward to do inconsequential things (like the usual textbook examples) but murderously difficult to implement anything I could be proud of. Hmmm . . . maybe it's just too difficult for me.
 
Top