Friday 27 January 2023

Computers and Model Railways

No, I'm not converting the layout to DCC! The layout ran well at Maidenhead with the exception of the two oldest locomotives. My model of "Earl" and my friend's model of "Countess" were not happy with the 12" radius points. "Earl" would fight its way through the points but "Countess" would only operate on the straight track. this is due to them being 20+ years old and using wheels with deep flanges which hit the check rails on the curves. "Earl" was able to go further since it has flangeless centre drivers. Combined with the deep wheels are the outside frames which mean there is no scope for opening the back to back of them. This, hopefully, isn't the end of the world as I have replacements for "Earl" and "Countess" which are planned to be built in the coming months and they will use modern wheel sets. If that fails then I will have to resort to mainline points which are 18" radius but I hope not to go there since that will reduce the length of the sidings by 4" overall.

The only problems encountered at Maidenhead was operator error on setting the points with the occasional stutter over the frogs. The stuttering will be sorted with point motors with frog switches. In my last post I mentioned I was short of one MP1 point motor. The estimated delivery of mid December came and went and another good friend, Adrian, offered me one from his secret stash so I bought that and had enough to start.

The next issue was whether I could reduce the operator error? Get better operators would be the correct answer but that would mean I wasn't allowed to operate my own layout! Traditional operation would have a switch for each point which still leaves scope for not setting them correctly. They could be paired up so that each switch operates the corresponding point at the opposite end. That would reduce the errors but was never going to fully eliminate them. One answer was to operate the points using a push button for each track that, when pressed, would change all the necessary points to make that track the running line. It had to be done!

This is the finished result from above.


The five sidings match 5 push button switches top left. This is matched by 5 LEDs next to the switches. The LEDS flash while the points are changing and go solid green when the points are set. The location of the switches is such that when operating the layout from one side (whilst talking to visitors) I can reach them. The is a red LED below the 5 green ones which will flash if something goes wrong and blips every 5 seconds to say the system is working. 

As an aside, have a look at the photo above and tell me where the baseboard side has gone. There is clearly some trick of the light taking place but I cannot see the baseboard side on the edge at the bottom of the picture - weird!

How did I do it, you may well ask. Well let me tell you. I have previously experimented with PIC controllers and briefly considered them but I would need to build a microprocessor board to make it work and the chance of failure would be high. I decided to take a look at the Raspberry Pi and Arduino group of educational systems. These are prebuilt microcontroller boards for which there are a variety of accessory cards, meaning there would be little extra circuitry to wire up. I didn't look too closely at the Raspberry Pi as when I looked at the Arduino it seemed to be ideal for the job.


Pictured above is the Arduino Uno by Keyestudio, wired up and fixed under the fiddle yard. The standard Uno features 14 digital input/output pins and 6 analog input pins. I also picked up the next variant, the Mega, which has many more inputs and outputs. The nice thing about these microcontrollers is that they use a standard USB connector to hook up to the computer, an off the shelf  9V power supply and they are programmed in 'C'. 'C; is the programming language I used most in my career, along with assembly language, so I felt at home straight away. The reality was that writing a program to flash the light on the PCB took me all of 10 minutes. 


Once the microcontroller was decided on then I needed a way to drive the point motors. I bought a solid state relay board but then discovered that it could only switch mains AC so that went back. Instead, I bought an Elegoo 8 channel relay board, shown above. The wiring is a neat ribbon cable to the Arduino and the relays have screw terminals to drive the point motors. The photograph above shows the board with one point motor still to be wired up.

It might be at this point that those of a less nerdy disposition should skip the next few paragraphs down to the photo of the point motor.


What remained was to connect the switches and the LEDs to the Arduino. The LEDs are simply driven from the remaining digital outputs (8 are used for the relays) They need a resistor inline to limit the customer so I used a small piece of strip board shown above. At this point I had run out of the digital inputs/outputs. I could have used the Arduino Mega but as there were unused analog inputs I used those. All it took was a pullup resistor on each input and each switch forces the respective input to zero.

The program was written in Arduino's latest Interactive Development Environment. After the initialisation sequence, the program looks for switch presses and changes the points accordingly. the biggest headache was considering how swiftly to change the point motors. I've got a 2A 9V supply driving the system which is more than enough to drive all point motors at once. However, every time a point motor changes, there will be a spike on the supply as the relay switches and as the point motor kicks in. It is theoretically possible that having all point motors go at once could cause a spike on the microcontroller. Separate power supplies for the motors compared to the microcontroller would help but this is an overkill. All I did was put a 0.2 second delay between each point motor change. This will mean there is a series of smaller spikes over the time taken for all point motors to change.

In order to simplify the brain cells required to understand the program I have two constants in the program, STRAIGHT and CURVED. These apply to the point movement. I also set the standard that the normally closed contacts on the relay card drive the points to the STRAIGHT direction. 

The startup sequence is also interesting. If you configure the point motor input/outputs as outputs then the outputs are immediately driven low which causes all the relays to fire at once. I discovered that you can write the outputs high before you configure them and this value is latched internally and used on configuration so none of the relays fire on power up.

The power up issue still occurs if the points are left in random positions when you turn the power off. Thankfully there can never be all the points in the CURVED way at once. Whilst each switch only changes the minimum number of points needed to achieve the goal of a particular track, the switch for the middle track drives all the points to the STRAIGHT position so it can also serve as an end of day switch to put everything to a known state.

I did put in a 6th LED which blips every 5 seconds just to say the system is still working. It will also flash more vigorously if it gets a weird input from the switches but I realised I wrote the software in such a way that it doesn't look for anything other than the 5 individual switch presses.

 


The point motors themselves were mounted on a small square of 9mm ply using M2.5 screws as shown above. I wired in each point motor and then held it roughly in place and fired it in both directions to find the optimum place. Once satisfied, I drew round the block of ply and used the hot glue gun to stick the block to the underside of the baseboard. Once done, I felt the position wasn't optimal. The rod that drives the point was hard against the point at each face. The MP1s have an adjustable throw and I had set them all to the smallest one, 3mm, but it was still too much. Peco points have a throw of just over 2mm. I solved this by removing the point motor and using two pieces of 9mm ply instead of just the one. This meant there was some slop at each end but the point always moves. If you ever get the position wrong, the MP1 has adjustments in all 3 dimensions. It really is a clever piece of kit.

With a computer controlled system like this, you do have to ask yourself what you would do if it went wrong. If it isn't a simple wire falling out then the "at an exhibition" option is to drop out the point motors (most of them you can just drop out the control rod) and also disconnect the frog wires. At that point it reverts to the manual operation that was used at Maidenhead.

There it is. Approximately £40 of expenditure and a few extra hours to wire it up and debug the program. It gets its first outing at the Basingstoke show in March - a 2 day show that will give it a good test!

And to cap it all. I finished the fiddle yard yesterday and within an hour I got n email saying my MP1 point motors, ordered in November, have finally shipped!

New Year Update

It's been a busy few weeks on the railway front. First it was the SWOONs open day on the Saturday where I had my sales stand for STModel...