CNC G Code Basics: A Clear Beginner Guide

CNC G-code is a language that tells a CNC machine how to move, cut, drill, and position its tools. It uses simple letter and number commands to control machine movements, speeds, and other functions.

Across the United States, CNC machines play a major role in manufacturing. American machine shops use them to make metal parts, wood products, plastic parts, signs, and custom components. Small workshops and large factories both rely on computer-controlled tools to produce accurate work. G-code is one of the main ways these machines receive instructions.

CNC G-code may look like a long list of letters and numbers at first. However, each command has a clear job. Some commands move a tool. Others set a cutting speed, choose a work position, or control the spindle. Once you understand the basic rules, a CNC program becomes much easier to read.

This guide explains the main G-code commands, common M-codes, coordinate settings, and basic program structure. It also includes simple examples and a sample cutting sequence. You will learn what each command does and why it matters before you try to use it on a real machine.

One key point comes first: CNC controllers do not all use the exact same code rules. A command that works on one machine may act differently on another. Always check the manual for your exact controller and machine before running a program.

G-Code for CNC Programming | Fusion 360 Blog | Cnc programming, Cnc ...
Source: pinterest.ca

What Is CNC G-Code?

CNC G-code is a programming language used to control computer numerical control machines. It gives the machine a set of instructions in a clear order. These instructions tell the machine where to move, how fast to move, and what action to perform.

The name G-code comes from the use of G commands in the language. These commands often control movement and machine modes. A CNC program can also contain M-codes, which control other machine functions.

A CNC controller reads the program one block at a time. A block is usually one line of code. The controller reads the commands and turns them into machine actions. It checks the current machine state before carrying out each move.

G-code is common in CNC mills, lathes, routers, plasma cutters, and other machine tools. The exact command set depends on the machine and controller. Many systems use a language based on RS274, but they may add their own features.

G Code And M Codes For CNC Programming Pdf | G code examples, Cnc ...
Source: jp.pinterest.com
  • Movement control

    G-code tells the machine to move along an axis or follow a programmed path.

  • Cutting control

    Feed and spindle commands help set the conditions for a machining operation.

  • Position control

    Coordinate commands help the machine find the correct part location.

  • Machine operation

    Other commands help manage tools, coolant, pauses, and program endings.

How a CNC Controller Reads G-Code

A CNC controller reads each line and checks its words. It then works out the requested action based on the active machine modes. For example, G01 X2.0 Y1.0 asks for a straight move to the stated X and Y position at the active feed rate.

The controller also keeps track of settings that remain active. These settings are called modal commands. A command can stay active even after its line ends. This is why a programmer must understand which settings carry over to later lines.

  • Program block

    One line of CNC instructions that may contain several words.

  • Command word

    A letter and number combination that gives an instruction.

  • Controller

    The electronic system that reads the program and controls machine movement.

Understanding the Structure of a G-Code Line

A G-code line contains letters and numbers that work together. Each letter has a specific meaning. The number after it gives a value or selects a command. A line can contain a movement command, axis positions, a feed rate, or other settings.

For example, G01 X2.0 Y1.0 F20.0 contains three main parts. G01 tells the machine to make a straight move. X2.0 and Y1.0 give the destination. F20.0 sets the feed rate to 20 units per minute when G94 feed-per-minute mode is active.

The meaning of a number depends on the command before it. An X value gives an X-axis position. An F value gives a feed rate. An S value often sets spindle speed. A T value selects a tool number.

Spaces make code easier to read. Most common controllers allow spaces between words. Line numbers are also common, but they are not always required. Comments can explain the purpose of a line without changing the machining action.

  • G

    Usually identifies a preparatory command, such as a movement or machine mode.

  • M

    Identifies a miscellaneous machine function.

  • X, Y, and Z

    Identify the main linear machine axes.

  • F

    Sets the programmed feed rate.

  • S

    Usually sets spindle speed.

  • T

    Selects a tool number.

  • N

    Can identify a program line number.

A Simple G-Code Line Example

Consider this example: G01 X3.0 Y2.0 F15.0. It asks the machine to move in a straight line to X3.0 and Y2.0. The programmed feed rate is 15 units per minute when the controller uses the usual feed-per-minute mode.

The units depend on the active unit setting. If the program uses inches, the coordinates are in inches. If it uses millimeters, the coordinates are in millimeters. This is why unit selection must be clear before a program starts.

  • G01

    Selects straight-line feed movement.

  • X3.0 Y2.0

    Defines the destination on the X and Y axes.

  • F15.0

    Sets the feed rate to 15 units per minute in G94 mode.

What Are G-Code Comments?

Comments help people understand a program. They can explain a tool change, a cutting pass, or a safe positioning move. On many RS274-based systems, comments can appear inside parentheses or after a semicolon.

For example, G00 X0 Y0 (Move to start position) contains a rapid movement command and a note. The comment does not tell the machine to move. It only helps the operator read the program. Check your controller manual because comment rules can vary.

  • Use clear notes

    Describe the purpose of a tool change or important movement.

  • Avoid unclear wording

    Write comments that help another operator understand the program.

Essential G-Code Commands Every Beginner Should Know

A small group of G-codes forms the base of many CNC programs. These commands control rapid movement, cutting movement, arcs, units, coordinate modes, and other important settings. Learning them first makes more complex programs easier to understand.

The following table lists common commands found on many milling and router controllers. Their exact behavior and availability must still be checked against the machine manual.

G01 G-Code: CNC Linear Interpolation Command Guide
Source: etcnmachining.com
  • G00

    Rapid positioning. Moves the machine to a destination at its rapid rate. It is usually used when the tool is not cutting.

  • G01

    Linear interpolation. Moves the tool in a straight line at the programmed feed rate.

  • G02

    Circular interpolation in a clockwise direction on the selected plane.

  • G03

    Circular interpolation in a counterclockwise direction on the selected plane.

  • G04

    Dwell. Holds the machine for a set time when supported by the controller.

  • G17

    Selects the XY plane on common milling controllers.

  • G18

    Selects the XZ plane.

  • G19

    Selects the YZ plane.

  • G20

    Selects inch units on common controllers.

  • G21

    Selects millimeter units on common controllers.

  • G40

    Cancels cutter radius compensation on common milling controllers.

  • G43

    Applies tool length compensation on supported machines.

  • G49

    Cancels tool length compensation on common milling controllers.

  • G54

    Selects a stored work coordinate system.

  • G80

    Cancels an active canned machining cycle on common controllers.

  • G90

    Selects absolute positioning.

  • G91

    Selects incremental positioning.

G00: Rapid Movement

G00 moves the machine to a new position at the available rapid rate. This command is useful for moving between cutting areas. It helps reduce the time spent on movements that do not remove material.

Rapid motion is not a cutting feed. The machine may move at a high speed, so the programmer must check the path for clamps, stock, fixtures, and other obstacles. Never assume a rapid move is safe just because the tool is above the part in the program preview.

  • Example

    G00 X1.0 Y1.0 moves toward the programmed X and Y position.

  • Main use

    Positioning the tool before a cutting operation.

G01: Straight-Line Cutting Movement

G01 makes a straight move at the active feed rate. It is one of the most common commands in CNC machining. It can move along one axis or several axes at the same time.

For example, G01 X4.0 Y2.0 F12.0 commands a straight move to the stated destination. The actual path and feed behavior depend on the controller, active units, and axis settings. Choose a feed rate that suits the tool, material, and machine.

  • Example

    G01 X4.0 F10.0 moves in a straight line along the X axis at the programmed feed rate.

  • Main use

    Cutting straight edges, slots, and other linear paths.

G02 and G03: Arc Movement

G02 and G03 make circular movements. G02 normally moves clockwise, while G03 normally moves counterclockwise in the selected plane. These commands help create circles, rounded corners, and curved toolpaths.

Arc commands often use I, J, and K values to describe the arc center relative to the starting point. Some controllers also support an R value for the arc radius. The correct format depends on the controller and the selected arc distance mode.

A poorly defined arc can cause an alarm or an incorrect toolpath. Check the start point, end point, center, radius, and direction before running the program.

  • G02

    Creates a clockwise arc in the selected plane.

  • G03

    Creates a counterclockwise arc in the selected plane.

  • I, J, and K

    Can define the arc center offsets for the relevant axes.

Understanding M-Codes in CNC Programming

M-codes control machine functions that are not usually direct axis movements. They are called miscellaneous codes. Common uses include turning the spindle on or off, switching coolant on or off, changing tools, and ending a program.

G-codes and M-codes often work together. A G-code may position the tool, while an M-code starts the spindle. A feed command can then control the cutting movement. The controller follows its own rules for how commands on the same line are handled.

Not every machine supports every M-code. Some machines use custom M-codes for special equipment or automated functions. Always check the official machine manual before using an unfamiliar command.

CNC Turning G-Codes - RenAnSoft CNC Training
Source: renancnc.com
  • M00

    Stops the program and requires the operator to resume it.

  • M01

    Optional stop. The stop depends on the machine’s optional-stop setting.

  • M03

    Starts the spindle in the clockwise direction on common machines.

  • M04

    Starts the spindle in the counterclockwise direction where supported.

  • M05

    Stops the spindle.

  • M06

    Commands a tool change on machines that use this convention.

  • M08

    Turns coolant on where supported.

  • M09

    Turns coolant off on common milling controllers.

  • M30

    Ends the program and commonly resets it for another run.

How G-Codes and M-Codes Work Together

Think of G-codes as instructions for the toolpath and machine modes. Think of M-codes as instructions for other machine functions. Both types are needed in many machining programs.

For example, a program may use T1 M06 to request a tool change. It may then use S1500 M03 to start the spindle at the programmed speed. G01 can then move the tool along the cutting path.

