## Background

For those readers who want more information on the process I used to design the drive mechanism for my turntable project, read on.

The KATO roundhouse has three stalls whose centerlines are spaced 10 degrees apart. We want the stepper mechanism to be able to precisely line up with these three tracks, but to do that the mechanism’s steps per revolution must be a multiple of (360° ⁄ 10°) or 36. None of the stepper motors’ step counts divide evenly by 36, so I need to use a gear or pulley mechanism to change the steps per revolution at the turntable side.

There are three variables which I can adjust to set up the gearing ratio:

- Number of steps per revolution of stepper motor
- Diameter of motor’s pulley or number of teeth in its gear
- Diameter of turntable’s pulley or number of teeth in its gear

To prevent slippage from causing errors in the turntable’s positioning, I am using a toothed belt to connect the motor and turntable, so I have to select the tooth counts of two gears. The belt is an MXL belt which has a pitch (distance between teeth) of 0.08 inches. I also have two small MXL-compatible gears to choose from for the motor side, with 18 or 32 teeth.

For the stepper motor, I have three different stepper motors on hand.

From left to right in Figure 2:

- An 8-wire geared stepper motor with 480 steps per revolution, with a pair of external gears I could use to change the effective resolution to 2400 steps per revolution, or 0.15 degrees per step. Unfortunately, I’ve had this motor for at least 35 years and no longer have the wiring diagram, and it’s too old to be found on the internet. It’s also quite heavy for its size.
- A newer 4-wire NEMA-17 stepper motor with 200 steps per revolution. Also a bit large and heavy. This motor and the 8-wire one are both designed to be driven with 12 volts. NEMA-17 steppers are commonly used in 3D printers and CNC machines. I got mine from Adafruit.com.
- A 5-wire geared stepper with 513(!) steps per revolution. This particular device is available both in 5 volt and 12 volt versions from Adafruit.com. Mine is the 5 volt unit.

That last one, with 513 steps per revolution, surprised me at first. 513 is one greater than 2 to the 9^{th} power, and powers of 2 are near and dear to the hearts of many diehard computer geeks. At first I thought 513 was a prime number, but I was wrong. This turned out to be a good thing later on.

**NOTE:** As of January 2021, the Adafruit geared steppers being sold have 4 times as many steps per revolution, 2052 instead of 513. The calculations below can be adjusted accordingly.

## Calculations

To get the smallest steps possible on the turntable side, we want the motor resolution high, the motor’s drive gear small, and the turntable’s gear large. Let’s call the number of teeth in the motor’s small gear *SG*, and use *LG* for the turntable’s larger gear.

Obviously both *SG* and *LG* are whole numbers, because you can’t have fractional teeth in a gear. But the gear ratio *GR* = (*LG *⁄ *SG*) can be fractional; a 3:2 or 1.5 gear ratio would be perfectly reasonable. Let’s also use *SC* for the motor’s step count per revolution. So if we connect a gearing mechanism to our motor, we can move the turntable with a new step count *TC* = *SC* × *GR* = *SC* × *LG* ⁄ *SG*. It’s *TC* that we now want to be a multiple of 36 to match the roundhouse dimensions.

I can’t find any larger gears for the turntable side which are compatible with the timing belt **and** have high enough tooth counts to give me a usable *TC*, so I will 3D-print a custom one with the number of teeth I want. So how many teeth *do* I want?

The step count *SC* is either 480, 2400, 200, or 513, depending on which motor I use, and in the case of the old 480-step motor, if I include the external 5:1 pair of gears. More steps is better, you would think. To get the highest gear ratio, I’ll use the smaller 18-tooth gear on the motor side, so let’s define *SG* to be 18. *TC* must be a multiple of 36, so I need to find suitable values for *LG*, the number of teeth in the turntable’s larger gear.

I have two more constraints I need to consider. First, my 3D printer’s print surface is 220 mm square, or about 8.7 inches in the X and Y directions. So I can’t create a large gear which won’t fit within this area. Also, I need the turntable’s step angle to be (much) smaller than the width of the rail heads at the ends of the turntable’s bridge, so that I can fine-tune the positions where the turntable will need to align with the tracks around its perimeter.

Most N-gauge track uses rails which are 0.025 inch or 0.635 mm thick at the top (the rail head). The turntable bridge is 151 mm long, so the radius is half that or 75.5 mm. So the end of each rail sweeps an angle of (0.635 ⁄ 75.5) ≅ 0.0084 radians ≅ 0.48°. I want the angle moved for each turntable step to be a fraction of that angle, say 0.10°. That will require a larger gear ratio to reduce the turntable step size, but not so large that the gear becomes too big for the printer.

After a few failed attempts at finding workable gear ratios caveman-style (pencil and paper), I finally wrote a Python program which lets me set the initial parameters, and it then searches for sizes of the large gear which meet all my constraints. You can find the source code at the bottom of this page.

