The 'magic' behind the curtain for CNC

This post is a lengthy review of what goes on inside a working DIY-

The 'magic' of the Windows-based MACH3 CNC software is a special dll
file that takes over the Windows timing to enable the CNC application
software to send consistent timed electrical pulse trains (on, off,
on, off, etc.) out different pins on the parallel port. Think of
standing next to a wall switch and turning the room light on and off
really fast many times and then be grateful for computers!

The parallel port was originally created to tell a printer what to
print. But it can also be hacked to do other things like move a DIY
CNC machine. It’s the same thing only different, right?

A parallel port has 25 pins. Pins 2 thru 9, 1, 14, 16, and 17 can be
used as outputs from the PC. Pins 10 thru 13, and 15 can be used as
inputs to the PC. Pins 16 thru 25 are grounds. It is a
simplification, but the voltage on the pins is typically 0 or 5 VDC,
with logic 1 'high' = 5 volts, and logic 0 'low' being 0 volts. Some
newer laptops that still have parallel ports only output 3.3 volts and
this can cause problems on legacy stepper drivers that traditionally
expect to see 5 volts to go 'high'. See
and other references.

Without going too deep, two parallel port output pins are used to
signal 'step' and 'direction' inputs to each stepping motor. A three-
axis machine can have three stepping motors (X, Y, and Z) and needs
six output pins to control the three motors. The other available
output pins (from the PC) can be used to operate other machine
functions like the spindle motor, coolant, vacuum, work-light, etc.
The input pins (to the PC) are used to signal machine axis home and
limit switch events and e-stop events back to the CNC application
software. Note all pins share a ground signal return path, but that
is an electrical detail.

Each 'step' and 'direction' signal pin pair from the parallel port get
wired to a stepper driver board (which is what James is working on) to
control a stepping motor. Power for the motor is also wired to the
stepper driver board. So the driver board has two 5 volt signal
inputs and the 24 to 80 VDC approx 2 amp power wiring.

The stepper driver board looks at the voltage on the 'direction'
signal and typically interprets 0 volts 'low' to mean clockwise
rotation and 5 volts 'high' to mean counter-clockwise rotation. Note
this setup assignment is arbitrary and the opposite case 0 volt 'low'
could mean CCW and 5 volt 'high' mean CW.

The stepper driver board also looks at the voltage on the 'step'
signal and typically interprets each state change, i.e. either 0 going
up to 5 volt 'rising' or 5 going down to 0 volts 'falling' as a

There is a confusing variety of 4, 6, or 8 wire uni-polar or bi-polar
types of stepping motors but they all function basically the same. It
is a simplification, but when powered-on, a stepping motor locks-up
and holds rotational position. Each 'step' signal then advances the
rotor 1/200th of a revolution (1.8 degrees). The rate of step signals
(on, off, on , off, etc,) establishes the motor's rotational speed.
The total quantity of step signals establishes how far the motor
rotates (1 step equals 1.8 degrees, 10 steps equals 18 degrees, 100
steps equals 180 degrees, 200 steps equals 1.0 rotation, 300 steps
equals 1.5 rotations, 400 steps equals 2.0 rotations, etc.).

The stepping motor contains multiple pairs of electrical coils which
lock-up and hold the rotor rotational position. The stepper driver
board has some low-level logic able to selectively energize adjacent
coils in the motor to cause the rotor to advance 1/200th of a
revolution CW or CCW for each 'step' signal. This is called 'full-
step' mode. You can have 'half-step', 'quarter-step', 'eighth-step'
modes, etc. by electrically proportioning the current between adjacent
coils to move the rotor a corresponding fraction of a full-step, but
that is a detail.

So now we are ready to put all this together in some examples.

We want our three-axis CNC machine to move the spindle from the 0.000"
position to the 1.000" position in the X-direction. Our X-direction
stepping motor has 200 steps/rev and drives a lead-screw that advances
1/5" (0.200") per revolution. We can do the math and see that one
step equals 0.001" of linear motion. Further, to make the 1.000" X-
direction linear move, we need to rotate the X-axis stepping motor
five full revolutions, or 1000 steps. We'll say parallel port pin 2
is assigned to be the X-axis 'direction' signal and pin 3 is the X-
axis 'step' signal.

The CNC application software knows all this (including current
position X=0.000", needed (+) direction to get to X=1.000”, and
intended linear move speed of say 10 inches/minute) from its setup and
reading of the current G-code instruction (ex: G1 X1.000 F10) in the
NC program. It sets pin 2 high or low for the needed direction and
ouputs a timed sequence of 1,000 on/off transitions on pin 3. The
stepper driver board gets these inputs and sequences the power to
energize the coils of the stepping motor that cause it to turn the
lead-screw that advances the X-axis from 0.000" to 1.000". It is all
a bit fancier that this, because the motion profile actually includes
an acceleration ramp-up-to-full-speed phase, a duration at full speed
phase, and a deceleration ramp-down-to-stop phase. This type of
motion profile is called 'trapezoidal' motion and there are other even
fancier motion profiles that are even better.

Now let’s decide to make a two-axis move simultaneously in X and Y
from the current (X,Y) position P1 (1.000, 0.000) to the next position
P2 (0.000, 0.500) at a speed of say 5 inches/minute. X will move in
the (-) direction 1000 steps and Y will move in the (+) direction 500
steps. The CNC application software reads the G-code move instruction
(G1 X0.000 Y0.500 F5) and knows to set the X and Y ‘direction’ output
pin voltages to the correct signal voltages and toggles the X and Y
‘step’ pin voltages at a 2-to1 ratio (1000 steps and 500 steps
respectively) since the X-axis has to go twice as far as the Y-axis
during the same motion profile. The X/Y step pulses occur half-as-
fast as the prior example since we have commanded a speed of 5 inches/
minute not 10 inches/minute.

It gets even fancier if we ask the CNC to interpolate a circular move
in the X/Y plane. The CNC application software is smart enough to
continuously coordinate the different step pulse trains in X and Y to
trace the commanded circular path.

Note that stepping motor drives as discussed above are described as
being ‘open-loop’. There is no ‘closed-loop’ feedback to confirm that
the motors are actually making the commanded moves as intended. If
the load is too great and causes a motor to stall, that motor is said
to ‘lose steps’. We count on working within an operating envelope
that is 100% reliable, start-to-finish on every move or else the
motion path and part we cut won’t come out right.

More sophisticated (and expensive) servo drive motors with encoder
feedback into sophisticated servo drive electronics are used for
higher performance motion control. But that is a future thread.

Enough for now... Thanks for hanging with me if you have read this