Home Services Industrial Knowledge Base Industrial Knowledge Base CNC Programming & G-Code Basics INDUSTRIAL

CNC Programming & G-Code Basics

Learn what G-code is, the most common G and M codes, the structure of a CNC program, absolute vs incremental moves, and the workflow from drawing to finished part.

CNC Programming & G-Code Basics

Behind every machined part is a program — a list of coordinated instructions that tells a CNC machine exactly where to move, how fast, and when to turn the spindle and coolant on. That language is G-code (with its companion M-codes). Whether your shop programs by hand, edits CAM output, or just needs to read what the machine is doing, understanding G-code turns the controller from a black box into a tool you command. This guide covers the common codes, the structure of a program, and the workflow from drawing to finished part — the practical foundation every machinist in a GCC workshop should own.

A clean, correct program is the difference between a good part and a crash. For programmed turned components, our precision turning & machining services prove out code before it touches expensive stock.

What G-code is

G-code is the standard programming language for CNC machines, defined broadly by ISO 6983 (and vendor dialects from Fanuc, Siemens, Haas, and others). A program is a sequence of blocks (lines), each made of words — a letter plus a number. The letter says what kind of instruction; the number gives the value:

  • G — preparatory (motion/mode) commands.
  • M — miscellaneous (machine) functions: spindle, coolant, program stop.
  • X Y Z — coordinates (and U W incremental on lathes).
  • F — feed rate; S — spindle speed; T — tool number.
  • N — line number; I J K / R — arc parameters.

Common G-codes

These are the codes you meet daily. Modal codes stay active until changed:

Code Function Notes
G00 Rapid move Positioning, not cutting
G01 Linear feed move Straight cut at F feed
G02 / G03 Clockwise / counter-clockwise arc With I J K or R
G17 / G18 / G19 Plane select (XY / XZ / YZ) G18 common on lathes
G20 / G21 Inch / metric units Set once, early
G28 Return to machine home Via an intermediate point
G40 / G41 / G42 Cutter compensation off / left / right Tool-radius offset
G43 Tool length offset (mills) With H word
G54–G59 Work coordinate systems Part zero / offsets
G90 / G91 Absolute / incremental positioning Big safety distinction
G96 / G97 Constant surface speed / constant RPM Lathe turning

Common M-codes

Code Function
M03 / M04 Spindle on clockwise / counter-clockwise
M05 Spindle stop
M06 Tool change
M08 / M09 Coolant on / off
M30 Program end and rewind
M00 / M01 Program stop / optional stop

The structure of a program

A typical program has three parts: a safe start-up block (units, work offset, absolute mode, cancel comp), the machining body (tool calls, spindle/coolant on, motion), and a safe ending (retract, spindle/coolant off, return home, end). A simple turning snippet might read:

G21 G18 G40       ; metric, XZ plane, comp off
G54               ; work offset
T0101             ; tool 1, offset 1
G96 S180 M03      ; constant surface speed, spindle CW
G00 X26 Z2        ; rapid to start
G01 Z-40 F0.2     ; turn along
G00 X100 Z100     ; retract
M05               ; spindle off
M30               ; end

The key safety habit: rapids (G00) never cut — position with clearance, then feed (G01) into the material.

Absolute vs. incremental — the costly confusion

G90 (absolute) measures every coordinate from the part zero (G54). G91 (incremental) measures each move relative to the current position. Mixing them up is a classic cause of crashes: a Z-40 in absolute goes to a fixed depth, but in incremental it moves another 40 mm deeper from wherever you are. Set the mode deliberately and prefer absolute for clarity.

From drawing to part: the workflow

  1. Read the drawing — dimensions, tolerances, datums, material.
  2. Plan the operations — facing, roughing, finishing, drilling, threading, in order.
  3. Program — by hand for simple parts, or with CAM software that generates G-code from a 3D model and a post-processor for your control.
  4. Set up the machine — workholding, tools, and set the work offset (G54) and tool offsets.
  5. Prove out — dry-run, single-block, and a first-article check before production.
  6. Run and inspect — confirm the first part to drawing, then run the batch.

Conclusion

G-code is not magic — it is a readable list of coordinated moves and machine functions. Learn the common G and M codes, respect the rapid-versus-feed and absolute-versus-incremental distinctions, structure programs with safe start and end blocks, and always prove code out before cutting expensive stock. For parts where the program decides the result, see our precision turning & machining services and the Industrial Knowledge Base.

FAQ

What is the difference between G-code and M-code? G-codes control motion and modes (how the tool moves); M-codes control machine functions like spindle, coolant, tool change, and program end.

Do I need to write G-code by hand? For simple parts, hand programming is fast and clear. Complex 3D parts are usually programmed in CAM software, which outputs G-code via a post-processor.

What does G00 vs G01 mean? G00 is a rapid positioning move (not for cutting); G01 is a controlled linear feed move that actually cuts at the programmed feed rate.

SKYLINE Engineering

@skyline

The engineering team at SKYLINE Industrial Solutions. We publish field-tested guides drawn from real KSA and GCC deployments.

See author profile
SKYLINE engineering services

Need this implemented for you?

Reading is free — building it right takes a team. SKYLINE engineers ship Industrial Knowledge Base for Aramco vendors, banks, hospitals and government agencies across Saudi Arabia. Talk to us before you start.

Aramco Approved Contractor ISO 9001 · ISO 27001 SAMA CSF aligned NCA ECC ready 247+ KSA clients

Comments

0 total · 0 threads
Be the first to leave a comment.