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.
The Location: Forests of the Pacific Northwest, OregonThe Year: 1948The Scale: On30The Blog: http://bvlcorr.tumblr.com
To be honest it was a few months, mostly due to the trial-and-error with trying to get a fully automatic version going. Once I decided upon the semi-automatic approach, it just took a few weeks. This is the sixth Arduino I have on my layout.
what is the angular resolution of your system? is it simply 0.11 (1.8 / 16) deg?
do you have a way to locate the zeroth position on the turntable for indexing?
i calculated the angle of the width of a rail (0.03") for different size turntables. it is 0.277 deg a 90' turntable, so the turntable would need to be finer than this to allow tracks to be laid arbitrarily
DiaFt DiaIn Circ Rail Deg Steps 72 9.9 31.2 0.030 0.346 1039 83 11.5 36.0 0.030 0.300 1200 90 12.4 39.0 0.030 0.277 1299 130 17.9 56.3 0.030 0.192 1877
greg - Philadelphia & Reading / Reading
It is 0.11 of a degree. As for indexing, I did have a hall effect sensor under the lead track at one point. However, I couldn't get the indexing to work properly with sensors that would minimise visual impact, so I went with the approach of aligning the bridge manually, then switching the track power automatically.
presumably you know that if you find a zero point on the pit, you can locate any track by simply counting steps once you know how many steps (stored in EEPROM).
i've had success using IR emitter/detector to precisely (to my eye) a point on the pit wall. Mel hinted at the idea using tubes. I believe the aperature to from the emitter and the detector need to be narrow. holes might do if they are vertically aligned.
I mounted them behind some styrene with a slot cut in the styrene. I actually mounted them about a 1/4" behind the slot but I think right up against it would be better, it blocks more of the lens.
Here's what i got when i just sampled the ADC. Can't tell you the width (time) of the null. But I did use it to detect the negative peak and stop the turntable by briefly applying reverse voltage with a DC motor.
if you tried this approach, you would have a zero necessary for an indexing system.
interested in talking more? pm me
Thank you, but counting steps was the first thing I tried. A hall effect sensor acted as the reference point. Thing is, I had to keep recalibrating it, as the step count only remained consistent for two or three goes before changing.
Also, I like to have a bit of a hands-on feel for my layout, as it's a backwoods logging operation. As such, manually aligning the turntable fits with the 'feel' of the layout.
tbdannyThing is, I had to keep recalibrating it, as the step count only remained consistent for two or three goes before changing.
that shouldn't happen with a stepper motor.
my experience with steppers is described today as "two-phase on full step". The polarity of each coil is alternately reversed. More torque results by energizing both coils instead of just one. you could alternately turn off a coil with less reliability. Microstepping increases the average voltage on one coil while decreasing the other using PWM.
it sounds like you lost alignment because the motor slipped, possibly because of being microstepped too quickly.
reading your post made me realize that you could full-step to get to the track and them apply microstepping at that final step to more precisely align the bridge with the track. You could micro-step during the first second of use.
i've had some success using a very small geared stepper motor with a 1 lb machinist block sitting on the bridge. the 18 deg stepper, 63:1 gearhead and 200:1 turntable gearing make the resolution 0.0014 deg. I could hear it slipping, but a few slips at this resolution wouldn't matter too much.
presumably, it could be re-zeroed whenever it passed the optical sensor.
the problem with this stepper is it takes 6.4 minutes for a complete revolution by stepping once every 1.5 msec, the fastest it worked reliably
my experience with the latest Walthers turntable is on a layout that is high enough to work under sitting in a chair. The indexing was essential because it was diffcult to see the alignment. We were also busy enough that you would program the TT and do something while it turned.
I think the speed may have been the problem. I was going for something that would turn inside of a couple of minutes, to avoid slowing down operations.
Part 2: Installation & Misadventures
With the subassemblies tested and working, I was ready to install the stepper motor for the turntable itself.
I started by modifying the turntable bridge. The base of the bridge is a separate piece, which connected to the original drive mechanism via a gear screwed to it. After removing the original drive mechanism, I separated it into its components on my workbench. To fit the stepper motor, I enlarged the holes in the centre of the circuit board, turntable base and the washer beneath it.
These were initially 2mm diameter. In order to enlarge them while maintaining accuracy, I drilled them out with a 2.5mm drill bit, then 3mm, and so on until I reached 5mm. This is the diameter of the driveshaft. At this point, I put the turntable base over the driveshaft of the motor, and used this as a guide to mount the driveshaft adapter.
With that done, I enlarged the hole in the middle of the turntable pit the same way. When it got to 5mm in diameter, I mounted the motor below, using some 18mm plywood and a 3D-printed mounting bracket I'd bought off Shapeways. My original plan had been to just put screws through the turntable base, to hold the motor flush. However, these would have been visible, and would have fouled the bridge.
In order to allow the motor to clear the spring contacts which provide track power to the bridge, I glued two strips of 3.2mm square styrene along the front and rear of the 3d printed mount. This allowed it to sit flush while leaving space for the wires.
After mounting the motor, I removed it and enlarged the hole to 6mm. This provided sufficient clearance for the driveshaft to move freely. I also took this chance to clean up the two spring-loaded contacts that provide track power to the bridge, via the circuit board. They'd gotten a bit of paint on them when I scenicked the turntable base, but this hadn't affected the functioning of the turntable.
With that done, I reinstalled the motor and put the base on the driveshaft. I then put the bridge on the base, without reconnecting the wires, and performed a load test with my heaviest locomotive on the bridge:
With that sorted, my next step was to test the track power by connecting a multimeter to the wires on the base, setting it to measure AC voltage, and sending the motor for a spin. To my surprise, I found that it lost power for about half of the rotation. After slightly sanding down the wood washer that sits between the base and the turntable pit, this issue was resolved.
At this point in construction, I was still going for the fully-automatic approach. As such, I installed a magnet at one end of the turntable bridge to trip a hall effect sensor. Once the glue for it had dried, I filled in gaps around it with putty.
I installed the Arduino, XBee socket, power supply and motor driver boards in a case to be mounted under the layout. At this point, I thought that the system was ready to go. So I wrote a simple sketch to perform basic calibration. I.e. to rotate until the hall effect sensor detected the magnet, then stop.
The first problem I encountered was that while the motor was turning, the LED display flickered. This was where I learned that electrical motors are noisy, and can interfere with other components on the same power supply. For this project, I had actually purchased a 5v 4A power supply, as well as a 12v 4A power supply. As such, I added a second socket, and connected this directly to the A4988's motor input supply. I then removed the voltage regulator board and wired the original socket to the +5V and ground connections to the other components. I then labelled them on the outside of the case, to ensure there were no accidents.
With that sorted, the LED display worked properly. At this point, I started running into problems. I wrote a sketch to count how many steps the motor had moved, and used this to count how many steps it was from the hall effect sensor to each track. However, I found that I was constantly recalbrating this. Owing to the way my tracks had been installed, I needed to have the A4988 motor driver working at 16 microstep resolution to get proper alignment without derailments. However, as it turns out, counting microsteps is not an accurate way of measuring the position of a stepper motor.
After learning this, I tried putting sensors at each of the tracks. I tried a few different types, but the accurate ones had to be visible and stood out visually, and the concealed ones weren't accurate enough. At this point, I took a step back and looked at what I wanted to accomplish. This was when I realised I wanted to have a hands-on element to it, and decided upon the semi-automatic approach shown in the first video in part 1. With this decided, I made the appropriate modifications to the control panel, then wrote the software accordingly. This software will be covered in part 3.