This is only a general example. Spindle speed limits, tool change rules, and command order differ across machines. A lathe, mill, and CNC router may also use different settings.

  • G-code example

    G01 controls a straight feed move.

  • M-code example

    M03 starts a clockwise spindle on common milling machines.

Why M-Code Differences Matter

A common mistake is to assume that an M-code always has the same function on every CNC machine. Machine builders can assign special functions to certain M-codes. A command used for one accessory may have a different purpose on another machine.

Before using an M-code, find its meaning in the controller and machine documentation. This is especially important for automatic doors, tool changers, pallet systems, probes, and auxiliary equipment.

  • Check the machine manual

    Confirm the command’s exact function and required conditions.

  • Check machine options

    Some commands only work when the required hardware is installed.

Absolute and Incremental Positioning: G90 and G91

G90 and G91 control how the machine reads position values. G90 selects absolute positioning. G91 selects incremental positioning. Understanding this difference is essential because it changes where the machine moves after each command.

In absolute mode, the programmed coordinates refer to the active work coordinate system. For example, X2.0 means the target is X2.0 from the selected work zero. The machine does not simply add two units to its current position.

In incremental mode, the programmed values describe movement from the current position. For example, G91 X1.0 asks for a one-unit move in the positive X direction from where the axis is now.

A program can use both modes, but the programmer must control each change carefully. Forgetting to switch back to the intended mode can send the tool to an unexpected position.

CNC G-Code Examples Every Beginner Must Know – CNC Programming Hub
Source: cncprogramminghub.com
  • G90

    Uses the programmed coordinates as positions relative to the active coordinate system.

  • G91

    Uses the programmed coordinates as movements relative to the current position.

Simple Absolute Positioning Example

Assume the work zero is at X0 Y0. The program contains G90 followed by G01 X2.0 Y1.0. The machine moves toward the absolute position X2.0 Y1.0 in the active work coordinate system.

If the next line says G01 X4.0 Y1.0, the target becomes X4.0 Y1.0. The machine does not add four units to the previous X position.

  • Starting point

    X0 Y0.

  • First target

    X2.0 Y1.0.

  • Second target

    X4.0 Y1.0.

Simple Incremental Positioning Example

Now assume the machine starts at X0 Y0 and uses G91. A command of G01 X2.0 Y1.0 moves two units in X and one unit in Y. The new position is X2.0 Y1.0 relative to the starting point.

A second command of G01 X2.0 Y0.0 moves another two units in X. The new position is X4.0 Y1.0. Each movement adds to the current position.

  • First movement

    Move X2.0 and Y1.0 from the current position.

  • Second movement

    Move another X2.0 from the new position.

When Should You Use Each Mode?

Absolute positioning is useful when a program uses fixed locations on a part. It makes it easier to see where the tool should go. Incremental positioning can be useful for repeated movements, step patterns, and certain cycles.

Neither mode is always better. Choose the one that makes the program clear and easy to check. Keep the active mode visible in the program and confirm it in the simulation.

  • Use G90

    When fixed part coordinates are the main reference.

  • Use G91

    When each move is based on the current position.

CNC Coordinate Systems and Work Offsets

A CNC machine needs a clear reference point before it can move to the right part location. Coordinate systems provide that reference. They tell the controller how the programmed X, Y, and Z positions relate to the machine and the workpiece.

Machine coordinates describe positions in the machine’s own reference system. Work coordinates describe positions relative to a selected work zero. This work zero is often set on a chosen corner, edge, or surface of the stock.

G54 is a common work coordinate selection on milling machines. Other systems may include G55, G56, G57, G58, and G59. These systems allow operators to store different part locations or setups.

A correct work offset helps the machine place the tool in the right position. An incorrect offset can shift the entire toolpath. This can cause a bad cut, damage to the workpiece, or a collision.

N.RAJESHKANNA, (Advanced CNC Machining Technician): CNC PREPARATORY ...
Source: nrajeshkannaato.blogspot.com
  • Machine zero

    The reference position used by the machine’s coordinate system.

  • Work zero

    The selected reference point on the part or stock.

  • G54

    Selects a common stored work coordinate system.

  • G55 to G59

    Select additional work coordinate systems where supported.

How to Set a Work Coordinate

The work zero must match the setup used when the program was created. First, inspect the part drawing and decide where the work zero belongs. Then secure the stock and use the approved probing or measurement method to find the reference point.

Enter or verify the measured values in the correct work offset. Check the displayed coordinates before starting the program. If the machine uses a probe, follow the probe maker’s instructions and the machine’s approved procedure.

Never change an offset to hide a programming error. Find the cause first. An unexpected offset change can move every programmed position and create a new hazard.

  • Check the drawing

    Confirm the intended part zero and axis directions.

  • Measure the stock

    Use a suitable tool or approved probing method.

  • Verify the offset

    Compare the stored values with the actual setup.

  • Check the first position

    Confirm the tool’s expected location before cutting.

