Turntable VIII: Microstepped

Trying to get the turntable to rotate more slowly while remaining smooth turned out to be a challenge. When slowed down, it began ticking like a clock with enough torque that it would likely have flicked an engine off the deck. My brother-in-law and I spent a couple of hours last night trying to figure out a solution, to no avail. After that I stayed up way too late digging through Arduino forums, trying to figure out how to add a microstepping sequence to break up the “tick-tock” movement into much smaller waves. I went to bed thinking all that research was for nothing.

When I woke up this morning, though, I had the sudden idea that maybe the Adafruit motor shield has a library with a microstepping sequence already loaded. This would save us from having to make calculations and then pull in (or invent) strings of compatible code from other sources. So, after a quick reread on Adafruit’s informational website I verified that, indeed, the Adafruit is already programmed to run a microstep sequence.

Lo and behold, all I did was replace instances of “Single” with “Microstep,” uploaded the code to the Arduino, and suddenly the turntable is slow moving and smooth.

Here’s a video of the turntable moving slowly and smoothly.

I think we can adjust the code further to give the turntable a more realistic appearance by accelerating and decelerating the deck every time it moves. For now, though, I am happy with how it functions, so am posting the code here in case anyone else can make use of it:

#include <NmraDcc.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>

#define DCC_ADDRESS 6 //Change accessory address here.
NmraDcc Dcc ; //Declare DCC Shield.
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); //Declare Motor Shield.
bool firstloop = true;
//Comment: getStepper(steps, stepper#)
//Stepper# is which port the motor is connected to. If using M1 and M2, indicate port 1. If using M3 and M4 indicate port 2.
//NEMA 14 Stepper Motor has a 0.9 degree/step. 360/0.9 = 400.
Adafruit_StepperMotor *myMotor = AFMS.getStepper(400, 2);

//This function is called whenever a normal DCC Turnout Packet is received.
void notifyDccAccTurnoutOutput( uint16_t Addr, uint8_t Direction, uint8_t OutputPower )
Serial.print(“DCC Turnout Packet Received\n”);
if ((Addr == DCC_ADDRESS) && OutputPower){
Serial.print(“Move 180 degrees\n”);
myMotor->step(200, Direction, MICROSTEP); //Move 180 degrees in the specified direction.
} else if ( (Addr == (DCC_ADDRESS + 1)) && OutputPower){
Serial.print(“Nudge 1 step\n”);
myMotor->step(1, Direction, MICROSTEP); //Move 1 step in the specified direction.
delay(200); //Wait 200 ms for debounce.

//setup(): This is executed first and only one time.
void setup()

while(!Serial); //Wait for the USB device to enumerate.
Serial.print(“Start Setup\n”);
AFMS.begin(); //Create with the default frequency 1.6KHz.
myMotor->setSpeed(0.025); //Set speed for 1/2 rotation per minute.
Dcc.pin(0, 2, 1); //Setup external interrupt, the pin it’s associated with that we’re using, and enable the pull-up.
Dcc.init( MAN_ID_DIY, 10, CV29_ACCESSORY_DECODER, 0 );//Call the main DCC init function to enable the DCC receiver.

Serial.print(“Setup Over\n”);

//loop(): If the DCC function detects a signal, it will call notifyDccAccTurnoutOutput on its own.
void loop()
if (firstloop) { Serial.print(“First Loop\n”); firstloop = false;}
Dcc.process(); //Calling this method makes the DCC shield check for new communications. It must be frequent or a message may be missed.
//myMotor->step(200, FORWARD, DOUBLE); //Move 180 degrees in the specified direction.

Turntable VII: Motorized!

The stack is in place and wired up.

Initially I had some issues loading the code into the Arduino, and finally worked it out with my brother-in-law’s guidance. I’d been loading it correctly, but a command was missing that caused the motor to remain unresponsive. Once that was sussed out the turntable started working without much of a problem, at least while it was powered via USB.

An issue arose when I powered the Arduino with 12V DC. When the motor was running on that voltage, it got sizzling hot and moved erratically when activated. However, when I switched back to the USB cable (which is regulated at 5V) it worked perfectly fine.

We are working on that, and then we will be keying in a slower speed. Ideally the deck will rotate at 1 rpm, but it is currently closer to 4-5 rpms. Overall, though, I am very excited and relieved that it is able to run!

Here’s a video of the turntable in action. You can see both the 180-degree rotation as well as the 0.9-degree nudge (just in case the deck goes out of alignment).

Edit: I think I’ve pinpointed the issue with the DC supply. While the motor indicates that it is a 12V motor, to be safe we should have calculated Ohms Law, which tells us that a  source should never exceed I=V/R (or “Current” equals “Voltage” divided by “Resistance”).  Any more than that and the motor will draw too much when idle (it draws the most voltage while holding). My brother-in-law and I both assumed that the motor shield would regulate the voltage.

For my motor (as per the specs listed on the packaging):

R=17 ohms
I=8.5 volts

Thus, according to Ohm’s Law we don’t want the voltage to exceed 8.5 volts. I guess that is what the motor specs mean:

“Each phase draws 500 mA at 8.5 V, allowing for a holding torque of 7Ncm (10oz.in) … Rated Current/phase: 0.5A … Phase Resistance: 17 ohms.”

To be honest, that’s mostly Greek to me, but the numbers align with my math.

I’m not too concerned with torque (because there isn’t much weight or resistance on the turntable) so 5 volts should be plenty. The motor has been working just fine on the 5V USB, but I would prefer a straight DC supply because I may decide to add a USB extension between the USB port and the fascia.

Turntable VI: Programming the Stack

Between random Saturday chores, I soldered the pins of the Adafruit Motor Shield. Prior to that, I had to squeeze the shield sideways for the Power light to signal. No big deal; just a bit more soldering than I expected.

From the DCC shield, I also pulled off the vestigial jumpers and those that would route power differently than needed. A few of the pulled jumpers were educated guesses, and (given the “educated” bit) I followed my brother-in-law’s lead.

The Arduino and both shields are now fully powered using either DC or USB. I had the option of powering it all from the DCC bus via the DCC shield, but I’m nervous about how much it would draw and possibly affect the performance of locomotives and switches. Thus, there will be two DC cables powering the layout.

My brother-in-law also had a great idea: I could add a USB extension that would lead to a panel on the front of the module, so I can simply plug my computer into that to program the turntable. In the future I may do that.

Anyhow, I loaded my brother-in-law’s sketch_turntable document into the Arduino library, so as far as I know the Arduino is now programmed.

Theoretically, choosing “Accessory 6” on the PowerCab should select the turntable; then pressing 1 or 2 will move it clockwise or counterclockwise 180 degrees. Choosing “Accessory 7” will also select it, but subsequently pressing 1 or 2 will only prompt it to move a single click.

We aren’t too optimistic about it working at this point, but I plan on wiring it up tomorrow to find out.

Edit: Once we have the code finalized, I will post it on this blog for safekeeping, and to share with others who may be interested.

Turntable V and Engine Shed Track

I spent about an hour soldering feeders to the turntable, and then soldered feeders to the track that will eventually lead from the turntable to the engine shed. It all works flawlessly.

Now, every inch of track is powered.

Next up, I will be continuing to work with my brother-in-law to configure the Arduino shield-stack, then will start programming.

Functionality of the Arduino will be super-simple (although I could make it more complex later on). The turntable will be selectable as an accessory (just like the switches), and will be able to turn 180 degrees clockwise and counter-clockwise with the push of a single button on the PowerCab.

By selecting a different address, the turntable will enter “nudge” mode. This way, if I need to align the deck with the tracks, I can nudge it a single click clockwise or counter-clockwise (the stepper motor moves 400 clicks with every rotation.

I’m hoping to start programming the Arduino tomorrow.