CNC G-Code Examples Every Beginner Must Know

CNC G-Code Examples Every Beginner Must Know

Computer Numerical Control (CNC) machining revolutionized manufacturing by automating precise material removal using programmed instructions. The primary language for these instructions is G-code (preparatory codes for geometry and motion), supplemented by M-codes (miscellaneous functions for machine control). Beginners often start with manual G-code programming to understand machine behavior before relying on CAM software.

This guide covers the most essential G-codes and M-codes, explains their functions with examples, and provides complete simple programs for both milling and turning. Mastering these basics—rapid moves, linear feeds, arcs, units, and safety commands—forms the foundation for all CNC programming. Always simulate code first and refer to your machine’s manual, as minor variations exist (e.g., Fanuc, Haas, GRBL).

Key Concepts for Beginners

  • Modal vs. Non-Modal: Modal commands (e.g., G01) stay active until canceled. Non-modal (e.g., G04 dwell) apply only once.
  • Absolute vs. Incremental: G90 (absolute positioning from origin) is default for beginners; G91 uses distances from current position.
  • Units: G20 (inches) or G21 (millimeters)—set early to avoid errors.
  • Feed Rate (F): Cutting speed in units/min (modal).
  • Spindle Speed (S): RPM (often with M03/M04).
  • Safety Line: Start programs with G17/G21/G90 for plane, units, and absolute mode.
  • Program Structure: Begin with setup, tool moves, cutting, end with retract and stop.

Most Common G-Codes for Beginners

CodeDescriptionExample UsageNotes
G00Rapid positioning (fast move, no cutting)G00 X10 Y10 Z5Non-cutting; use for safe approaches.
G01Linear interpolation (straight cut at feed rate)G01 X50 Y50 F200Most used for cutting; requires F.
G02Clockwise circular arcG02 X… Y… I… J… F…I/J = center offset from start.
G03Counterclockwise circular arcG03 X… Y… I… J… F…Common for circles/holes.
G04Dwell (pause)G04 P1.0 (pause 1 second)Non-modal; P in seconds or ms.
G17XY plane selectionG17Default for milling.
G20Inch unitsG20Set at start.
G21Millimeter unitsG21Common for most beginners.
G40Cancel cutter compensationG40Safety cancel.
G41/G42Cutter compensation left/rightG41 D1 (offset from tool table)For precise contours.
G54-G59Work offsets (datums)G54Set zero points.
G80Cancel canned cyclesG80Safety.
G90Absolute positioningG90Beginner default.
G91Incremental positioningG91Use sparingly.

Most Common M-Codes for Beginners

CodeDescriptionExampleNotes
M00Program stop (optional pause)M00Operator resume.
M01Optional stopM01If button enabled.
M03Spindle on clockwiseM03 S1000S = RPM.
M04Spindle on counterclockwiseM04 S1000Rare for most tools.
M05Spindle stopM05Always end with this.
M06Tool changeT01 M06T selects tool first.
M08Coolant on (flood)M08
M09Coolant offM09
M30Program end and rewindM30Resets to start.

These cover 90% of basic programs. Advanced codes (e.g., G81 drilling cycle) come later.

Simple CNC Milling Program Example

This beginner program mills a basic square pocket with a rounded corner (using arcs). Assume a 10mm end mill, aluminum stock, origin at top-left corner.

O1000 (Simple Square Pocket Example)
G21 G17 G90 G40 G49 (Safety: mm, XY plane, absolute, cancel offsets)
G54 (Work offset)
T1 M06 (Tool change to end mill)
S5000 M03 (Spindle 5000 RPM clockwise)
G00 X0 Y0 Z5 (Rapid to start above material)
M08 (Coolant on)
G00 Z-2 (Rapid plunge to depth - careful!)
G01 X50 Y0 F200 (Cut right side)
G01 X50 Y50 (Up side)
G02 X40 Y60 I-10 J0 (Clockwise arc corner)
G01 X0 Y60 (Left side)
G01 X0 Y0 (Down side)
G00 Z5 (Retract)
M09 (Coolant off)
M05 (Spindle stop)
G00 X0 Y0 Z50 (Safe home)
M30 (End program)

Explanation line-by-line:

  • Program number and safety setup.
  • Tool/spindle start.
  • Rapid to position, plunge, linear cuts.
  • Arc for smooth corner.
  • Retract and shutdown.

Run in single-block mode first. Simulate to visualize path.

Simple CNC Turning (Lathe) Program Example

Lathes use X (diameter/radius) and Z (length). This turns a simple cylinder with taper and facing.

O2000 (Simple Turning Example)
G21 G18 G90 G40 (mm, XZ plane, absolute)
G97 S1500 M03 (Constant RPM 1500 clockwise)
T0101 M06 (Turning tool)
G00 X30 Z5 (Rapid approach)
G00 Z0 (Face start)
G01 X-1 F0.2 (Face cut to center)
G00 X30 Z2 (Retract slightly)
G00 Z-50 (Position for OD turn)
G01 X25 F0.15 (Taper start)
G01 X20 Z-70 (Linear taper)
G01 X20 Z-100 (Straight section)
G00 X35 (Retract)
G00 Z5 (Home Z)
M05 (Spindle stop)
M30

Key differences from milling:

  • G18 for XZ plane.
  • G96/G97 for constant surface speed vs. fixed RPM.
  • No Y-axis usually.

Typical CNC lathe turning operations shown here.

Tips for Beginners

  1. Always Simulate: Use free tools like NC Viewer or CAM simulators to check for crashes.
  2. Dry Run: Run program with tool offset high above material.
  3. Feed/Depth: Start conservative (e.g., F100-200 mm/min, shallow depths).
  4. Tool Offsets: Use G43/G49 for length, D for radius comp.
  5. Practice: Start with air cuts or soft materials like foam/MDF.
  6. Common Errors: Forgot units (crash scales), wrong plane, no retract.
  7. Next Steps: Learn canned cycles (G81 drill, G71 roughing on lathe) after basics.

With these codes and examples, you can write and run basic programs. Practice by modifying the samples—change coordinates, add dwells, or combine arcs/lines. CNC programming rewards patience; soon you’ll create complex parts confidently.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *