A Semi-Automatic Turntable: Part 1
Part 1: Intro & Subsystems
For a while now, I've been considering using an Arduino in order to automate the operation of the turntable on my layout. After considering several 'bolt-on' additions, I realised that trying to add indexing to the existing, DC-motor mechanism would be cumbersome. As such, I decided to replace the drive mechanism with a stepper motor, and use that for indexing.
My original plan was to just add indexing, with a keypad to select the desired track. But then I realised that I could take it a step further, and make it fully automated. This didn't quite work out, and I instead ended up with a semi-automatic version. This is how it ended up working:
I decided on this approach for two reasons:
1) I couldn't find sensors which would give me the accuracy required while being hidden. That is, there was always a trade-off between accuracy and visual impact.
2) My layout is a backwoods operation, with operating ground throws to change the turnouts.
As such, I wanted to have a hands-on element to the turntable's operation. While testing the fully automatic version, I found that I felt a bit 'disconnected' from what was happening. So in this series of posts, I'll be covering how I built this final version, and some of the things I learned along the way.
This first part will cover the construction and testing of the subsystems that make up the turntable controller. Some of these were built or adjusted after I'd decided to go from a fully automatic to a semi-automatic system.
I started by working out how to operate the stepper motor, a 12-volt NEMA-17 motor. This was the key to the whole system, and I'd never used one before. In order to drive it, the Arduino controls an A4988 chip. This is the red board in the photo above, with a silver heatsink on it. It takes a control signal from the Arduino, and a completely separate power supply for the motor itself. The only additional component required is the capacitor, to protect the inputs for the motor power supply, as well as a 10K ohm resistor to hold down the motor step pin when not in use.
The A4988 offers the option to drive the motor in 'microsteps', in which each pulse moves the motor by a fraction of a full, 1.8-degree step. Pins MS1, MS2 and MS3 on the A4988 are used to set which fraction is used. Setting them high, in various combinations, allows the A4988 to drive the motor in increments as little as 1/16 of a step. At this point in the build, I wasn't sure what resolution I would need. As such, I added a 4-way DIP switch between these three pins and the +5v rail, to allow me to try them. One of the switches from the DIP switch wasn't used.
Working from what I'd worked out on the breadboard, I built a motor driver board to be used in the final build.
My next step was to test that the motor had enough torque to move my locomotives. My heaviest locomotive is my boxcab, which was built on an Athearn blue-box mechanism. As such, it weighs in at around 450g.
I cut a length of wood to the same length as the turntable bridge. After finding the centre, I attached the driveshaft adapter I'd had 3D-printed by Shapeways. This fits around the shaft of the stepper motor, with a flat section where the drive shaft is flattened. It took a few tries at different sizes before it fit perfectly.
To simulate the load, I used a box of old motors I'd bought at a sale at my model train club. They were the only thing I had to hand which were heavy enough. I taped them to the top of the board, until it weighed 500g (for a bit of wriggle room). I then placed it on the end of the driveshaft.
The motor was able to move the load without any problems. With that confirmed, I started working on the components for the control panel.
The original, fully-automatic design had two 3mm LEDs on it, one red and one green. These were to have been 'stop' and 'go' signals for when the automated turntable was operating. The other component for the control panel was a 4-digit LED display to show the address of the currently selected locomotive. Owing to the change in focus for this project, the design of the control panel changed slighlty between the initial and final versions. But before I could build it, I needed to build the 4-digit LED display.
I'd done one of these on the base station for my DCC system. On that occassion, I'd made the display from four individual 7-segment displays, driven my a MAX7219 LED driver chip. This time around, I decided to use a 4-digit display, driven by the same chip. With the four digits in the one package, it only needs 12 connections for full functionality. I wired the 4-digit display to the MAX7219, leaving out the connections for the decimal points between the digits. They weren't needed.
To test it, I wrote an Arduino sketch (program) that counted up to 10,000 in 0.01 second increments and ran it. I was able to re-use a function I'd written for the base station, which will take a number up to 9,999 and display it across the four digits of such a display.
With that done, I was able to build the control panel itself.
When I decided to go from automatic to semi-automatic operation, I replaced the red LED on the panel with a single-pole, double-throw momentary contact switch. This is used to turn the turntable, via the stepper motor. I'd used a length of Cat5 network cable to connect the control panel to the Arduino, to keep things organised. This had a spare wire left in it. As such, I was able to wire the switch to the ground connection, then use the original wire for the red LED and the spare wire to connect to each side of it.
In order to get the program to work properly, there are a couple of points at which it pauses to prevent a false triggering while the locomotives move on and off the turntable. After initial testing, I realised it would make things clearer if there were an indication of when these pauses were occurring. So I added a yellow LED to the control panel, to indicate when the system was active. If this LED is on, then the turntable can be turned, locos can arrive and depart, etc. If it's off, then the system is paused.
Once completed, this control panel was installed in place of the original control panel on the fascia. The original panel had just had two switches, a DPDT rocker to control the DC turntable motor, and a 12-position rotary switch to select track power.
Next up was the occupancy detector, to determine when a train was on the bridge. The design of this turntable provides constant power to the tracks, with an auto-reverser reversing the polarity as needed. After a bit of experimentation, I found that wrapping the wire at least four times through the coil was enough to allow it to detect the current of a sound decoder at idle. At least, that's what I thought.
It's the first time I've used a coil like this, and it was sold amongst other Arduino modules. As such, I thought I would be able to plug it straight into an analog pin of the Arduino, and take a reading from that. This was not actually the case. As DCC is very close to AC current, I ended up getting several values from the coil over the course of a second, including 0, which would create false negatives. After asking about this on the Arduino forums, I learned that the way these coils work is by generating AC current, in response to the current going through them. I thought they just sensed it. As such, I'd accidentally been putting 38v of AC into the analog pin I was using, and had damaged it.
In order to use the coil for DCC occupancy detection, some supporting circuitry is needed. I found this article here, outlining how to build a sensor out of such a coil. I didn't have the exact same transistor, and instead used a BC548 NPN general-purpose transistor. The yellow wires off the board go to the two sides of the coil, and the green one goes to the Arduino.
Once I'd built this, I tested it with my locomotives before connecting it to the Arduino. This was where I discovered something interesting. The circuit is designed to give a digital output, in which anything less than 1.5v on the Arduino pin is counted as a 0, and anything above it as a 1. When testing it, I found that it produced an output of 3.7 volts when no locomotive was present, and that this dropped when one was detected. However, about half of my locomotives only dropped it to a value above the 1.5v required for a digital 0, yet less than the 3.7v of the 'nothing detected' state. As such, I connected it to another analog pin on my Arduino. These voltages translated to an analog read value above 900 when nothing was detected, and below 900 when something was. Thus, I used an analog reading with a threshold of 900 in the program function to detect occupancy.
At this point, the next item to be tested was a socket for an XBee wireless module. I'd already used these to make my DCC system, as well as to transmit the address of the incoming locomotive to my automated staging controller. As such, I pulled out a spare XBee, and configured it identically to the one for the staging controller. It'll be used for the exact same purpose, receiving addresses when a locomotive is dispatched. I tested it by connecting it to the Arduino, then rigging up the Arduino to display the received locomotive address on the LED display.
The next step was to prepare the Arduino shield. I usually use prototyping shields for the connections to the Arduino, as this means that I can just unplug the shield and pull the Arduino out if any software updates are needed. I've found it's easier to solder two wires together under the layout, than it is to solder to a shield. So I added small lengths of decoder wire to each output.
My turntable has 11 tracks around it, and I didn't have 11 spare pins on the Arduino. Instead, I used a 16-channel multiplexer with channels 1-11 wired to a bank of relay switches. I started counting at channel 1 instead of 0 in order to make the software code a bit simpler. The resistor on pin 4 is 220 ohms, and is connected to the green LED on the control panel. At this point, I hadn't added the yellow LED to the control panel. When I did, I added a 220 ohm resistor to pin 13, and connected this LED in there.
The other component of note is the variable resistor connected to pin A1. I was originally going to have a light detecting resistor in the turntable lead track, to trigger the Arduino when a train was leaving or arriving. However, after getting the occupancy sensor working reliably, the LDR was no longer needed.
The final building block was the relay bank. I'd bought a 16-relay module off eBay, and I installed it behind the fascia, next to the control panel. The wires from the original 12-way rotary switch to the tracks came out here, so by putting it in this position, I didn't have to do too much in the way of rewiring. Each track was wired to the normally-open contacts on the first 11 switches, with wires from the common side of the relay going to the track bus. The multicoloured ribbon cable on the far side of the relay bank goes to the Arduino, with two wires for the power supply and the other wires for the track control.
With all the building blocks worked out, the next step was to install the stepper motor and modify the turntable. This will be covered in part 2 of this writeup.