Why Tool Length Offsets Matter

A tool length offset tells the controller how long a tool is compared with its reference. This helps the machine place the tool at the correct Z position. On many milling controllers, G43 activates tool length compensation, often with an H number that selects an offset.

For example, G43 H01 selects the tool length offset associated with H01 on a controller that uses this format. The value must match the actual tool and setup. A wrong offset can make the tool cut too deep or stay above the intended surface.

  • Tool number

    Identifies the selected cutting tool.

  • H number

    Selects a stored tool length offset on supported machines.

  • Verification

    Confirms that the offset matches the installed tool.

Units, Feed Rates, and Spindle Speed

A CNC program needs the correct units and cutting settings. These settings affect tool movement, cutting force, surface finish, and machining time. A mistake in any of them can cause a poor result or damage.

G20 and G21 select the unit system on many common CNC controllers. G20 selects inches, while G21 selects millimeters. The program must use the same units as the intended coordinates and cutting values.

The F word sets the programmed feed rate. The S word commonly sets spindle speed. The controller uses the active feed mode to interpret F. In G94 mode, F commonly means units per minute. In G95 mode, it commonly means units per spindle revolution.

The correct feed and spindle settings depend on the material, tool type, tool diameter, flute count, machine power, tool engagement, and cutting method. Do not copy values from an unrelated program without checking these conditions.

  • G20

    Selects inch units on common controllers.

  • G21

    Selects millimeter units on common controllers.

  • F

    Sets the programmed feed rate.

  • S

    Sets the programmed spindle speed where supported.

  • G94

    Selects feed per minute on common controllers.

  • G95

    Selects feed per spindle revolution on common controllers.

How to Understand Feed Rate

Feed rate describes how fast the cutting tool moves through the programmed path. It is not the same as spindle speed. A feed rate that is too high may overload the tool or machine. A feed rate that is too low may cause rubbing, excess heat, or poor tool life.

For example, F10.0 in G94 mode may mean 10 inches per minute when G20 is active. If G21 is active, the same number may mean 10 millimeters per minute. The unit mode changes the meaning of the value.

  • Check the units

    Confirm whether the program uses inches or millimeters.

  • Check the feed mode

    Confirm whether feed is measured per minute or per revolution.

  • Check tool limits

    Use settings suitable for the cutter and material.

How to Understand Spindle Speed

Spindle speed is commonly measured in revolutions per minute, or RPM. The S word gives the programmed spindle speed. For example, S2000 requests 2,000 RPM on a machine that interprets S in the usual way.

The correct RPM depends on the cutter, material, cutting diameter, and machine limits. A higher spindle speed does not always produce a better cut. The tool maker’s cutting data is a useful starting point, but the machine setup must also be considered.

  • S value

    Sets the requested spindle speed.

  • M03

    Commonly starts clockwise spindle rotation.

  • M05

    Stops spindle rotation.

Common CNC G-Code Program Structure

A CNC program usually follows a clear sequence. It begins with preparation commands, sets the required machine modes, selects the tool, starts the spindle, and performs the programmed movements. It then shuts down the relevant functions and ends the program.

The exact order depends on the machine and operation. A milling program may need a tool change and length offset. A router program may use different spindle controls. A lathe program may need different axis conventions and spindle modes.

A clear program structure helps the operator understand what will happen next. It also makes errors easier to find. A consistent startup section is especially useful because it can set important modes instead of relying on settings left by a previous program.

  • Program identification

    A program number or name helps identify the job.

  • Startup settings

    Select units, plane, positioning mode, work offset, and other required states.

  • Tool setup

    Select the tool and apply the correct compensation.

  • Cutting operation

    Run the required positioning and cutting movements.

  • Shutdown

    Stop the spindle and coolant as needed.

  • Program end

    Use the correct end command for the controller.

A Simple Illustrative CNC Program

The following example shows the general layout of a simple milling program. It is an educational example, not a ready-to-run cutting program. It does not define a real machine, tool, stock size, fixture, safe height, cutting strategy, or verified offsets.

The example uses common milling commands. The controller may require different startup commands, tool change rules, spindle settings, or end codes. Never run this sample directly on a machine.

  • Program number

    O1000 identifies the example program on controllers that use this format.

  • Unit selection

    G20 selects inch units on common milling controllers.

  • Position mode

    G90 selects absolute positioning.

  • Movement

    G00 and G01 demonstrate rapid and feed movement.

Example Program Code

A basic example may look like this: O1000, G17 G20 G40 G49 G54 G80 G90 G94, T1 M06, S1000 M03, G00 X0 Y0, G01 X1.0 F5.0, M05, M30.

The first line identifies the program. The startup line selects the XY plane, inch units, compensation states, work coordinate, canned-cycle cancellation, absolute positioning, and feed-per-minute mode. The tool change and spindle commands follow. The movement commands show a rapid position and a straight feed move.

