What Is G-Code in CNC? Essential Commands, Examples, and Troubleshooting for Beginners

What Is G-Code in CNC? Essential Commands, Examples, and Troubleshooting for Beginners

G-code is the instruction language used by CNC machines to follow programmed toolpaths. It tells a CNC router or milling machine where to move, how to move, and which machining operations to perform.

For beginners, understanding G-code helps explain how a digital design becomes a finished part and makes it easier to identify setup errors, unexpected movements, and incorrect cutting depths. CAM software usually generates G-code automatically, but knowing the basics helps operators check programs before machining.

This guide covers essential G-code commands, a simple program example, common troubleshooting steps, and the checks beginners should complete before running a CNC machine.

1. What Is G-Code in CNC?

G-code is a set of instructions interpreted by a CNC controller to coordinate machine movements and machining operations. Depending on the controller, commands can specify positions, movement types, feed rates, spindle operations, and other machining parameters.

G-code is used in CNC routers, milling machines, lathes, and other computer-controlled machine tools. Common applications include engraving, pocketing, contour cutting, and milling wood, plastic, and metal.

G-Code vs. M-Code

G-codes commonly define movement and machining modes, while M-codes commonly control auxiliary functions. These are general conventions, and exact behavior depends on the controller.

lG01: Commands linear movement at a programmed feed rate.

lM03: Commonly requests clockwise spindle operation.

lM05: Commonly requests the spindle to stop.

lM30: Commonly ends a program and may reset certain program states.

Always check the documentation for the intended CNC controller because supported commands and their behavior can vary.

2. How Does G-Code Work?

A typical CNC workflow has three main stages:

CAD (Computer-Aided Design): Creates the geometry of a part, such as an engraved sign or an aluminum component.

CAM (Computer-Aided Manufacturing): Converts the design into machining operations. The operator selects the cutter, toolpath strategy, cutting depth, feed rate, and other parameters.

Post-processing: Converts the calculated toolpath into G-code formatted for a particular CNC controller.

The workflow is:

CAD design → CAM toolpath → Post-processor → G-code → CNC controller → Machined part

Errors can occur at any stage. Incorrect cutter dimensions, workpiece coordinates, post-processor settings, or machine configuration can cause problems even when the original design is accurate.

LUNYEECNC 

3. Essential G-Code Commands for Beginners

Movement Commands

Command

Function

Typical use

G00

Rapid positioning

Moving between locations

G01

Linear movement at a programmed feed rate

Cutting straight paths

G02

Clockwise arc movement

Machining curves

G03

Counterclockwise arc movement

Machining curves

G00: Rapid positioning

1  G90

2  G00 X10 Y10

With absolute positioning active, this requests rapid movement toward X = 10 and Y = 10 in the active coordinate system. Rapid moves are intended for positioning, not ordinary controlled cutting. They can still cause collisions if the cutter passes through the workpiece, clamps, or other obstacles.

G01: Linear movement

1  G21

2  G94

3  G01 X50 Y10 F300

On a compatible controller, G21 selects millimeter units, G94 selects feed per minute, and G01 requests linear movement toward X = 50 and Y = 10 at a programmed feed rate of 300 units per minute.

The appropriate feed rate depends on the material, cutter, spindle speed, cutting depth, and machine capabilities.

G02 and G03: Arc movement

G02 and G03 commonly command clockwise and counterclockwise arcs. Depending on the controller, the arc may be defined using center-offset parameters such as I and J or a radius parameter such as R. The active plane and arc-center mode can also affect interpretation.

Check the intended controller's documentation before using unfamiliar arc commands. Incorrect arc syntax can cause a program to be rejected or produce an unintended toolpath.

Coordinate and Unit Commands

Command

Function

G20

Selects inch units on compatible controllers

G21

Selects millimeter units on compatible controllers

G90

Selects absolute positioning

G91

Selects incremental positioning

G90: Absolute positioning

1  G90

2  G00 X50

This requests movement toward X = 50 in the active coordinate system.

G91: Incremental positioning

1  G91

2  G00 X50

This requests movement 50 units in the positive X direction from the current position. If the current X position is 20, the requested destination is 70. In contrast, G90 X50 requests a destination of X = 50.

Confusing these modes can cause unexpected movement. Confirm which mode is active before running unfamiliar code.

G20 and G21 determine the programmed units on compatible controllers. If a program intended for millimeters is interpreted in inches, movement distances can be dramatically different from what the operator expects.

Feed, Spindle, and Program Commands

Command or letter

Function

F

Specifies a feed-related value

S

Specifies a spindle-speed-related value

G94

Selects feed per minute on compatible controllers

M03

Commonly requests clockwise spindle operation

M05

Commonly requests spindle stop

M30

Commonly ends a program

For example:

1  M03 S10000

This commonly requests clockwise spindle operation at a programmed speed of 10,000 RPM on a compatible system. Actual spindle behavior depends on the hardware and controller configuration.

Under G94, F commonly represents distance per minute. Other feed modes may interpret F differently. Always check the active feed mode and confirm that the values suit the material, cutter, and machine.

LUNYEECNC 

4. How to Read a CNC G-Code Program

The following simplified example demonstrates common commands for a straight-line machining movement.

Important: This is a learning example, not a ready-to-run machining program. It does not establish every coordinate, tool, clearance, or safety state required by a real machine. Do not execute it directly without adapting and verifying it for the specific CNC system.

1  G21

2  G90

3  G94

4  G17

5  G00 Z5

6  G00 X10 Y10

7  M03 S10000

8  G01 Z-1 F100

9  G01 X50 Y10 F300

10  G00 Z5

11  M05

12  M30

Understanding the Program

Code

Meaning

G21

Selects millimeter units

G90

Selects absolute positioning

G94

Selects feed per minute

G17

Selects the XY plane on compatible controllers

G00 Z5

Requests rapid movement to Z = 5

G00 X10 Y10

Requests positioning at X = 10, Y = 10

M03 S10000

Requests clockwise spindle operation

G01 Z-1 F100

Requests controlled movement to Z = -1

G01 X50 Y10 F300

Requests linear movement toward X = 50, Y = 10

G00 Z5

Requests rapid movement to Z = 5

M05

Requests spindle stop

M30

Commonly ends the program

The program establishes its unit, positioning, feed, and plane modes before requesting positioning at Z = 5 and moving to X = 10, Y = 10.

It then requests spindle operation, moves to Z = -1 at a controlled feed, and travels along the X-axis toward X = 50, Y = 10. Because the second linear move does not specify a new Z coordinate, the previous Z position normally remains active under compatible modal behavior.

Finally, the program requests a rapid move to Z = 5, stops the spindle, and ends.

If the workpiece surface is correctly established at Z = 0 and applicable offsets are accounted for, Z = -1 may represent a position 1 mm below that reference. However, actual cutting depth depends on the work origin, tool setup, and other settings. Z = 5 is not automatically a safe clearance height.

What Are Modal Commands?

Modal commands remain active until another command changes them or the controller's rules cancel them. For example, G90 generally remains active until another positioning mode is selected. G01 may also remain active for subsequent axis movements.

Therefore, a line cannot always be interpreted in isolation. Earlier commands and active modes can affect how later instructions are executed.

LUNYEECNC 

5. What Do X, Y, Z, F, and S Mean?

Letter

Common meaning

X, Y, Z

Linear axis coordinates

A, B, C

Additional or rotary axes, if configured

F

Feed rate or another feed-related value

S

Spindle-speed-related value

I, J, K

Commonly used for arc-center parameters

N

Optional program line number

On a typical three-axis CNC router, X and Y control movement across the work area, while Z controls vertical movement. Axis orientation and positive directions depend on the machine configuration.

Consider this command:

1  G01 X50 Y10 F300

lG01 specifies linear movement at the programmed feed rate.

lX50 Y10 specifies the destination under the active positioning mode.

lF300 specifies the feed value under the active feed mode.

The command does not independently specify the cutter, spindle speed, cutting depth, or workpiece zero. Those details must be established elsewhere in the program or machine setup.

6. Common G-Code Problems and Troubleshooting

Problem

What to check

Incorrect finished dimensions

Units, scale, work origin, cutter diameter, and toolpath compensation

Unexpected movement direction

G90/G91, work coordinate system, and axis configuration

Incorrect cutting depth

Z zero, work offsets, units, and programmed depth

Program will not load

Controller compatibility, post-processor, and unsupported commands

Collision risk

Toolpath simulation, rapid moves, clamps, and machine limits

Incorrect feed rate

Active feed mode, units, F value, and controller settings

Incorrect circular movement

G02/G03 syntax, active plane, and arc parameters

Why Is the Cutting Depth Incorrect?

Check the Z-axis zero and work coordinate offsets first. If the workpiece surface should be Z = 0, verify that the reference was established correctly.

Then compare the CAM settings with the generated program. Confirm the intended cutting depth, units, work origin, and applicable tool compensation. Do not change the programmed depth at random to compensate for an unexplained error.

Why Does the Machine Move in the Wrong Direction?

Check whether G90 or G91 is active, then verify the work coordinate system and axis configuration. Absolute mode identifies a destination, while incremental mode specifies a displacement from the current position.

If the movement could cause a collision, stop the job and investigate before continuing.

Why Will the Machine Not Run the Program?

The program may contain unsupported commands or have been generated with a post-processor intended for a different controller.

Check the controller documentation, firmware, post-processor settings, and reported errors. Do not remove unfamiliar commands at random because they may be necessary for positioning, spindle control, or tool compensation.

Why Is the Circular Toolpath Incorrect?

Check the G02/G03 syntax, arc parameters, active plane, and arc-center mode. Controllers may use different arc formats or coordinate conventions.

Inspect the toolpath in compatible simulation software and verify the generated code against the controller documentation. Simulation can reveal programming errors, but it cannot verify every physical condition.

7. How to Check G-Code Before Machining

Before running unfamiliar code, complete these checks:

1. Confirm controller compatibility. Use a post-processor appropriate for the intended controller and machine.

2. Verify units and modes. Check inches versus millimeters and absolute versus incremental positioning.

3. Confirm the work origin. Verify X, Y, and Z zero positions and applicable offsets.

4. Review the toolpath. Inspect cutting moves, rapid movements, depths, and travel limits in compatible simulation software.

5. Check machining parameters. Confirm that the cutter, feed rate, spindle speed, and cutting depth suit the material and machine.

6. Inspect the physical setup. Secure the workpiece and cutter, and ensure clamps do not obstruct the toolpath.

7. Follow the machine's operating procedure. Complete required clearance checks and supervised first-run procedures.

Simulation cannot guarantee that a program is safe. It may not reflect actual tool length, fixture placement, machine limits, or every controller-specific behavior. Inspect the physical setup and follow the machine manufacturer's instructions before machining.

8. Do CNC Beginners Need to Learn G-Code?

Most beginners do not need to write G-code manually for every project. CAM software commonly generates code for engraving, pocketing, contour cutting, and other operations.

Understanding basic G-code is still useful when checking generated programs, diagnosing unexpected movements, verifying coordinates, and identifying compatibility issues.

A practical learning order is:

1. Recognize G00, G01, G02, and G03.

2. Understand G20/G21 and G90/G91.

3. Learn how F and S values are interpreted.

4. Read a short program line by line.

5. Practice checking coordinates, depths, and toolpaths in compatible software.

6. Learn controller-specific commands as needed.

LUNYEECNC 

Frequently Asked Questions

Is G-code difficult to learn?

The basics are manageable for beginners. Start with movement commands, coordinates, units, and feed rates before learning more complex instructions.

Can I generate G-code without writing it manually?

Yes. CAM software commonly generates G-code from a design and selected machining operations. Choose a compatible post-processor and verify the resulting program before machining.

Can the same G-code run on every CNC machine?

No. Controllers differ in supported commands, syntax, and machine-specific settings. Confirm compatibility before running a program.

What is the difference between G-code and M-code?

G-codes commonly define motion and machining modes, while M-codes commonly control auxiliary functions such as spindle operation and program termination.

What does G90 mean in CNC?

G90 commonly selects absolute positioning. Coordinate values refer to positions in the active coordinate system rather than movements relative to the current position.

What is the difference between G00 and G01?

G00 is generally used for rapid positioning, while G01 commands linear movement at the programmed feed rate.

Why does my CNC machine ignore a G-code command?

The command may be unsupported, incorrectly formatted, or interpreted differently by the controller. Check the controller documentation, firmware, post-processor, and reported errors.

What is the difference between G-code and a toolpath?

A toolpath describes the route the cutter should follow. G-code expresses that route and related machining instructions in a format a compatible CNC controller can interpret.

Conclusion

G-code connects a CNC toolpath to the machine movements that produce a part. For beginners, the most valuable skills are recognizing common commands, understanding coordinate modes, reading a short program, and checking the setup before machining.

The goal is not to memorize every instruction. It is to understand what the program tells the machine to do and recognize when the code, controller, or physical setup needs further verification.

Always consult the documentation for the specific CNC controller and machine because G-code support and behavior vary across control systems.

ก่อนหน้า ถัดไป