GreatLight CNC Machining Factory logo
CNC Machining
Rapid Prototyping
Materials
Industries
News
About GL

Get Instant Quote

CNC programming basics

What Is G Code and M Code in CNC Machine Control?

G code tells the machine where to move. M code tells it what to switch on or off. Together they form the program that turns a CAD model into a metal part. This page explains both sets of commands, the limits of each, and how to judge a shop by the code it runs.

G code = motionM code = machine functionsISO 6983 basis±0.005 mm tolerance
what is g code and m code in cnc machine
Quick summary

Key takeaways

Two families, one programG codes command axis motion and coordinates; M codes command spindle, coolant, tool change and program flow.
G code is modalMost G codes stay active until replaced, so a missing G01 can leave the next line cutting in rapid.
M code controls the shop floorCoolant, spindle direction and pallet changes are M functions, not motion.
CAM output is a draftRaw CAM code is safe but rarely efficient; programmers edit feed, retract and order before release.
Dialect mattersFanuc, Siemens and Haas share the core but differ on canned cycles and macro syntax.
Definition

What g code and m code actually do

A CNC program is a list of lines. Each line carries a letter address and a number. The letter says what kind of instruction it is; the number says how much. G addresses motion and geometry. M addresses machine state. F sets feed rate, S sets spindle speed, T selects a tool position, and X, Y, Z sit on the axes themselves.

The split exists because two different things must be coordinated. The machine has to move to a point in space, and it has to be in the right condition when it gets there. G code handles the first job. M code handles the second. A roughing pass into 6061 aluminium might read G01 X50.0 Y20.0 F1200 M08, meaning feed to that point with coolant on.

Blocks are read top to bottom. The control executes each line, then moves on. Some lines change state and stay changed. That is what modal means. G01 stays in effect until a G00 or G02 replaces it. Feed rate stays until a new F appears. Programmers rely on this to keep code short, which is also how a single mistake propagates through hundreds of lines.

The language is standardized under ISO 6983, but only at the core. Every machine builder adds its own canned cycles, macro variables and probing routines. That is why the same part posted for a Fanuc control and a Siemens control does not look identical, even when both cut to ±0.005 mm.

  • 1
    G = geometryPosition, interpolation, compensation, coordinate systems, canned cycles.
  • 2
    M = machineSpindle, coolant, tool change, pallet, program stop, program end.
  • 3
    F, S, TFeed rate, spindle speed, tool selection. Not G or M, but always present.
G code

The G codes that shape a cut

G00 moves at rapid traverse. It is not a cutting move and should never touch material. G01 feeds in a straight line at the programmed F value. G02 and G03 cut arcs clockwise and counterclockwise, defined by an endpoint plus either a radius R or centre offsets I and J. The choice between R and I/J matters on long arcs, where a single R value can produce a bowed profile.

Coordinate systems come next. G54 through G59 define work offsets, letting one program run on several vises or fixtures. G90 sets absolute positioning and G91 sets incremental. Mixing them mid-program is legal and useful, but only if the programmer keeps track. G91 in a drilling loop is common; G91 in a profile pass usually means a scrapped part.

Compensation is where the real skill sits. G41 and G42 offset the path left or right of the programmed contour to account for cutter diameter. G40 cancels it. Used correctly, a nominal toolpath can be adjusted for actual tool wear without reposting. Used carelessly, the control will alarm on an inside corner smaller than the radius, or worse, gouge the wall.

Canned cycles compress repetitive work. G81 drills, G83 pecks with a retract, G84 taps, G76 bores. Each takes a set of parameters and repeats them at every listed position. On a part with 60 holes, that turns 60 blocks of code into three. It also hides the detail, so a wrong R plane or clearance value can crash a drill into a fixture before anyone reads the line.

  • 1
    G00 / G01Rapid positioning versus controlled feed. Never cut with G00.
  • 2
    G02 / G03Arc interpolation. Prefer I/J over R on arcs wider than a quarter circle.
  • 3
    G41 / G42 / G40Cutter compensation left, right, and cancel.
  • 4
    G54–G59Work offsets for multi-fixture setups.
M code

The M codes that run the machine

M03 starts the spindle clockwise and M04 counterclockwise. M05 stops it. On a mill, M03 with a right-hand cutter is the normal direction; on a lathe, the same code can mean the opposite relative to the operator, which is why setup sheets matter. Spindle speed is not an M function. It is an S value, and it takes effect when the spindle starts.

Coolant is M07 for mist and M08 for flood. M09 turns it off. Leaving flood coolant on through a dry finishing pass is a common CAM default, and on aluminium it can wash chips back into the cut. Programmers usually insert M09 before a finishing contour and let air blast or through-spindle coolant take over.

Tool changes run through M06 with a T address. The control retracts, rotates the magazine, and confirms the position before the next move. On machines with a pre-staging option, the next tool is already waiting, which saves several seconds per change. On a job with 20 tools, that adds up to minutes per part.

Program flow uses M00 for an unconditional stop, M01 for an optional stop the operator can enable, M30 to end and rewind, and M02 to end without rewind. Subprogram calls use M98 and returns use M99. These four commands decide whether a lights-out run continues or sits waiting for a person, so they are worth auditing before any unattended shift.

  • 1
    M03 / M04 / M05Spindle on clockwise, on counterclockwise, stop.
  • 2
    M07 / M08 / M09Mist on, flood on, coolant off.
  • 3
    M06Tool change, paired with a T address.
  • 4
    M00 / M01 / M30Forced stop, optional stop, end and rewind.
