NC and CNC part programming

NC, CNC and DNC, machine axes and zeros, program structure, G- and M-codes, circular interpolation, cutter compensation and stepper-drive BLU, with tool-path, machining-time and pulse-frequency numericals.

Drafted with Aria, reviewed by the AiCanCode.org team. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Every CNC lathe, machining centre, laser cutter and 3D printer runs on a part program, so writing, reading and checking G-code is a daily task for a manufacturing or mechatronics engineer. The same program also links design to production: the CAM system writes it, the controller interpolates it, and the servo or stepper drives turn it into tool motion. Small mistakes, such as a wrong sign, a missing decimal point or absolute in place of incremental, crash machines and scrap parts.

Key ideas

NC versus CNC versus DNC

  • NC (numerical control): the controller is hard-wired logic that reads the program block by block from punched tape; there is no stored program, so the tape is read again for every part and editing means punching a new tape.
  • CNC (computer numerical control): a dedicated computer in the controller stores the whole program in memory, allows editing at the machine, does interpolation, tool offsets, canned cycles and diagnostics in software, and supports subprograms and parametric programming.
  • DNC (direct/distributed numerical control): several CNC machines connected to a central computer that downloads programs and collects production data.

Elements of an NC system: the part program, the machine control unit (MCU, with data-processing and control-loop units), the drive system (servo or stepper motors with ball screws) and the machine tool itself. In open-loop control a stepper motor is commanded pulse by pulse with no position feedback; in closed-loop control an encoder or linear scale feeds the actual position back to the controller.

Basic length unit (BLU): the smallest axis movement the controller can command, i.e. one pulse. It sets the programming resolution.

Axes and coordinates (right-hand rule): Z is the spindle axis, positive away from the work; X is the principal horizontal axis; Y completes the right-handed set. Rotary axes A, B, C turn about X, Y, Z. A programmer assumes the tool moves and the work is stationary, whatever the machine actually moves.

  • Machine zero (reference point, fixed by the builder), work zero / program zero (set by the programmer via work offsets such as G54) and tool reference point.
  • Absolute (G90): every coordinate is measured from the work zero. Incremental (G91): every coordinate is the distance from the previous point.

Program structure: a program is a sequence of blocks, each a line of words, e.g. N40 G01 X60.0 Y25.0 F150 S1200 M03. Word addresses: N sequence number, G preparatory function, X/Y/Z coordinates, I/J/K arc centre offsets, F feed, S spindle speed, T tool, D/H offset numbers, M miscellaneous function. Most G-codes are modal: they stay active until cancelled or replaced.

Common G-codes (ISO 6983 style)

  • G00 rapid positioning (no cutting); G01 linear interpolation at feed F.
  • G02 circular interpolation clockwise; G03 counter-clockwise, viewed from the positive end of the axis normal to the plane (from +Z for the XY plane).
  • G04 dwell; G17/G18/G19 select the XY/ZX/YZ plane; G20/G21 inch/mm input.
  • G28 return to reference point; G40 cancel cutter radius compensation; G41 left, G42 right compensation (tool to the left or right of the path, looking in the direction of travel); G43 tool length compensation.
  • G54–G59 work coordinate systems; G81–G89 canned cycles for drilling, peck drilling, tapping, boring.
  • G90 absolute, G91 incremental; G94 feed per minute, G95 feed per revolution (typical for lathes); G96 constant surface speed, G97 constant rpm.

Common M-codes: M00 program stop, M01 optional stop, M02 end of program, M03 spindle on clockwise, M04 counter-clockwise, M05 spindle stop, M06 tool change, M08 coolant on, M09 coolant off, M30 end of program and rewind.

Circular interpolation: an arc block gives the end point and either the radius R or the centre offsets I, J (K) measured from the arc's start point to its centre, incremental in most controllers even when G90 is active.

Cutter radius compensation: the programmer writes the part contour; the controller offsets the tool centre path by the cutter radius stored in the offset register. The same program then works after tool wear or a change to a different diameter. Climb milling of an outside contour clockwise uses G41.

