🇬🇧 EnglishFebruary 20, 2026·6 min read·Education

Understanding G-code: A Beginner's Guide to CNC Programming

G-code is the universal language of CNC machines. This guide breaks down the essential commands, explains coordinate systems, and walks through practical examples.

#G-code#CNC#programming#education#beginner

What is G-code?

G-code is a programming language that tells CNC (Computer Numerical Control) machines how to move. Developed at MIT in the 1950s, it remains the standard language for controlling milling machines, lathes, laser cutters, plasma cutters, 3D printers, and waterjet cutters worldwide. Despite its age, G-code persists because of its simplicity: each line is a single instruction that the machine executes sequentially.

Think of G-code as driving directions for a robot: "Go to this location at this speed, lower the tool, move in this direction, raise the tool, return home." String enough of these instructions together, and you can produce virtually any 2D or 3D shape.

Anatomy of a G-code Line

A G-code file is plain text. Each line (called a "block") typically contains one or more commands. Here is a simple example:

N10 G01 X50.0 Y25.0 F500

Breaking this down:

  • N10: Line number (optional, used for reference)
  • G01: Linear interpolation command (move in a straight line)
  • X50.0: Move to X coordinate 50.0 mm
  • Y25.0: Move to Y coordinate 25.0 mm
  • F500: Feed rate of 500 mm/min

Comments are added with semicolons or parentheses: G01 X50 Y25 ; move to corner or G01 X50 Y25 (move to corner).

Essential G Commands (Preparatory Commands)

G commands control the machine's motion mode and geometry:

Motion Commands

  • G00 — Rapid Positioning: Moves the tool to the specified coordinates at maximum speed. No cutting occurs. Used to reposition the tool between cuts. Example: G00 X0 Y0 Z5 — rapidly move to origin at 5mm height.
  • G01 — Linear Interpolation: Moves the tool in a straight line at the specified feed rate (F value). This is the fundamental cutting command. Example: G01 X100 Y50 F500 — cut in a straight line to (100, 50) at 500 mm/min.
  • G02 — Clockwise Circular Interpolation: Cuts a clockwise arc. The center of the arc is specified with I and J parameters (incremental from current position) or R (radius). Example: G02 X50 Y0 I0 J-25 — clockwise arc to (50,0) with center offset (0,-25).
  • G03 — Counter-Clockwise Circular Interpolation: Same as G02 but in the opposite direction.

Setup Commands

  • G17 / G18 / G19 — Plane Selection: G17 selects the XY plane (standard for 2D cutting), G18 selects XZ, G19 selects YZ. Most 2D operations use G17.
  • G20 / G21 — Unit Selection: G20 sets inches, G21 sets millimeters. Always declare this at the start of your program. Mixing units is a classic source of errors.
  • G28 — Home Position: Sends the machine to its reference (home) position. Used at program start and end for safety.
  • G40 / G41 / G42 — Cutter Compensation: G40 cancels compensation, G41 compensates left, G42 compensates right. This offsets the toolpath by half the cutter diameter so the cut profile matches the programmed dimensions.
  • G90 / G91 — Positioning Mode: G90 is absolute (all coordinates are relative to the origin), G91 is incremental (each coordinate is relative to the current position). Absolute mode (G90) is used in the vast majority of programs.

Essential M Commands (Miscellaneous Commands)

M commands control the machine's mechanical functions:

  • M00 — Program Stop: Pauses execution. The operator presses cycle start to continue.
  • M03 — Spindle On (Clockwise): Starts the spindle. The S parameter sets RPM. Example: M03 S12000 — start spindle at 12,000 RPM.
  • M05 — Spindle Off: Stops the spindle motor.
  • M06 — Tool Change: Changes to the tool specified by T parameter. Example: M06 T02 — change to tool 2.
  • M08 / M09 — Coolant On / Off: Controls the coolant system.
  • M30 — Program End: Ends the program and rewinds to the beginning.

The Coordinate System

CNC machines use a Cartesian coordinate system with three primary axes:

  • X axis: Left-right movement (horizontal)
  • Y axis: Front-back movement (horizontal)
  • Z axis: Up-down movement (vertical). Negative Z values mean the tool is moving into the material.

The work origin (zero point) is the reference point for all coordinates in the program. It is typically set at the top surface of the material at one corner (often the front-left). G54 through G59 allow you to define multiple work origins for different setups.

Complete Example: Cutting a Rectangle

Here is a complete G-code program that cuts a 60mm x 40mm rectangle from 3mm aluminum:

; Rectangle cutting program
; Material: 3mm Aluminum
; Tool: 6mm end mill
G21             ; Metric (mm)
G90             ; Absolute positioning
G17             ; XY plane
G28             ; Home position

M06 T01         ; Select tool 1
M03 S10000      ; Spindle on at 10,000 RPM
M08             ; Coolant on

G00 X0 Y0 Z10  ; Rapid to start position
G00 Z2          ; Lower to 2mm above material

; First pass: 1.5mm depth
G01 Z-1.5 F100  ; Plunge to 1.5mm depth
G01 X60 F800    ; Cut along bottom edge
G01 Y40         ; Cut along right edge
G01 X0          ; Cut along top edge
G01 Y0          ; Cut along left edge (close rectangle)
G00 Z2          ; Retract above material

; Second pass: 3mm depth (through cut)
G01 Z-3.2 F80   ; Plunge to 3.2mm (0.2mm past material)
G01 X60 F600    ; Cut along bottom edge
G01 Y40         ; Cut along right edge
G01 X0          ; Cut along top edge
G01 Y0          ; Cut along left edge
G00 Z10         ; Retract to safe height

M09             ; Coolant off
M05             ; Spindle off
G28             ; Home position
M30             ; Program end

Notice the two-pass approach: the first pass cuts halfway through, the second pass completes the cut. This reduces tool load and produces better edge quality. The final depth is 3.2mm (0.2mm past the 3mm material) to ensure a clean through-cut.

Common G-code Mistakes

  • Unit confusion: Forgetting G21/G20 or mixing metric and imperial. A 50mm dimension interpreted as 50 inches moves the machine 1,270mm — likely crashing into a limit switch.
  • Missing safe height: Not retracting to a safe Z height before rapid moves (G00). The tool can crash into clamps, fixtures, or the workpiece.
  • Forgetting the spindle: Starting a cut without M03. The tool plunges into material without rotating, causing immediate breakage.
  • No feed rate: Issuing a G01 without an F value. Some controllers use the last F value, others default to an extremely slow rate.
  • Absolute vs. incremental confusion: Switching between G90 and G91 without careful tracking leads to the tool moving to unexpected positions.

Tools That Generate G-code For You

Writing G-code by hand is educational but impractical for complex parts. Modern tools that generate G-code include:

  • SnapCAM: Generates G-code directly from photos. No CAD or manual programming needed. Try it for free.
  • CAM software: Fusion 360, Mastercam, and SolidCAM generate toolpaths from 3D models.
  • G-code simulators: CAMotics and NCViewer let you preview G-code visually before running it on your machine.

For a step-by-step walkthrough of the photo-to-G-code workflow, see our complete guide to photo-to-CNC.

C
SnapCAM Team
SnapCAM Team

Try SnapCAM Free

Generate G-code from photos. 3 free projects per month.

Start Free