The example does not include a complete safe Z approach, a real cutting depth, or verified tool compensation. It must not be treated as a finished machining cycle. A qualified operator must adapt and verify every required setting for the actual machine.

  • G17

    Selects the XY plane.

  • G20

    Selects inch units.

  • G54

    Selects the first common work coordinate system.

  • G94

    Selects feed per minute.

  • M30

    Ends the example program on common controllers.

How to Write Your First Basic G-Code Program

The best way to learn programming is to begin with a simple movement task. Do not start with a complex part or a deep cutting operation. First, learn how the machine reads positions and movement commands.

You should have access to the correct controller manual, a suitable programming editor or CAM system, and a machine simulation. You also need to understand the machine’s coordinate system and basic safety controls.

Start with a simple path that contains a few movements. Check each command and make sure the toolpath matches the intended shape. Only move to a real cutting test after the program and setup have passed the required checks.

  • Prerequisite 1

    Know the machine model and controller type.

  • Prerequisite 2

    Understand the X, Y, and Z axis directions.

  • Prerequisite 3

    Know the active work zero and machine limits.

  • Prerequisite 4

    Have a verified tool and workholding setup.

Step 1: Choose a Simple Machining Task

Choose a basic task, such as moving a tool between two safe positions. A simple rectangular path is also useful for learning. Keep the first exercise small and avoid complicated curves or multiple tool changes.

Write down the start point, end point, work zero, and intended movement direction. This gives you a clear plan before you enter any code.

  • Keep the task small

    Use a simple shape or movement sequence.

  • Define the reference

    Identify the work zero and coordinate directions.

Step 2: Select Units and Positioning Mode

Choose the correct unit system. Use G20 for inch programming or G21 for metric programming on controllers that support these standard commands. Then select G90 or G91 based on the way you want to define positions.

Use one clear coordinate approach for the first exercise. Absolute positioning is often easier for beginners because every target can be compared with the drawing.

  • Set units

    Confirm the unit system used by the drawing and machine setup.

  • Set position mode

    Choose absolute or incremental positioning.

Step 3: Add Movement Commands

Use G00 for a suitable rapid positioning move and G01 for a feed movement. Add the required axis coordinates after each movement command. Keep the path simple enough to inspect by eye.

Do not place a cutting move at an unsafe height. A real machining program needs a verified approach and retract plan. Consider clamps, fixtures, stock height, and tool length before selecting any position.

  • Position

    Use a safe, verified rapid path.

  • Move

    Use a suitable feed movement for the planned operation.

Step 4: Set Cutting Conditions

If the exercise includes a cutting operation, choose the spindle speed and feed rate using suitable tool data and machine limits. Add the correct tool selection and compensation commands where required.

Do not guess the cutting values. Check the cutter maker’s data and consider material, tool diameter, tool engagement, machine rigidity, and workholding.

  • Feed rate

    Select a suitable value for the tool and material.

  • Spindle speed

    Stay within tool and machine limits.

  • Tool offset

    Verify the correct stored tool measurement.

Step 5: Simulate and Check the Program

Load the program into a suitable simulator or the machine’s graphics mode. Check the complete toolpath from start to finish. Look for unexpected movements, wrong coordinates, missing retracts, and collisions.

Compare the simulated path with the drawing. Confirm that the program uses the intended units, work offset, tool, and coordinate mode. A good simulation is useful, but it does not replace a real setup check.

  • Check the path

    Make sure every movement follows the intended route.

  • Check clearances

    Inspect the tool, stock, clamps, and fixture positions.

  • Check the settings

    Verify units, offsets, feed, speed, and machine modes.

Step 6: Run the Approved First Test

Only run the program after the machine, setup, and program have been checked by a trained operator. Use the machine’s approved test procedure. This may include a dry run, single-block operation, reduced rapid override, or another controlled method.

Keep the machine’s emergency stop accessible. Stay outside the enclosure during automatic operation unless the machine’s safety procedure permits otherwise. Never reach into a moving machine to correct a problem.

  • Observe

    Watch the first movements from a safe operator position.

  • Stop if needed

    Use the approved stop procedure if the machine behaves unexpectedly.

  • Inspect the result

    Check the part and compare it with the drawing.

Common CNC G-Code Mistakes and How to Avoid Them

Many beginner programming problems come from small mistakes in machine modes, positions, and command order. A single wrong unit setting can change the scale of an entire toolpath. A forgotten modal command can also affect several later movements.

The best approach is to check the program in a fixed order. Start with the machine and controller settings. Then check coordinates, offsets, tool data, and movement commands. Finally, review the complete path in simulation.

When a machine reports an alarm, do not remove commands at random. Read the alarm message and compare the line with the controller manual. Find the cause before changing the program.

  • Wrong units

    Using inch values in a metric program, or the reverse, can produce a badly scaled toolpath. Set the correct unit mode at the start.

  • Wrong positioning mode

    Confusing G90 with G91 can send the tool to an unexpected location. Make the intended mode clear.

  • Incorrect work offset

    A wrong G54 or other offset can shift the complete program. Check the stored values against the actual setup.

  • Incorrect tool offset

    A wrong tool length or radius value can cause poor cuts or collisions. Verify the selected offset.

  • Unsafe rapid movement

    G00 can move quickly through the machine’s working area. Check all clearances before running it.

  • Wrong feed rate

    An unsuitable feed can overload or damage the cutter. Use appropriate cutting data.

  • Missing program end

    An incomplete ending can leave machine functions active. Use the correct shutdown and ending commands.

What to Do When a G-Code Program Does Not Run

If the controller rejects a program, first read the alarm or error message. Note the line number and command that caused the issue. Check for missing values, unsupported commands, incorrect arc data, and invalid combinations of modal codes.

If the program runs but the machine moves incorrectly, stop the operation using the approved procedure. Then inspect the coordinate system, work offset, tool offset, units, and positioning mode. Do not continue cutting until the cause is clear.

If the error involves a machine-specific command, consult the machine builder or qualified service staff. Some errors involve hardware, tool changers, probes, or other functions that cannot be fixed by editing the G-code alone.

  • Read the alarm

    Use the exact error message to identify the affected command.

  • Inspect the line

    Check its letters, numbers, and required parameters.

  • Check active modes

    Confirm the current units, coordinate mode, and motion mode.

  • Use the manual

    Confirm the command is supported by the exact controller.

  • Test again safely

    Use simulation or the approved machine test procedure.

CNC G-Code Safety Rules for Beginners

CNC machines can cause serious injury and equipment damage. A correct-looking program is not enough to make a machining job safe. The machine, workholding, cutting tool, offsets, guards, and operator procedure must all be correct.

Before running a new program, confirm that the stock is secure and the tool is installed correctly. Check that the work offset matches the setup. Make sure the toolpath does not cross clamps, fixtures, or other obstacles.

Use simulation or graphics mode to inspect unfamiliar code. Follow the machine maker’s instructions for dry runs and single-block testing. Never bypass an interlock or safety device to make a program run.

Keep hands and tools away from moving parts. Do not open a machine enclosure while the spindle or axes are moving unless the machine’s safety system and approved procedure specifically allow it. Use lockout and tagout procedures when servicing equipment, as required by the workplace.

  • Check workholding

    Secure the stock and confirm that the fixture can handle the planned cutting forces.

  • Inspect the tool

    Check the tool condition, holder, installation, and length data.

  • Verify the offsets

    Confirm the correct work and tool offset values.

  • Review the toolpath

    Use simulation and an approved test method before cutting.

  • Use machine guards

    Keep protective doors, interlocks, and safety devices in their required condition.

  • Know the stop controls

    Understand the approved feed hold, program stop, and emergency stop procedures.

Why Simulation Is Not a Complete Safety Check

A simulator can show the programmed toolpath and help identify many programming errors. It may also show collisions if the correct machine, stock, fixture, and tool models are loaded.

However, a simulation may not match the real machine setup. An incorrect fixture model, missing clamp, wrong tool length, or inaccurate offset can hide a problem. The actual machine may also have limits that the simulation does not fully represent.

Use simulation as one part of the safety process. Combine it with setup inspection, machine documentation, and the approved first-run procedure.

  • Simulation

    Checks the programmed movement against the loaded models.

  • Setup inspection

    Checks the real stock, tools, clamps, and offsets.

  • Controlled test

    Checks machine behavior under the approved operating procedure.

G-Code for CNC Mills, Lathes, and Routers

The basic idea of G-code is similar across many CNC machines, but the programming details can change. A milling machine, lathe, and router do not perform the same type of work. Their axes, tool systems, spindle functions, and cutting methods differ.

A CNC mill often uses X, Y, and Z to position a rotating cutting tool around a fixed workpiece. A CNC router uses a similar three-axis arrangement for wood, plastic, foam, and some other materials. A CNC lathe commonly rotates the workpiece while the cutting tool moves along the part.

Some controllers also support extra axes, such as A, B, and C. These may control rotation around the main linear axes. The available commands and axis meanings depend on the machine design.

  • CNC milling

    Uses programmed tool movement to remove material from a secured workpiece.

  • CNC turning

    Usually rotates the workpiece while a cutting tool follows a programmed path.

  • CNC routing

    Uses a rotating cutter to shape sheet goods, wood, plastics, and other suitable materials.

G-Code in CNC Milling

CNC milling often uses G00, G01, G02, and G03 for movement. It may also use G17 to select the XY plane, G54 for a work coordinate system, and G43 for tool length compensation.

Milling programs can include drilling cycles, cutter compensation, tool changes, and multiple work offsets. The operator must check the machine’s supported commands and setup rules before using these features.

  • Common task

    Making pockets, slots, holes, profiles, and flat surfaces.

  • Main concern

    Correct tool length, work zero, cutting depth, and tool clearance.

