G-code is the universal language of CNC machines, translating digital designs into precise movements. To use it effectively, you’ll need a G-code editor or CAM software to generate the commands, and a CNC controller (like a CNC mill or lathe) with compatible firmware (e.g., G-code interpreters for Mach3, CNC4PC, or newer systems). No prior programming experience is needed—what you’ll learn here covers the fundamentals you’ll use daily.

What You'll Need

  • A CNC machine (mill or lathe) with G-code support (check the controller manual for compatibility)
  • G-code editing software (e.g., FreeCAD, Fusion 360, or a dedicated editor like Fusion CNC)
  • CNC firmware compatibility (e.g., Mach3, CNC-Jr, or modern Motion Controllers)
  • Basic familiarity with CAD/CAM software (most commands assume you’ve exported a DXF/IGES file)
  • A power supply, proper cooling for the spindle, and tooling (drills, end mills, etc.)
  • Safety gear: safety glasses, gloves, and ear protection (spindle noise is loud)

Step-by-Step How to Use G-Code Commands

  1. Prepare Your Workpiece and Tooling
    1. Mount your workpiece securely on the machine’s table (use clamps or vise). Ensure it’s level and stable. A shim or leveling tool can help align the workpiece to the Z-axis (bed reference).
    2. Load the correct cutting tool (e.g., an end mill for milling or a drill for holes). Verify the tool’s diameter and length in the G-code editor to avoid collisions. Tools should be sharpened or replaced if dull—they’ll cause poor surface finish or tool breaks.
    3. Enable spindle power and set the RPM for your tool. Check the controller’s manual for the correct spindle curve (e.g., linear, exponential). For example, a 12-inch drill might run at 3,000 RPM on a 3-axis mill, while a lathe might use 100–5,000 RPM depending on diameter and material.
  2. Set Up the Coordinate System
    1. Initialize the machine’s coordinate system using the G28 command. This returns the tool to the home position (usually the Z-axis up, X/Y to the corners). For example: G28 X0 Y0 Z0
    2. Set the machine’s origin (workpiece reference) using G92 or a G00 move to the actual workpiece position. For example, if your workpiece is 2 inches from the home position along the X-axis: G92 X2 Y0 Z0 (This resets the origin to the workpiece’s X=2, Y=0 position.)
    3. Define the tool’s length offset (TOOL LENGTH COMPENSATION) in the controller settings. Use G43 H? (where H? is the tool’s length code, e.g., H1 for Tool 1). Measure the tool’s length from the spindle tip to the cutting edge (use a micrometer or caliper) and enter it in the controller’s tool database. This ensures the tool reaches the correct depth for each operation.
  3. Load and Understand Your G-Code File
    1. Open the G-code file in your CAM software or editor. Look for comments (lines starting with % or ; ) to explain the code. For example: % Part: Test Block, Material: Aluminum ; Move to X=5, Y=10, Z=50 to start machining G00 X5 Y10 Z50
    2. Check for missing variables like G17 (XY plane), G18 (XZ plane), or G19 (YZ plane). These define the current coordinate plane used for linear/movements. Omit them for defaults.
    3. Verify toolpaths for collisions. Use the preview feature in your CAM software to simulate the G-code. For example, in Fusion 360, select View > Simulation > G-Code Simulation to check if the tool touches the workpiece or toolchanger.
  4. Run the G-Code in Simulation Mode
    1. Before running the G-code on the machine, test it in simulation mode to avoid errors. Most CNC controllers support this via a software emulator or a tool like Mach3’s "Simulate" mode. In Fusion 360, click the Simulate button on the G-code tab.
    2. Pay attention to the following behaviors:
      • F-velocity control: The F command sets feedrate (mm/min or ipm). Ensure this is realistic for your tool and material (e.g., 200 mm/min for aluminum with an 8mm end mill). Using too high a feedrate can cause chattering or tool failure.
      • R-rapid speed: The G00 command is for rapid movement (no tool engagement). For example, G00 X10 Y20 moves the tool quickly to X=10, Y=20 without cutting.
      • T-tool change: The T command switches tools. Example: T1 M6 (This loads Tool 1, typically before a cut starts.)
  5. Run the G-Code on the Machine
    1. Start the machine in Manual Mode (if available) to observe tool movements. If using a linear axis (e.g., X/Y), ensure the ball screws are lubricated and free of debris to avoid backlash.
    2. Begin the G-code execution with the Run button in your controller software. For Mach3, click Run under the "Program" tab. If the G-code fails to run, check for errors in the output log (e.g., missing tool, incorrect Z offset).
    3. Monitor the spindle and axes during operation. A sudden stop or vibration may indicate a tool break, collision, or power issue. Pause immediately if the tool gets stuck.
    4. After completion, run a G28 to return to home position and reset the machine for the next job.

