What is CNC G-code? Basic Commands for Beginners, Usage Examples, and Troubleshooting


By JP.LUNYEECNC我的商店
9 min read

CNCのGコードとは?初心者向けの基本コマンド、使用例、トラブルシューティング

G-code is the instruction language that CNC machine tools use to follow programmed toolpaths. It tells CNC routers and milling machines where to move, how to move, and what machining operations to perform.

For beginners, understanding G-code helps explain how a digital design becomes a finished product and makes it easier to spot setup errors, unexpected movements, and incorrect cutting depths. CAM software usually generates G-code automatically, but knowing the basics lets you review the program before machining.

This guide covers basic G-code, a simple program example, how to troubleshoot common issues, and what to check before operating a CNC machine tool.

1. What Is G-code in CNC?

G-code is a set of instructions that a CNC controller interprets to control machine movements and machining operations. It can specify machining conditions such as position, movement type, feed rate, and spindle operation.

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

Difference Between G-code and M-code

In general, G-code defines movements and machining modes, while M-code controls auxiliary functions. This is a general rule; actual behavior varies by controller.

l G01: Moves in a straight line at the specified feed rate.

l M03: Usually commands the spindle to rotate clockwise.

l M05: Usually commands the spindle to stop.

l M30: Usually ends the program and may reset some states.

Since supported commands and their behavior vary, always check the manual for the CNC controller you are using.

2. How Does G-code Work?

A typical CNC workflow consists of three main stages:

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

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

Post-processing: Converts the calculated toolpath into a G-code format compatible with a specific CNC controller.

Workflow:

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

Errors can occur at any stage. Even if the original design is correct, problems can arise from incorrect tool dimensions, work coordinates, post-processor settings, or machine setup.

 

3. Basic G-code Commands for Beginners

Movement Commands

Command

Function

Common use

G00

Rapid positioning

Moving between positions

G01

Linear movement at the specified feed rate

Straight-line cutting

G02

Clockwise arc movement

Curved machining

G03

Counterclockwise arc movement

Curved machining

G00: Rapid Positioning

1  G90

2  G00 X10 Y10

In absolute coordinate mode, this commands a rapid move to X=10, Y=10 in the current coordinate system. Rapid movement is for positioning, not for normal cutting feeds. The tool may collide if it passes through material, clamps, or other obstructions.

G01: Linear Movement

1  G21

2  G94

3  G01 X50 Y10 F300

On a compatible controller, G21 selects millimeters, G94 selects feed per minute, and G01 commands a linear move to X=50, Y=10 at a programmed feed rate of 300 units per minute.

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

G02 and G03: Arc Movement

G02 and G03 usually command clockwise and counterclockwise arc movements. Depending on the controller, an arc is defined by center offsets such as I and J, or by a radius such as R. The active plane and arc-center mode also affect how the command is interpreted.

Check the manual for the specific controller before using unfamiliar arc commands. Incorrect syntax may cause the program to be rejected or produce an unintended toolpath.

Coordinate and Unit Commands

Command

Function

G20

Selects inches on compatible controllers

G21

Selects millimeters on compatible controllers

G90

Selects absolute positioning

G91

Selects incremental positioning

G90: Absolute Positioning

1  G90

2  G00 X50

Commands a move to X=50 in the current coordinate system.

G91: Incremental (Relative) Positioning

1  G91

2  G00 X50

Moves 50 units in the positive X direction from the current position. If the current X coordinate is 20, the destination is 70. By contrast, G90 X50 sets the destination to X=50.

Confusing the two modes can cause unexpected movement. Check the active mode before running unfamiliar code.

On compatible controllers, G20 and G21 determine the program units. Interpreting a program written in millimeters as inches results in movement distances that are very different from what you expect.

Feed, Spindle, and Program Commands

Command/letter

Function

F

Sets a feed-related value

S

Sets a spindle-speed-related value

G94

Selects feed per minute on compatible controllers

M03

Usually commands the spindle to rotate clockwise