G-Code in CNC Turning

CNC lathes use a different machining layout. X and Z are common linear axes, with X often controlling movement across the workpiece diameter and Z controlling movement along its length. The exact axis convention depends on the machine.

Turning programs may use constant surface speed, fixed RPM, threading cycles, and roughing or finishing cycles. Commands such as G96 and G97 are common on many turning controllers, but their exact operation must be checked in the lathe manual.

  • Common task

    Turning shafts, facing ends, boring holes, and cutting threads.

  • Main concern

    Correct work coordinates, spindle direction, tool position, and workpiece security.

G-Code in CNC Routing

CNC routers often use G-code for cutting signs, cabinets, panels, and custom shapes. Many router programs are created in CAM software. The software turns the design into a toolpath and then produces code for the selected controller.

Router programs may use common movement commands such as G00 and G01. They may also use machine-specific spindle, dust collection, and tool change functions. The operator must verify that the generated code matches the router’s control system.

  • Common task

    Cutting profiles, pockets, lettering, and decorative designs.

  • Main concern

    Workholding, cutter direction, dust control, tool depth, and clearance.

G-Code Programming Versus CAM Software

You can create CNC programs by writing G-code manually or by using computer-aided manufacturing software. Both methods produce instructions for the CNC controller, but they serve different needs.

Manual programming gives you direct control over the code. It is useful for simple movements, small edits, and learning how a controller works. However, complex toolpaths can take a long time to write and check by hand.

CAM software creates toolpaths from a digital design. The user selects the tool, material, machining strategy, and cutting settings. The software then creates a program through a machine-specific postprocessor.

A postprocessor converts CAM toolpaths into code suited to a selected machine and controller. Choosing the wrong postprocessor can produce incorrect or unsupported commands. Always verify the output before running it.

  • Manual G-code

    Useful for simple programs, direct edits, and understanding controller behavior.

  • CAM programming

    Useful for complex shapes, repeated toolpaths, and multi-step machining work.

  • Postprocessor

    Converts CAM output into the required machine-specific code format.

When Manual Programming Makes Sense

Manual programming can work well for simple positioning tasks and short machining routines. It also helps beginners understand what the controller reads. A person who knows the basic commands can often identify a small code error more easily.

However, manual programming is not always the best choice for complex parts. Many curves, pockets, and multi-axis movements need careful calculations. CAM software can help create and check these paths, but the output still needs review.

  • Short programs

    Simple movements and basic machine operations.

  • Learning

    Understanding commands and machine modes.

  • Small edits

    Making controlled changes to verified programs.

When CAM Software Makes Sense

CAM software is useful when a part has complex geometry or several machining operations. It can create toolpaths from a CAD model and help the user choose cutting strategies.

The quality of the result depends on the design, tool data, machine model, machining settings, and postprocessor. CAM output is not automatically safe or correct. Review the generated code and simulate the complete job.

  • Complex geometry

    Useful for curves, pockets, contours, and multi-axis paths.

  • Repeated production

    Helps create consistent programs for similar parts.

  • Toolpath planning

    Helps compare machining strategies and cutting approaches.

A Practical CNC G-Code Learning Plan

You do not need to learn every command at once. Start with the basic movement and position commands. Then learn machine modes, offsets, tool settings, and common program structure. This order helps you build a strong foundation without taking on too much information.

Use the controller’s official manual as your main reference. Keep a small command list for the machine you use. Add notes when you learn how a command works on that controller.

Practice with sample programs in a simulator. Change one command at a time and observe the result. This makes it easier to understand how the controller reads each instruction.

  • Stage 1: Learn movement

    Study G00, G01, G02, and G03.

  • Stage 2: Learn positioning

    Practice G90, G91, and coordinate systems.

  • Stage 3: Learn machine settings

    Study units, feed modes, spindle settings, and offsets.

  • Stage 4: Learn M-codes

    Understand spindle, coolant, tool change, stop, and end commands.

  • Stage 5: Read complete programs

    Follow the program from startup to the final movement.

  • Stage 6: Simulate

    Practice finding errors before using a real machine.

How to Know You Understand the Basics

You are ready to move beyond the first lessons when you can read a short program and explain what each line does. You should be able to identify movement commands, active modes, coordinates, feed rates, and spindle commands.

You should also be able to explain the difference between machine zero and work zero. You must know how an incorrect unit or offset can change the toolpath. Most importantly, you should know when a program needs a machine-specific check before use.

  • Read a short program

    Explain the purpose of its main commands.

  • Follow a toolpath

    Work out the programmed destination of each movement.

  • Identify active modes

    Recognize when units and positioning modes change.

  • Find basic errors

    Spot missing values, wrong units, and incorrect coordinates.

  • Check machine limits

    Know when the controller manual or a trained operator is needed.

Important Research Notes and Further Learning Needs