Manual versus computer-assisted programming: manual programming suits point-to-point drilling and simple contours; APT-type languages and modern CAM systems generate tool paths from CAD geometry, post-processed into the G-code dialect of a specific controller.

Formulas

N = 1000·V / (π·D)

  • N = spindle speed (rev/min), V = cutting speed (m/min), D = tool or work diameter (mm).

F = N · z · f_z (milling) and F = N · f (turning, drilling)

  • F = feed rate (mm/min), z = number of teeth, f_z = feed per tooth (mm/tooth), f = feed per revolution (mm/rev).

t = L / F

  • t = machining time (min), L = length of tool path at feed, including approach and overtravel (mm).

Arc length s = r·θ, with θ in radians.

BLU = p / (n_s · G)

  • p = leadscrew pitch (lead, for a single-start screw) (mm), n_s = steps per motor revolution, G = gear ratio (motor revolutions per screw revolution). BLU in mm per pulse.

Number of pulses = distance / BLU; pulse frequency f_p = (F / p) · G · n_s / 60 (Hz), with F in mm/min.

Worked examples

Example 1 (standard): reading a contour program. A 10 mm end mill starts at the work zero (0, 0) and runs, at a programmed F200 (mm/min): N10 G90 G21 G17 G01 X80.0 Y0 F200 N20 G03 X100.0 Y20.0 I0 J20.0 N30 G01 Y60.0 Cutting speed is 40 m/min with a 2-flute cutter.

  1. N10: straight line (0, 0) to (80, 0); length = 80 mm.
  2. N20: arc start (80, 0), centre = start + (I, J) = (80, 20), radius r = 20 mm. End (100, 20) is at angle 0° from the centre; start is at −90°. Moving CCW from −90° to 0° is a quarter circle: s = r·θ = 20 × π/2 = 31.42 mm.
  3. N30: Y changes 20 → 60 with X modal at 100; length = 40 mm.
  4. Total path L = 80 + 31.42 + 40 = 151.42 mm; t = L/F = 151.42/200 = 0.757 min = 45.4 s.
  5. Spindle speed N = 1000 × 40/(π × 10) = 1273 rev/min; feed per tooth f_z = F/(N·z) = 200/(1273 × 2) = 0.0785 mm/tooth.

Example 2 (GATE level): open-loop stepper drive. A stepper motor with 200 steps/rev drives a leadscrew of 5 mm pitch through a gearbox; the motor turns 2 revolutions per screw revolution. The table must move 250 mm at 300 mm/min.

  1. BLU = p/(n_s·G) = 5/(200 × 2) = 0.0125 mm per pulse.
  2. Pulses needed = 250/0.0125 = 20 000 pulses.
  3. Screw speed = F/p = 300/5 = 60 rev/min; motor speed = 2 × 60 = 120 rev/min.
  4. Pulse frequency = 120 × 200/60 = 400 Hz.
  5. Check: time = 250/300 = 0.8333 min = 50 s, and 20 000 pulses/50 s = 400 Hz, consistent.

Common mistakes

  • Treating I and J as absolute coordinates of the centre; in most controllers they are offsets from the arc's start point.
  • Mixing up G02 and G03 by viewing the plane from below, or forgetting that the lathe's X axis is often programmed in diameter.
  • Leaving G91 active after an incremental move, so later absolute coordinates are added on.
  • Using G00 to approach close to the work; rapid moves are not controlled-path and can hit clamps.
  • Forgetting to turn on or cancel cutter radius compensation (G41/G42 with G40) on a linear lead-in move.
  • In BLU problems, using the gear ratio upside down: if the motor turns faster than the screw, the BLU gets smaller.
  • Ignoring approach and overtravel when computing machining time.

For GATE ME

Expect MCQs on the meaning of G- and M-codes, absolute versus incremental programming, NC versus CNC versus DNC, and open versus closed loop. Numericals typically ask for the tool path or end point of a short program, arc lengths and machining time, or the BLU, pulse count and pulse frequency of a stepper-driven axis. Practise tracing a 4–6 block program by hand and the stepper chain pitch → gear → steps.