First I ran the Python code using the 18-tooth small gear and the 200-step motor (middle one in Figure 2), and I found the following possible solutions.

```
Large Diameter Diameter Gear Step Steps/ Railhead
Gear (mm) (in) Ratio (deg) Stall Fraction
----- -------- -------- ----- ------ ------ --------
81 52.4 2.06 4.5 0.4000 25 0.8301
162 104.8 4.13 9.0 0.2000 50 0.4150
243 157.2 6.19 13.5 0.1333 75 0.2767
```

Out of curiosity I ran the code again using the 18-tooth small gear but with the (what I thought was a prime number) 513-step motor.

```
Large Diameter Diameter Gear Step Steps/ Railhead
Gear (mm) (in) Ratio (deg) Stall Fraction
----- -------- -------- ----- ------ ------ --------
48 31.0 1.22 2.7 0.2632 38 0.5461
72 46.6 1.83 4.0 0.1754 57 0.3641
96 62.1 2.44 5.3 0.1316 76 0.2730
120 77.6 3.06 6.7 0.1053 95 0.2184
144 93.1 3.67 8.0 0.0877 114 0.1820
168 108.7 4.28 9.3 0.0752 133 0.1560
192 124.2 4.89 10.7 0.0658 152 0.1365
216 139.7 5.50 12.0 0.0585 171 0.1214
240 155.2 6.11 13.3 0.0526 190 0.1092
264 170.8 6.72 14.7 0.0478 209 0.0993
288 186.3 7.33 16.0 0.0439 228 0.0910
```

*Eleven solutions?* That’s when I looked more closely at the number 513 and realized it’s not prime at all, it’s equal to 3×3×3×19. (I should have noticed that earlier; because its digits add up to 9, it means 513 is a multiple of 9.) As a result, its much higher resolution gave me 11 solutions compared to the 200-step motor’s 3 solutions. This means I can use the more compact, lighter weight motor after all. Woohoo!

## Python code

Below is the Python 3 script I used to search for valid combinations of gear sizes and stepper motor step sizes to work on my railroad turntable project.

`import math # Script update 2018/9/21 by Thomas W Rackers # Improved formatting of results. # Script updated again 2020/4/23 # Even better formatting of results. :) def millimeters(v): return v * 25.4 def inches(v): return v / 25.4 def make_gear(pitch): return lambda x : x * pitch / math.pi # Adjustable parameters stepper_count = 513 # stepper resolution, counts/revolution small_gear = 18 # tooth count on small pulley`

`# Fixed values roundhouse_delta = 10 # angle between roundhouse stalls, degrees belt_pitch = millimeters(0.08) # MXL belt pitch, mm rail_width = millimeters(0.025) # N gauge rail head width, mm rail_length = 151 # rail length, mm max_dia = 200 # max large gear diameter, # limited by 3D printer print volume`

`# Define belt gear generator with known belt pitch. gear_dia = make_gear(belt_pitch) # gear_dia(teeth) will return diameter in mm.`

`# Angle between stepper steps, degrees stepper_delta = 360 / stepper_count`

#`Effective diameter of small gear, mm small_gear_dia = gear_dia(small_gear)`

#`Arc width of rail end, degrees rail_arc = math.degrees(rail_width / (rail_length / 2))`

#`Header and format strings hdr1 = "Large Diameter Diameter Gear Step Steps/ Railhead" hdr2 = " Gear (mm) (in) Ratio (deg) Stall Fraction" hdr3 = "----- -------- -------- ----- ------ ------ --------" fmt = "{:5} {:8.1f} {:8.2f} {:5.1f} {:6.4f} {:6} {:8.4f}"`

# Print header.`print(hdr1) print(hdr2) print(hdr3)`

`# Try sizes for large gear from small gear's size up. for large_gear in range(small_gear, 18 * small_gear): # Stop if we will exceed 3D printer capacity. large_gear_dia = gear_dia(large_gear) if large_gear_dia > max_dia: break`

`# Calculate the gear ratio.`

`gear_ratio = large_gear / small_gear`

`# Calculate the turntable's step size (degrees).`

`turntable_delta = stepper_delta / gear_ratio`

`# If the turntable steps are not less than the arc of`

`# the end of a rail head, skip this one.`

`rail_widths = turntable_delta / rail_arc`

`if rail_widths > 1:`

`continue`

`# Calculate number of steps between roundhouse stalls.`

`roundhouse_steps = roundhouse_delta / turntable_delta`

`# Number of steps must be a whole number.`

`if roundhouse_steps == int(roundhouse_steps):`

`print(fmt.format(large_gear, large_gear_dia,`

`inches(large_gear_dia), gear_ratio,`

`turntable_delta, int(roundhouse_steps),`

`rail_widths))`