Boundaries

Where the code stops and the process begins

A correct program is not a good process. The code can position a cutter to ±0.005 mm and still produce a part that fails inspection, because deflection, thermal growth and workholding sit outside the instruction set. A 4,000 mm aluminium extrusion will bow under its own weight no matter how clean the toolpath is. The programmer can only manage what the machine controls.

Tool engagement is the clearest example. The same G01 block cuts differently at 5 percent radial engagement and at 50 percent. CAM software estimates chip thinning and adjusts feed, but the estimate depends on the actual tool geometry and the material condition. A 7075 block that has been sitting in a cold shop behaves differently from one that just came off a saw.

Surface finish is set by parameters the code only partly names. Feed per tooth, stepover, tool runout and coolant delivery all land in the same Ra number. Getting Ra 0.8–1.6 μm on a wall is a system result, not a G code result. When a shop quotes a finish, it is quoting the whole setup, not one line of the program.

This is why code review is a real step in production. A programmer who reads the posted output before it reaches the machine catches a wrong retract plane, a missing M09, or a feed rate left at the CAM default. It takes minutes and prevents the failure modes that cost hours.

  • 1
    Code sets position, not stiffnessDeflection and vibration live in the setup.
  • 2
    Engagement changes everythingFeed must match radial and axial depth.
  • 3
    Finish is a system numberTool, holder, coolant and parameters together.
Workflow

How a CAM post becomes a released program

The order we use before a program is allowed to cut metal.

  • 1
    Post and inspectGenerate code from CAM, then read the first 40 and last 40 lines. Check G54, units and safe Z.
  • 2
    Verify offsetsConfirm work offset and tool length values against the setup sheet. A 0.1 mm error here shows up everywhere.
  • 3
    Dry run above the partRun with Z shifted +50 mm or use the control's simulation. Watch for rapid moves that pass through stock.
  • 4
    Cut a first articleMachine one part, measure critical features, and adjust cutter compensation rather than reposting.
  • 5
    Tune feed and coolantRaise feed until chip load is stable. Move M09 to a point that clears chips before finishing.
  • 6
    Release with revision controlStore the proven program against the part number and setup. Reuse it instead of reposting.
Side by side

G code versus M code at a glance

Both families appear in the same program block and are read left to right.

ItemG codeM code
What it controlsAxis motion and geometryMachine state and accessories
Typical addressesG00, G01, G02, G41, G54M03, M06, M08, M30
Units affectedMillimetres or inches, degreesNone, it is a switch
Modal behaviourMostly modal, stays activeMostly non-modal, acts once
Set by CAMAlways, for every cutPartly, often edited by hand
Common failureWrong offset or arc radiusCoolant or spindle left on
Read byServo and interpolatorPLC and machine logic

The judgment call

If your part is a one-off prototype, let CAM write the code and spend your time on fixturing and first-article measurement. If it is a repeating production part, invest in reading and editing the program by hand: feed, retract and coolant edits usually cut cycle time more than a machine upgrade would.

FAQs

Questions engineers ask about G and M code

Can the same program run on a Fanuc, Haas and Siemens control?

The core set, G00, G01, G02, G03, G54 and the common M codes, is close enough that a simple program will often run.

Canned cycles, macro syntax, probing and high-speed look-ahead differ. A program written for one control is normally reposted rather than patched by hand.

Does more G code mean a more accurate part?

No. Accuracy comes from machine geometry, thermal stability, toolholding and measurement, not from line count.

Very dense code with small segment lengths can actually slow the control down if look-ahead is not tuned, which shows up as chatter on corners.

Why does my CAM output start with so many safety blocks?

Those lines set units, cancel compensation, select the work offset and position the machine at a safe height.

They are cheap insurance. Removing them saves nothing and risks a rapid move into the fixture.

What is the difference between M00 and M01?

M00 always stops the program. The operator must press cycle start to continue.

M01 only stops if the optional stop switch is on. Shops use M01 for mid-run checks and leave the switch off during unattended runs.

Can cutter compensation replace reposting after a tool change?

Yes, within limits. G41 and G42 let the operator adjust for a re-ground or slightly different cutter.

The offset cannot fix a path that is already wrong in shape, and it will alarm on internal corners smaller than the cutter radius.

How do you check a program before it cuts metal?

Simulate or dry run with a Z offset, verify the work offset and tool lengths against the setup sheet, and single-block through the first toolpath.

Most crashes come from a wrong offset or a retract plane below the stock, and both are visible in the first minute of a dry run.

Send us your part, not your code

Upload a STEP file and we return a quotation with free DFM analysis within 12 hours. Our programmers handle the G code and M code, the fixturing and the first article.

12-hour quote100% inspectionNo minimum order quantity

Follow

More from our shop floor

We publish setup notes, tooling trials and inspection data from the factory floor.

FacebookTikTokYouTubeLinkedInInstagramThreadsPinterest

Trusted by engineers and manufacturers worldwide

Tesla Ford Motor Company BYD Auto Denso Magna International Boeing Airbus Medtronic KUKA FANUC