Quick check

  1. Which G-code selects incremental programming?
  2. What does M06 do?
  3. An arc block G02 X40 Y0 I20 J0 starts at (0, 0). Where is the centre?
  4. A stepper with 1.8° steps drives a 6 mm pitch screw directly. What is the BLU?
  5. Is G41 tool on the left or the right of the path, looking along the direction of motion?

Answers: 1. G91. 2. Tool change. 3. (20, 0). 4. 6/200 = 0.03 mm. 5. Left.

Try answering each one aloud before you open it.

  1. 1.What is NC part programming?Concept

    NC part programming refers to the process of creating a set of instructions for a machine tool to follow in order to produce a part. These instructions are typically in the form of a code that specifies the movements and operations the machine must perform, such as cutting, drilling, or milling.

  2. 2.Explain the difference between NC and CNC.Concept

    In NC the controller is hard-wired logic that reads the program block by block from punched tape; nothing is stored, so the tape is re-read for every part and any change needs a new tape. In CNC a dedicated computer in the controller stores the whole program, so it can be edited at the machine, and features like interpolation, tool offsets, canned cycles, subprograms and diagnostics are done in software. The G-code itself can be written by hand or by CAM for either; the difference is the controller, not who writes the code.

  3. 3.What are the main components of a CNC machine?Concept

    The main components of a CNC machine include the control unit, machine tool, drive system, feedback system, and the interface. The control unit processes the program instructions, the machine tool performs the operations, the drive system moves the machine parts, the feedback system ensures accuracy, and the interface allows user interaction.

  4. 4.Why is G-code used in CNC programming?Application

    G-code is used in CNC programming because it provides a standardized language for instructing CNC machines on how to move and operate. It specifies the paths, speeds, and operations needed to produce a part, making it essential for precision and repeatability in manufacturing.

  5. 5.What happens if a CNC machine loses its reference point during operation?Application

    The controller no longer knows where machine zero is, so the work offsets (G54 etc.) and soft limits point to the wrong places and the tool can cut in the wrong position or crash into the part, fixtures or the machine. This happens after a power loss on machines with incremental encoders, a crash, or a missed step on an open-loop stepper drive. The axes must be re-homed to the reference point (G28 or manual reference return), offsets checked, and the part re-verified before restarting.

  6. 6.How does tool compensation work in CNC programming?Application

    With cutter radius compensation the programmer writes the part contour, and G41 (tool to the left of the path) or G42 (to the right) makes the controller offset the tool centre by the radius stored in a D register; G40 cancels it. Tool length compensation (G43 with an H register) shifts Z by each tool's measured length so all tools share one work zero. When a tool wears or is replaced by a different size, only the offset value changes, not the program.

  7. 7.Explain the role of M-codes in CNC programming.Concept

    M-codes in CNC programming are used to control miscellaneous functions of the machine, such as starting or stopping the spindle, turning on or off coolant, and changing tools. They complement G-codes by managing non-motion-related operations essential for the machining process.

  8. 8.What is the significance of feed rate in CNC machining?Application

    The feed rate in CNC machining determines the speed at which the cutting tool moves through the material. It is crucial for optimizing the machining process, affecting the surface finish, tool life, and overall efficiency. An incorrect feed rate can lead to poor quality or tool damage.

  9. 9.Calculate the spindle speed for a CNC machine if the cutting speed is 100 m/min and the diameter of the workpiece is 50 mm.Numerical

    The spindle speed (N) can be calculated using the formula: N = (1000 × V) / (π × D), where V is the cutting speed and D is the diameter. Substituting the given values: N = (1000 × 100) / (π × 50) ≈ 636.62 RPM.

  10. 10.A CNC machine is programmed to drill a hole with a depth of 20 mm at a feed rate of 100 mm/min. How long will it take to complete the drilling operation?Numerical

    The time (T) to complete the drilling operation can be calculated using the formula: T = Depth / Feed Rate. Substituting the given values: T = 20 mm / 100 mm/min = 0.2 minutes or 12 seconds.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?