CNC programming references do not always use identical command lists or definitions. This is because controller families can support different commands and may assign special functions to certain codes. A general reference is useful for learning, but the exact machine manual remains the final source for supported functions.

The core explanations in this guide follow common RS274-style programming concepts and documented examples from LinuxCNC and Haas milling documentation. The command examples are educational. They are not the result of a physical machine test or a claim that every controller behaves in the same way.

The most useful next learning step is to study the programming manual for the controller installed on your machine. Focus on its startup requirements, modal groups, work offsets, tool compensation, motion commands, and program-ending rules.

  • Next question: Does every CNC machine use the same G-code?

    Reader stage: Before programming. Basis: Inferred next need from controller differences. Coverage: Common introductory guides may give standard command lists but not always explain machine-specific limits. Benefit: Prevents unsafe assumptions. Priority: High. Placement: This section and the controller compatibility discussion.

  • Next question: How do I check whether a G-code program is safe?

    Reader stage: Before the first machine run. Basis: Inferred next need from the risks of rapid movement and incorrect offsets. Coverage: Basic command lists often provide limited setup checks. Benefit: Connects code knowledge with real machine safety. Priority: High. Placement: Safety and simulation sections.

  • Next question: What is the difference between G54 and G43?

    Reader stage: During setup. Basis: Inferred next need from work and tool coordinate concepts. Coverage: Both functions are documented in machine manuals, but beginners may confuse their roles. Benefit: Helps prevent incorrect position calculations. Priority: High. Placement: Coordinate systems section.

  • Next question: Should I write G-code manually or use CAM?

    Reader stage: After learning basic commands. Basis: Inferred next need from the choice of programming method. Coverage: The methods have different strengths and limitations. Benefit: Helps readers select a suitable workflow. Priority: Medium. Placement: CAM comparison section.

  • Next question: How do I find the right feed and spindle speed?

    Reader stage: Before cutting. Basis: Inferred next need from the use of F and S commands. Coverage: General command explanations cannot provide universal cutting values. Benefit: Encourages tool-specific and material-specific settings. Priority: High. Placement: Feed rate and spindle speed section.

Frequently Asked Questions

What is the easiest G-code to learn first?

G00 and G01 are good starting points. G00 controls rapid positioning, while G01 controls straight movement at the programmed feed rate. After these, learn G90, G91, G20, and G21 to understand positioning and units.

Practice each command in a simulator before using it on a real machine.

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

G-code usually controls tool movement and machine modes. M-code usually controls other machine functions, such as spindle operation, coolant, tool changes, and program stops.

Both types can appear in the same CNC program. Their exact functions depend on the controller and machine.

Can I write CNC G-code without CAM software?

Yes. You can write a G-code program manually with a suitable text editor or programming system. This works well for simple movements and short programs.

For complex parts, CAM software can help create toolpaths and machine-specific output. You must still check the postprocessor and verify the final program.

What does G90 mean in CNC programming?

G90 selects absolute positioning on common CNC controllers. The programmed coordinates refer to the active coordinate system rather than the current position.

G91 selects incremental positioning. It tells the machine how far to move from its current position.

Why does my CNC machine show a G-code alarm?

A G-code alarm may appear when the controller finds an unsupported command, missing value, invalid arc, incorrect command combination, or other programming problem. Some alarms can also relate to machine setup or hardware.

Read the full alarm message and check the affected program line. Compare it with the controller manual. Do not remove commands at random or run the program until the cause has been corrected.

What is the difference between G54 and G43?

G54 selects a stored work coordinate system on common milling controllers. It helps define the part’s work zero.

G43 activates tool length compensation on supported controllers. It uses a stored tool measurement, often selected with an H number. These commands perform different jobs and should not be treated as alternatives.

Do CNC routers and CNC mills use the same G-code?

Many CNC routers and mills use similar basic movement commands, such as G00 and G01. However, their controllers may use different settings, machine functions, and postprocessors.

Always use code designed for the exact machine and controller. Check the machine manual before transferring a program between machines.

✦ Wrapping Up

Conclusion

CNC G-code is the language that turns a machining plan into machine movements. It uses simple letter and number commands to control positions, cutting paths, feed rates, spindle speeds, and other functions. Once you understand the main commands, reading a basic CNC program becomes much easier.

Start with G00, G01, G02, and G03. Then learn G90, G91, G20, G21, and common M-codes. After that, study work offsets, tool length compensation, and basic program structure. Practice with short examples and use simulation to check each movement.

Remember that standard G-code knowledge is only the starting point. Every CNC machine has its own limits, setup needs, and controller rules. Check the official manual, verify the work and tool offsets, and follow the approved safety process before running a new program.

💡 Key Takeaway

Start by writing a short program with G00, G01, G90, and the correct unit command. Simulate the toolpath, check every coordinate, and compare the commands with your machine’s official programming manual before attempting a real machining operation.

John Anderson

Contributor

Leave a Comment

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

Scroll to Top