Common Mistakes

  • Incorrect Tool Length Offset: Forgetting to set or updating the G43 H? command can cause the tool to not reach the correct depth. Always verify the tool’s length in the controller’s database before running the G-code.
  • Feedrate Too High: Using excessive feedrates (e.g., 1,000 mm/min for aluminum) can lead to chipping, tool breakage, or poor surface finish. Refer to your tool’s datasheet for recommended speeds and feeds.
  • Missing Coordinate Planes: Omitting G17, G18, or G19 can cause the machine to interpret moves in the wrong plane, leading to errors like the tool cutting sideways instead of perpendicular.
  • Collisions or Overcutting: Simulating the G-code is critical. A tool that collides with the workpiece or toolchanger can damage the machine. Check for gaps in toolpaths or misaligned fixtures.
  • Ignoring Spindle Power Limits: Some machines limit spindle current (e.g., 10–30 amps). Exceeding this can fry the controller or motor. Monitor the spindle current in real-time during operation.

Tips for Better G-Code Use

  • Use Tool Compensation: For cylindrical tools, use G43 H? to compensate for the tool’s length. For end mills, use G43 H? with a positive value for length offset (e.g., H1 = 10 mm).
  • Optimize Feedrates: Feedrates are tool-dependent. Start with conservative values (e.g., 50–100 mm/min for aluminum) and increase gradually. For example, in Fusion 360, use the "Optimize Feedrates" tool to suggest values based on material and tool.
  • Enable Rapid Retracts: Use G00 Z+5 (or another safe Z-position) before retracting the tool to avoid collisions with the workpiece. This is especially important in live-axis machining (e.g., turning).
  • Document Your G-Code: Comment out sections as you modify them (e.g., ; Start Pocketing). This helps track changes and avoids overwriting critical parts of the code.
  • Use G-Code Subroutines: For repetitive tasks (e.g., drilling holes), define subroutines (e.g., M01 for start/stop) and call them with G49 or M02 (end of program).
  • Check for Missing Tools: If the G-code fails at a tool change (e.g., T2), verify the tool is loaded in the controller’s database. Use M06 (tool change) but ensure the correct tool is selected.

Troubleshooting Common Issues

  • Machine Won’t Start:
    1. Check the power supply and ensure the machine is plugged into a stable outlet.
    2. Restart the controller software (Mach3, CNC-Jr) and verify the G-code file is loaded correctly.
    3. Inspect the spindle and motor for overheating (use a thermal camera or multimeter for current).
    4. If the issue persists, test the machine in manual mode to confirm axes move freely.
  • Tool Won’t Engage:
    1. Verify the tool is loaded in the toolchanger and the spindle is not jammed.
    2. Check the T? command in the G-code matches the tool’s code in the controller’s database.
    3. If using a live-axis lathe (e.g., CNC4PC), ensure the G43 H? offset is correct for the turning tool.
  • G-Code Execution Errors:
    1. Review the controller’s output log for specific errors (e.g., "Tool 1 not found").
    2. Correct missing or incorrect commands (e.g., add G17 for XY plane).
    3. Simulate the G-code in software to identify logical errors (e.g., a move to negative Z).
  • Surface Finish Issues:
    1. Check the feedrate and spindle speed. Too high a speed or feedrate can cause chatter.
    2. Use a slower feedrate (e.g., 30–50 mm/min) for finishing passes. Adjust the F command accordingly.
    3. Ensure the tool is properly sharpened and aligned. A dull or misaligned tool can leave grooves or poor finish.

Frequently Asked Questions

What is the difference between G00 and G01 in G-code?

G00 is a rapid move (no tool engagement) to a position, while G01 is a linear move with tool engagement at a specified feedrate (e.g., G01 X5 Y10 F50 moves the tool at 50 mm/min). G00 is faster but doesn’t cut; G01 is slower but precise.

Do I need to know Python or CAD to use G-code?

No. G-code is interpreted by CNC software (e.g., Fusion 360) or firmware (e.g., Mach3). Most CAM programs auto-generate G-code from your CAD model. Learn basic CAD/CAM to export files, but the G-code commands themselves are simple (e.g., G00, G01).

What happens if I use an incorrect tool length in G-code?

The tool may not reach the correct depth, causing overcutting (undercutting) or collisions. Always measure the tool’s length in the controller’s database using G43 H?. Simulate the G-code to verify the toolpath.

Can I use G-code for both milling and turning on a CNC lathe?

Yes, but the commands differ. For turning, use commands like G41 (tool offset) and G02/G03 (circular interpolation). For milling, G01 handles linear moves. CNC lathes often use S for spindle speed (e.g., S3000 M03 for 3,000 RPM).

Why does my G-code run slow when I press "Run"?

Possible causes:
  • The spindle is not engaged (check for missing M03 for spindle start).
  • The feedrate (F) is set too low or the tool is not properly loaded.
  • The controller is buffering or the G-code file is corrupted. Restart the software and check the file path.

How do I save a G-code file for future use?

In most CAM programs (e.g., Fusion 360), save the G-code as a text file with a .gcode or .nc extension. For example: File > Save As > "Workpiece.gcode" Copy the file to your machine’s SD card or directly to the controller’s memory (if supported). Always store a backup in case of corruption.