Manual CNC part programming: G and M codes
Block and word structure, modal G codes, common G and M codes, arc programming with I/J, absolute versus incremental, and path-time calculations.
Drafted with Aria, reviewed by the AiCanCode.org team. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Even when CAM software writes most programs, the shop floor still runs on G and M codes. Operators edit them, setters prove them out, and anyone debugging a scrapped part reads them line by line. A wrong sign on an arc centre or a missing G90 can crash a machine, so knowing exactly what each word does is a basic skill for a production engineer.
Key ideas
Program structure. A part program is a sequence of blocks (lines). Each block is made of words: a letter address followed by a number.
- N — sequence number; G — preparatory function (type of motion or mode); X, Y, Z — coordinates; I, J, K — arc-centre offsets along X, Y, Z; F — feed; S — spindle speed; T — tool number; M — miscellaneous (machine) function; D/H — offset registers.
- Example:
N40 G01 X60.0 Y10.0 F120 S1200 M03. The codes below follow the common ISO 6983 / Fanuc style; individual controllers differ, so always check the machine's programming manual.
Modal and non-modal codes. Most G codes are modal: they stay active until replaced by another code from the same group (G00/G01/G02/G03 are one group, G90/G91 another). Once G01 and F120 are given, the next block containing only coordinates still cuts at 120 mm/min. Codes such as G04 (dwell) act only in their own block.
Important G codes.
- G00 rapid positioning (no cutting; path may not be a straight line on some machines); G01 linear interpolation at feed F.
- G02 circular interpolation clockwise; G03 counter-clockwise — viewed from the positive end of the axis perpendicular to the plane (looking down −Z for the XY plane).
- G17/G18/G19 select the XY/ZX/YZ plane for arcs and compensation.
- G20/G21 inch/metric units.
- G90 absolute coordinates (from the work zero); G91 incremental (from the current point).
- G40/G41/G42 cancel cutter-radius compensation / compensate left / right of the path, looking along the direction of travel.
- G43 tool-length compensation; G54–G59 work-coordinate offsets; G28 return to reference point.
- G81 drilling cycle, G83 peck drilling, G80 cancel cycle (canned cycles compress a repeated drilling sequence into one block).
- Turning: G96 constant surface speed (S in m/min), G97 constant rev/min, G94/G95 feed per minute/per revolution (Fanuc lathes often use G98/G99 for the same pair).
Important M codes. M00 program stop; M01 optional stop; M02 program end; M03 spindle clockwise; M04 spindle counter-clockwise; M05 spindle stop; M06 tool change; M08 coolant on; M09 coolant off; M30 program end and rewind to the start.
Arc programming. With the I, J, K method, I and J are the distances from the arc start point to the arc centre along X and Y, and on most controls they are incremental even in G90. Alternatively R gives the radius (positive for arcs up to 180°). The end point must be at the same radius from the centre as the start point.
Work zero and offsets. Coordinates are written from a program zero chosen on the part (often a corner or the centre of a bore). The setter measures where that zero is on the machine and enters it in G54; tool lengths and radii go in the offset tables, so the same program works whatever tool is loaded.
Formulas
F = N · f
- F feed rate (mm/min); N spindle speed (rev/min); f feed per revolution (mm/rev). For milling
F = N · f_z · z.
L = √(Δx² + Δy²)
- L length of a G01 move (mm).
L_arc = r · θ
- r arc radius (mm); θ swept angle (rad).
t = L / F
- t time for a feed move (min).
t = L_cut / (f · N)
- Turning-pass time (min) for a cut length L_cut (mm).
Worked examples
Example 1 (standard). A tool moves with G01 X100 Y100 F200 from (0, 0). How long does the move take? Then a lathe cut of 100 mm length runs at f = 0.2 mm/rev and N = 1000 rev/min; find its time.
L = √(Δx² + Δy²)= √(100² + 100²) = 141.42 mm.t = L / F= 141.42 / 200 = 0.7071 min = 42.4 s.- Turning: F = 0.2 × 1000 = 200 mm/min; t = 100/200 = 0.5 min (30 s).
Example 2 (GATE level). The tool is at (10, 10) in the XY plane, in G90 and G17. Find the arc centre and the total cutting time of:
N10 G01 X60 Y10 F120
N20 G03 X80 Y30 I0 J20
N30 G01 X80 Y70
- N10: straight move from (10, 10) to (60, 10), L₁ = 50 mm.
- N20: centre = start + (I, J) = (60 + 0, 10 + 20) = (60, 30). Start radius = |(60, 10) − (60, 30)| = 20 mm; end radius = |(80, 30) − (60, 30)| = 20 mm ✓. The start is below the centre and the end is to its right, so going counter-clockwise (G03) sweeps 90°. L₂ = 20 × π/2 = 31.42 mm.
- N30: modal G01 and F120 still apply; (80, 30) → (80, 70), L₃ = 40 mm.
- Total L = 50 + 31.42 + 40 = 121.42 mm; t = 121.42/120 = 1.012 min = 60.7 s.
- The same path in G91:
G91 G01 X50 Y0,G03 X20 Y20 I0 J20,G01 X0 Y40.
Common mistakes
- Reading I and J as absolute centre coordinates; on most controls they are measured from the arc start point.
- Deciding CW/CCW by looking from below or in the wrong plane.
- Forgetting that G01 and F are modal and accidentally cutting at a rapid-sized feed later, or forgetting to cancel G91 and drifting off the part.
- Confusing M02 and M30, or M03 and M04.
- Applying G41/G42 in the same block as an arc, or without a lead-in move long enough for the offset.
- Using the 3-D diagonal instead of the plane distance, or forgetting to convert minutes to seconds.
For GATE PI
Typical questions give a short program and ask for the final tool position, the arc centre or radius, the total path length or machining time, or the meaning of a code. Practise switching between G90 and G91, working out I and J, and reading CW/CCW correctly. Short MCQs test common G and M codes.
Quick check
- Which code gives counter-clockwise circular interpolation?
- What is the difference between M02 and M30?
- In G91, the tool at (20, 30) executes
G01 X15 Y−10. Where does it end? - A G02 semicircle of radius 25 mm is cut at 150 mm/min. How long does it take?
- Which code cancels cutter-radius compensation?
Answers: 1. G03. 2. Both end the program; M30 also rewinds to the start. 3. (35, 20). 4. π × 25/150 = 0.524 min ≈ 31.4 s. 5. G40.
Interview questions
All Computer Integrated Manufacturing interview questionsTry answering each one aloud before you open it.
1.What are G codes and M codes in CNC programming?Concept
G codes are preparatory commands that control the movement and operation of the CNC machine, such as positioning and cutting. M codes are miscellaneous commands that control auxiliary functions like coolant and spindle on/off. Together, they form the basic language for CNC programming.
2.Explain the difference between absolute and incremental positioning in CNC programming.Concept
In absolute positioning, all coordinates are referenced from a fixed origin point. In incremental positioning, each coordinate is referenced from the last position of the tool. Absolute positioning is useful for consistency, while incremental can simplify programming for repetitive tasks.
3.Why is G00 used in CNC programming, and what does it do?Application
G00 is used for rapid positioning in CNC programming. It moves the tool quickly to a specified location without cutting. This is useful for reducing non-productive time when moving between cutting operations.
4.What happens if you use G01 instead of G00 for moving the tool to a new position?Application
Using G01 instead of G00 will move the tool at a controlled feed rate rather than rapidly. This can increase cycle time and is generally used for cutting operations rather than positioning.
5.Explain the purpose of M03 and M05 codes in CNC programming.Concept
M03 is used to start the spindle in a clockwise direction, while M05 stops the spindle. These codes are essential for controlling the spindle's operation during machining processes.
6.Why is it important to use M30 at the end of a CNC program?Application
M30 is used to end a CNC program and reset the machine to the start of the program. It ensures that the machine is ready for the next operation and prevents unintended movements or operations.
7.What is the function of G02 and G03 codes in CNC programming?Concept
G02 is used for circular interpolation in a clockwise direction, while G03 is for counterclockwise. These codes are used to create arcs and circles in the machining process.
8.If a CNC program uses G90, what does it imply about the positioning mode?Application
G90 sets the CNC machine to absolute positioning mode. This means all coordinates are referenced from a fixed origin point, ensuring consistency in tool movements.
9.A CNC program uses G01 to move the tool from (0,0) to (100,100) at a feed rate of 200 mm/min. How long will the movement take?Numerical
Distance = √((100-0)² + (100-0)²) = √(10000 + 10000) = √20000 = 141.42 mm. Time = Distance / Feed rate = 141.42 mm / 200 mm/min = 0.7071 min ≈ 42.43 seconds.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?