NC and CNC part programming
NC, CNC and DNC, machine axes and zero points, program structure, common G- and M-codes, arc programming and cutter compensation, open- and closed-loop drives, with a contour program and BLU calculation.
Drafted with Aria, reviewed by the AiCanCode.org team. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Almost every machined automotive part – cylinder heads, crankshafts, brake callipers, transmission cases – is produced on CNC turning and machining centres, and the person who writes or checks the part program decides cycle time, accuracy and whether a crash happens on the first part. Even when CAM software generates the code, engineers must read G- and M-codes, set work offsets and understand how the controller moves the axes.
Key ideas
NC, CNC and DNC. Numerical control (NC) runs a machine from coded instructions; early NC used hard-wired logic and read the program block by block from punched tape. Computer numerical control (CNC) has a dedicated computer in the controller: the program is stored in memory, can be edited at the machine, and the controller provides tool-length and cutter-radius compensation, canned cycles, subprograms, graphics simulation and diagnostics. DNC (direct/distributed NC) links many CNC machines to a central computer for program transfer and monitoring.
Machine axes. Axes follow the right-hand rule. Z is parallel to the spindle axis (positive away from the work); X is the principal horizontal axis (in turning, radial); Y completes the right-handed set. Rotary axes about X, Y, Z are A, B, C. A machining centre with X, Y, Z plus two rotary axes is "5-axis".
Reference points. Machine zero is fixed by the builder; the programmer chooses a work (program) zero on the part, and its position relative to machine zero is stored as a work offset (G54–G59 on many controllers). Absolute programming (G90) gives every coordinate from the work zero; incremental (G91) gives each move from the previous point.
Program structure. A program is a sequence of blocks; each block holds words: N (sequence number), G (preparatory function), X/Y/Z (coordinates), I/J/K or R (arc centre or radius), F (feed), S (spindle speed), T (tool), M (miscellaneous), plus an end-of-block character. Many G-codes are modal (they stay active until replaced).
Common codes (ISO 6983 / widely used Fanuc-style; always check the controller manual, because some codes differ between makers):
- G00 rapid positioning; G01 linear interpolation at feed F; G02 circular interpolation clockwise; G03 counter-clockwise; G04 dwell.
- G17/G18/G19 select the XY/ZX/YZ plane; G20/G21 inch/metric input; G28 return to reference point.
- G40/G41/G42 cutter-radius compensation off/left/right; G43 tool-length compensation.
- G90/G91 absolute/incremental; G94/G95 feed per minute/per revolution; G96/G97 constant surface speed on/off (lathes).
- G81 drilling canned cycle, G83 peck drilling, G80 cancel cycle.
- M00 program stop; M01 optional stop; M02/M30 end of program (M30 also rewinds); M03/M04/M05 spindle clockwise/counter-clockwise/stop; M06 tool change; M08/M09 coolant on/off.
Interpolation and compensation. The interpolator breaks lines and arcs into coordinated axis increments. For arcs, I, J, K give the centre relative to the arc's start point (on most controllers), or R gives the radius. With cutter compensation, the program follows the part outline and the controller offsets the tool centre by the cutter radius – G41 if the tool is to the left of the part looking along the direction of travel, G42 if to the right. Without compensation, the program must be written for the cutter-centre path.
Control systems. Open loop – stepper motors receive a counted number of pulses; no position feedback; cheap but steps can be lost under load. Closed loop – servo motors with encoders or linear scales feeding back actual position; standard on modern CNC. The basic length unit (BLU) is the smallest table movement the system can command.
Manual vs computer-assisted programming. Manual programming suits simple 2-D parts; APT (a language-based method) and today's CAM systems generate tool paths from CAD geometry, then a post-processor converts them into the specific controller's G-code. Always verify with simulation and a dry run.
Formulas
BLU = p / (n_s·r_g)
Basic length unit (mm per pulse); p = lead-screw pitch (mm), n_s = steps per motor revolution, r_g = gear ratio (motor revolutions per screw revolution).
n_p = L / BLU f_p = F / (60·BLU)
Pulses needed for a move of length L (mm), and pulse frequency (Hz) for feed rate F (mm/min).
N_m = 60·f_p / n_s
Motor speed (rev/min).
F = f_z·Z·N (milling) F = f·N (turning, mm/min)
Programmed feed rate; f_z in mm/tooth, f in mm/rev, N in rev/min.
R = √(I² + J²)
Arc radius from the centre offsets (mm); I, J = incremental distances from arc start to centre.
Worked examples
Example 1 (standard) – contour program. Profile (cutter-centre path, work zero at bottom-left, metric, absolute): (0, 0) → (60, 0) → counter-clockwise arc of radius 20 about (60, 20) to (80, 20) → (80, 50) → (0, 50) → (0, 0). Depth 5 mm, 1200 rev/min, feed 200 mm/min.
N10 G21 G90 G17 G54
N20 T01 M06
N30 S1200 M03
N40 G00 X0 Y0 Z5
N50 G01 Z-5 F100 M08
N60 G01 X60 Y0 F200
N70 G03 X80 Y20 I0 J20
N80 G01 X80 Y50
N90 G01 X0 Y50
N100 G01 X0 Y0
N110 G00 Z50 M09
N120 M05
N130 M30
Checks: at (60, 0) the centre (60, 20) lies 0 in X and +20 in Y away, so I0 J20; R = √(0² + 20²) = 20 mm ✓. Moving from angle −90° to 0° about the centre is anticlockwise, so G03 ✓.
Example 2 (GATE level) – open-loop stepper drive. Lead-screw pitch 5 mm, stepper 200 steps/rev, gear reduction so the motor turns 2 revolutions per screw revolution. The table must move 250 mm at 300 mm/min.
BLU = p/(n_s·r_g)= 5/(200 × 2) = 0.0125 mm.- Pulses: n_p = 250/0.0125 = 20 000.
- Pulse frequency: f_p = 300/(60 × 0.0125) = 400 Hz.
- Motor speed: N_m = 60 × 400/200 = 120 rev/min (check: screw at 60 rev/min × 5 mm = 300 mm/min ✓).
Example 3 – feed for a milling block. A 4-tooth end mill at 1500 rev/min and 0.05 mm/tooth: F = 0.05 × 4 × 1500 = 300 mm/min, programmed as F300 under G94.
Common mistakes
- Swapping G02 and G03 – view the plane from the positive side of the third axis (for G17, looking down from +Z).
- Taking I and J from the work zero instead of from the arc's start point.
- Forgetting that G-codes are modal; a leftover G91 makes the next absolute coordinates move incrementally.
- Using G41 when the cutter must be on the right of the profile (climb vs conventional milling depends on this choice).
- Plunging with G00 into the work – rapid moves are for clear air only.
- Confusing M02 and M30, or M03 (clockwise) and M04.
For GATE ME
Expect identification of G- and M-code functions, absolute vs incremental coordinates, arc programming (centre, radius, direction), interpretation of a short program (final tool position, distance travelled, machining time), BLU and pulse calculations for open-loop systems, and comparisons of NC/CNC/DNC and open/closed-loop control. Practise tracing a program block by block.
Quick check
- Which code gives linear interpolation at the programmed feed?
- What does M06 do?
- An arc starts at (10, 0) with centre (0, 0); what are I and J?
- BLU for 4 mm pitch, 200 steps/rev, direct drive?
- Which axis is parallel to the spindle?
Answers: 1. G01; 2. Tool change; 3. I = −10, J = 0; 4. 0.02 mm; 5. Z.
Interview questions
All Engineering Materials and Manufacturing Processes interview questionsTry answering each one aloud before you open it.
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 manufacture a part. These instructions are typically coded in a numerical control (NC) language, which specifies the movements and operations of the machine tool.
2.Explain the difference between NC and CNC.Concept
Conventional NC used hard-wired control logic and read the part program block by block from punched tape each time a part was made, so editing meant punching a new tape. CNC has a dedicated computer in the controller: the program is stored in memory and can be edited at the machine, and the controller provides tool-length and cutter-radius compensation, canned cycles, subprograms, graphic simulation and diagnostics. The G- and M-code programming language is essentially the same; what changed is how the controller stores and executes it.
3.Why is G-code commonly used in CNC programming?Application
G-code is commonly used in CNC programming because it is a standardized language that allows for precise control of machine tools. It specifies the movements, speeds, and operations of the machine, making it versatile and widely compatible with different CNC machines.
4.What happens if a CNC program has an incorrect tool offset?Application
If a CNC program has an incorrect tool offset, it can lead to inaccurate machining, resulting in parts that do not meet specifications. This can cause issues such as improper fit, surface finish defects, or even damage to the machine or tool.
5.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 other auxiliary operations. They complement G-codes, which primarily control the movement and cutting operations.
6.How does the use of CAD/CAM software benefit CNC part programming?Application
CAD/CAM software benefits CNC part programming by allowing designers to create detailed 3D models and automatically generate the corresponding CNC code. This reduces the likelihood of errors, speeds up the programming process, and allows for more complex geometries to be machined efficiently.
7.What is the significance of tool path optimization in CNC programming?Application
Tool path optimization in CNC programming is significant because it improves machining efficiency, reduces cycle time, and extends tool life. By optimizing the path, unnecessary movements are minimized, and the most efficient route is taken, leading to cost savings and improved productivity.
8.Calculate the feed rate for a CNC milling operation given the spindle speed is 1500 RPM, the number of teeth on the cutter is 4, and the chip load per tooth is 0.05 mm.Numerical
Feed rate (F) can be calculated using the formula: F = N × T × C, where N is the spindle speed, T is the number of teeth, and C is the chip load per tooth. Substituting the given values: F = 1500 RPM × 4 × 0.05 mm = 300 mm/min.
9.What are the potential consequences of using an incorrect spindle speed in a CNC operation?Application
Using an incorrect spindle speed in a CNC operation can lead to poor surface finish, increased tool wear, and even tool breakage. If the speed is too high, it may cause excessive heat and vibration, while a speed that is too low can result in inefficient cutting and longer cycle times.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?