M05

Usually commands the spindle to stop

M30

Usually ends the program

Example:

1  M03 S10000

On a compatible system, this usually commands the spindle to rotate clockwise at 10,000 RPM. Actual spindle operation depends on the hardware and controller settings.

With G94, F usually represents the distance traveled per minute. F may have a different meaning in other feed modes. Check the active feed mode and verify that the value is appropriate for the material, tool, and machine.

 

4. How to Read a CNC G-code Program

The following simplified example shows common commands used for linear machining.

Important: This is an educational example, not a ready-to-run machining program. It does not define all the coordinates, tool details, clearance distances, and safe states required for a real machine. Do not run it without adapting and validating it for your 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

Program Breakdown

Code

Meaning

G21

Selects millimeters

G90

Selects absolute positioning

G94

Selects feed per minute

G17

Selects the XY plane on compatible controllers

G00 Z5

Rapid move to Z=5

G00 X10 Y10

Positioning move to X=10, Y=10

M03 S10000

Commands the spindle to rotate clockwise

G01 Z-1 F100

Moves to Z=-1 at the controlled feed rate

G01 X50 Y10 F300

Linear move to X=50, Y=10

G00 Z5

Rapid move to Z=5

M05

Spindle stop

M30

End program

This program sets the units, coordinate system, feed rate, and plane, theninstructs a position move to Z=5 and a move to X=10, Y=10.

Next, it activates the spindle and moves toZ=-1 at the specified feed rate, then moves along the X-axis to X=50, Y=10. Because the second linear move does not specify a new Z coordinate, the corresponding modal operation usually maintains the previous Z position.

Finally,it instructs a rapid move to Z=5, spindle stop, and end program.

If the workpiece surface is correctlyset to Z=0 and the appropriate offset is considered, Z=-1 may indicate 1 mm below that reference. However, the actual cutting depth varies depending on the workpiece origin, tool setting, and other factors. Z=5 is not necessarily a safe clearance height.

What are modal commands?

Modal commands remain active until they are changed by another command or canceled by the controller's rules. For example,G90 usually remains active until another coordinate system mode is selected. G01 may also carry over to subsequent axis moves.

Therefore,you cannot always determine the command from a single line. Preceding commands and active modes affect subsequent movements.

 

5. What do X, Y, Z, F, and S mean?

Letter

General meaning

X, Y, Z

Linear axis coordinates

A, B, C

Additional axes or rotary axes, if configured

F

Feed rate or feed-related value

S

Spindle speed-related value

I, J, K

Usually arc center parameters

N

Any program line number

In a typical3-axis CNC router, X and Y control movement within the work plane, while Z controls vertical movement. Axis orientation and positive direction vary depending on machine settings.

Consider the following command.

1  G01 X50 Y10 F300

l G01 represents linear movement at the specified feed rate.

l X50 Y10 represents the destination in an active coordinate specification mode.

l F300 represents the feed value in an active feed mode.

This command alone does not specify the tool, spindle speed, cutting depth, or workpiece origin. These must be defined elsewhere in the program or in the machine settings.

6. CommonG-code issues and solutions

Issue

Check

Incorrect finished dimensions

Units, scale, workpiece origin, tool diameter, toolpath compensation

Unintended movement direction

G90/G91, workpiece coordinate system, axis settings

Incorrect cutting depth

Z zero point, workpiece offset, units, specified depth

Cannot load the program

Controller compatibility, post-processor, unsupported commands

Collision risk

Toolpath simulation, rapid move, clamps, machine work envelope

Incorrect feed rate

Active feed mode, units,F value, controller settings

Incorrect arc movement

G02/G03 syntax, active plane, arc parameters

Why is the cutting depth incorrect?

First,check the Z-axis zero point and workpiece coordinate offset. If the workpiece surface is set to Z=0, verify that this reference is correctly configured.

Next,compare the CAM settings with the generated program. Check the cutting depth, units, workpiece origin, and tool compensation. Do not randomly change the cutting depth to compensate for unexplained errors.

Why does the machine move in the wrong direction?

Check whether G90 or G91 is active, and verify the workpiece coordinate system and axis settings. Absolute coordinate mode specifies the destination, while relative coordinate mode specifies the amount of movement from the current position.

If there is a risk of collision, stop the machining, investigate the cause, and then resume.

Why can't the program be executed?

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

Check the controller manual, firmware, post-processor settings, and error messages. Do not carelessly delete unknown commands. They may be required for positioning, spindle control, or tool compensation.

Why is the arc toolpath incorrect?

Check the G02/G03 syntax, arc parameters, active plane, and arc center mode. Arc formats and coordinate rules vary depending on the controller.

Check the toolpath in the corresponding simulation software and compare the generated code with the manual. Simulation helps discover program errors, but it cannot verify all conditions of the actual machine.

7. How to check G-code before machining

Before running unfamiliar code, check the following.

1. Compatibility with the controller: Use a post-processor suitable for the target controller and machine.

2. Units and modes: Check inches/millimeters and absolute/relative coordinate specification.

3. Workpiece origin:Check the X, Y, and Z zero points and the applied offsets.

4. Toolpath: Check cutting movements, rapid moves, depth, and movement limits in the corresponding simulation software.

5. Machining conditions: Check whether the tool, feed rate, spindle speed, and cutting depth are suitable for the material and machine.

6. Machine setup: Secure the workpiece and tool, and ensure that the clamps do not interfere with the toolpath.

7. Machine operation procedures: Perform the necessary clearance checks and initial run procedures under supervision.

Simulation alone cannot guarantee the safety of the program. It may not fully reflect the actual tool length, fixture position, machine work envelope, or controller-specific behaviors. Check the condition of the actual machine before machining and follow the manufacturer's instructions.

8. Do CNC beginners need to learn G-code?

Most beginners do not need to enter G-code manually every time.CAM software generates code for engraving, pocketing, contouring, and other operations.

However, understanding the basics helps you check generated programs, diagnose unexpected behavior, verify coordinates, and identify compatibility issues.

Practical learning order:

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

2. Understand G20/G21 and G90/G91.

3. Learn how to interpret F and S values.

4. Read a short programline by line.

5. Practice checking coordinates, depth, and toolpaths in the corresponding software.

6. Learn controller-specific commands as needed.

 

Frequently Asked Questions

Is learning G-code difficult?

Beginners can learn the basics. Before tackling complex commands, start with movement commands, coordinates, units, and feed rates.

Can G-code be generated without manual entry?

Yes.CAM software can generate G-code from designs and selected machining processes. Select the corresponding post-processor and verify the program before machining.

Can the sameG-code be used on all CNC machines?

No. Supported commands, syntax, and machine-specific settings vary by controller. Check compatibility before running it.

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

Generally,G-code defines movement and machining modes, while M-code controls auxiliary functions such as spindle operation and program termination.

What does G90 mean in CNC?

G90 usually selects absolute coordinate specification. Coordinate values indicate the position in the active coordinate system, not the amount of movement from the current position.

What is the difference between G00 and G01?

G00 is usually used for rapid positioning, while G01 instructs linear movement at the specified feed rate.

Why does a CNC machine ignore G-code?

The command may be unsupported, the format may be incorrect, or the controller may interpret it differently. Check the manual, firmware, post-processor, and error messages.

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

A toolpath represents the path the tool takes.G-code expresses that path and related machining commands in a format that the corresponding controller can interpret.

Summary

G-code links CNC toolpaths with the machine movements that create parts. For beginners, the important things are identifying basic commands, understanding coordinate modes, reading short programs, and checking settings before machining.

The goal is not to memorize every command. It is to understand what the program instructs the machine to do and to recognize when additional checks of the code, controller, or actual machine settings are necessary.

Because G-code support and operation vary depending on the control system, always refer to the manuals for the target CNC controller and machine.


Leave a comment