Sunday 26 May 2019

Pennstadt/Valdorf and Arduinos

I have added an Auhagen level crossing (Schrankenlage) to the reverse loop as a means of getting from one side to the other. As I have been a programmer for more years than I care to mention, I thought that I could automate it using an Arduino. My son-in-law bought me a set when I retired but I have never seemed to have a reason to pull it out and have a go. This project seemed ideal for it but the box is buried in our storage room. I therefore went to my friendly Amazon account and bought an Elegoo Arduino starter set. This comes with an amazing set of accessories and all for £25. I soon got onto messing around with the Servo and the light dependent resistor (LDR) projects. Once I had both cracked, I worked on putting them together. 

Firstly, here is the level crossing in situ.


The kit comes with holes ready for wires to raise the arms so that wasn't difficult. However, one of the pivot pins broke off so I had to drill the arm out to accept a brass rod. Once they were operating, it was a case of working  out how this all would work. The reverse look only accepts trains in one direction. They comes out of staging, travel around the oval and traverse the reverse loop to enable them to go back into staging. Hence, I only have to manage trains going in one direction.

One LDR is buried into one of the approach tracks, just sitting level with the surface of the track. This is connected to an analog input to the Arduino (using a potentiometer layout if that means anything). This means that the LDR gives a reading of between 0 and 1023 depending upon the amount of light falling on it. This is mapped onto a range of 0 - 179. Originally, I then split the range into 0 - 90 and 91 - 179 with 0-90 being lit and 91-179 being darkness. 

Once the LDR goes dark (a train is over it), the gates lower. When the LDR goes light, the Arduino waits for 5 seconds - to give the train time to cross the level crossing - and the gates raise. The action is all achieved by having two servos attached to the digital outputs of the Arduino.

This works very well and, given that I have never used the C language before, was quite easy to do.  Here is a little demo of the gates before I installed them onto the layout.



The crossing gates are now fitted into place with an Arduino under the table. As I didn't want to use the Arduino that came with the starter pack - it uses jump leads so the connections are not too secure - I obtained a set of three Arduino Pro-Mini versions. This requires that all the leads be soldered in place, which was quite easy - using a flux pen made it easier. Once wired up, the Pro-mini has a mini USB socket for programing.  Here is a run showing it in operation. You may notice a couple of problems!


Firstly, the LDR is mounted too close to the crossing so the gates haven't quite cosed when the loco crosses. This is simply changed by moving the LDR another 6 inches further away on the curve. The second problem is more difficult to fix. It appears that the LDR is very sensitive and is seeing a change of light across the defined threshold when the coaches are passing over it. Fixing this will be an interesting bit of coding as I am going to have to recognise the spurious reactions. I think that it is a case of counting the reactions and if the light threshold is broken for too short a time, then the gates will not move. I am new to C programming (I have been using other, higher level languages since 1976 so I have some learning to do).

If you are interested, the code is available as a PDF - HERE

No comments: