In this guide
A CNC machine does exactly what its program tells it — nothing more, nothing less. That program is written in G-code, a language of short letter-number commands that has run machine tools since the 1950s. You do not need to memorize all 100+ codes to start cutting: about fifteen of them cover nearly everything a student program does.
This guide explains the commands students actually use, how the machine reads them, and a complete worked program for a simple milled part — plus the mistakes that crash tools and scrap workpieces.
What G-code actually is
G-code is a sequence of blocks (lines). Each block is one instruction, executed in order, top to bottom. A typical block looks like this:
G01 X50.0 Y25.0 F200
- G01 — the command: move in a straight line at a controlled feed rate.
- X50.0 Y25.0 — the destination coordinates in millimetres.
- F200 — the feed rate, 200 mm per minute.
Most codes are modal: once you set G01, every following block keeps moving in straight lines until another motion code (G00, G02, G03) replaces it. A few codes are one-shot — they act only on their own line. Knowing which is which is half of reading G-code.
The coordinate system first
Every CNC program runs in a right-hand coordinate system:
- X — left and right along the table.
- Y — front to back.
- Z — up and down; Z0 is usually the top surface of the workpiece, and positive Z moves the tool away from it.
Two zeroes matter and students mix them up:
| Zero | Meaning | Set by |
|---|---|---|
| Machine zero (home) | The machine's fixed reference point, found by homing | Machine, at switch-on |
| Work zero (part zero) | The origin of your part — usually a corner or the centre of the top face | You, with an edge finder, probe, or by touching off |
All coordinates in your program are measured from work zero, not machine zero. Work offsets (G54–G59) store the distance between the two, so the same program can run on different setups by changing the offset instead of the code.
The fifteen commands that matter
| Code | Meaning | Notes |
|---|---|---|
| G00 | Rapid move | Fastest positioning; never cut with G00 — it ignores feed rate |
| G01 | Linear move at feed rate | The workhorse: straight cuts and positioning under control |
| G02 | Clockwise arc | Needs an end point plus an arc centre (I, J) or a radius (R) |
| G03 | Counter-clockwise arc | Same as G02, opposite direction |
| G17 / G18 / G19 | Select XY / XZ / YZ plane | G17 (XY) is the default for milling |
| G20 / G21 | Units: inches / millimetres | G21 for mm; forgetting this is a classic scrap-maker |
| G28 | Return to home | Usually written as G28 Z0 first, then X0 Y0 |
| G40 / G41 / G42 | Cutter compensation off / left / right | G41/G42 offset the tool path by the tool radius |
| G43 | Tool length compensation | Applies the measured tool length; always pair with a tool change |
| G54–G59 | Work coordinate offsets | G54 is the standard first work offset |
| G80 | Cancel canned cycle | Always cancel a cycle before moving on |
| G81 | Drilling canned cycle | Drill to depth, retract — one block per hole |
| G83 | Peck drilling cycle | Drills in small pecks, clearing chips; use for deep holes |
| G90 / G91 | Absolute / incremental positioning | G90: coordinates from work zero; G91: coordinates relative to the last position |
| M03 / M05 | Spindle on (clockwise) / spindle stop | M03 takes an S value for RPM, e.g. M03 S1200 |
| M06 | Tool change | Pairs with a T word, e.g. T1 M06 |
| M08 / M09 | Coolant on / coolant off | |
| M30 | End of program, rewind |
A complete worked program
This example mills a 60 × 40 mm rectangular pocket, 5 mm deep, in an aluminium plate, with a 6 mm end mill. All values are illustrative — check speeds, feeds and depths against your machine and material before running anything.
O1001 (RECTANGULAR POCKET 60 X 40, 5 MM DEEP)
G21 G17 G40 G49 G80 G90 (mm, XY plane, comp off, absolute)
G54 (use work offset 1)
T1 M06 (6 mm end mill)
M03 S3000 (spindle on, 3000 RPM)
G00 X-33.0 Y-23.0 (rapid to a start point clear of the part)
G00 Z5.0 (rapid to 5 mm above the surface)
G43 H1 Z2.0 (apply tool length, approach to 2 mm above)
G01 Z-2.5 F100 (plunge to first depth, 2.5 mm)
G01 X33.0 F300 (cut the first side)
G01 Y23.0 (second side)
G01 X-33.0 (third side)
G01 Y-23.0 (fourth side - pocket outline at 2.5 mm deep)
G01 Z-5.0 F100 (plunge to full depth)
G01 X33.0 F300 (repeat the outline at full depth)
G01 Y23.0
G01 X-33.0
G01 Y-23.0
G00 Z20.0 (retract clear)
M05 (spindle stop)
M09 (coolant off)
G28 Z0 (home the Z axis)
M30 (end of program)
Read it as a story: set up units and offsets, load the tool, start the spindle, move fast to a safe point, approach slowly, cut the outline in two depth passes, retract, stop everything, go home. Every student program follows this shape.
Notice the pocket is cut 3 mm oversize on each side (X ±33 instead of ±30) because the 6 mm cutter removes 3 mm of material on each side of its centre path. That is cutter radius compensation done by hand — G41/G42 does the same job automatically once you understand it.
Canned cycles: drilling without the tedium
Drilling four holes by hand means writing a plunge, a retract and a move for every hole. A canned cycle does it in one block per hole:
G81 X10.0 Y10.0 Z-8.0 R2.0 F120 (drill one hole 8 mm deep)
X50.0 Y10.0 (same cycle, next hole)
X50.0 Y30.0 (next hole)
X10.0 Y30.0 (next hole)
G80 (cancel the cycle)
G81 stays modal, so each X/Y line drills another hole at the same depth. G83 is the same idea but pecks — drilling a few millimetres, retracting to clear chips, then going deeper — which is what you want for holes deeper than about three times the drill diameter. Forgetting G80 after a cycle is a common error: the next positioning move drills a hole where you did not want one.
Absolute vs incremental: the most expensive mix-up
In G90 (absolute), every coordinate is measured from work zero. In G91 (incremental), every coordinate is measured from the current position. A program written in one mode and run in the other goes somewhere entirely different — usually into the workpiece or the vice.
Rules that prevent this:
- Put G90 (or G91) in the safety line at the top of every program, and never assume the machine is in the mode you want.
- Never mix modes inside one program unless you have a specific reason and mark the switch clearly.
- After any interruption (tool change, pause, edit), re-check the mode before restarting.
Five mistakes that scrap parts
- Wrong units (G20 vs G21). A program written in millimetres run in inches mode moves 25.4× too far. Check the safety line before pressing cycle start.
- No tool length offset. Without G43, the machine does not know how long the tool is, and the first Z move goes to the wrong depth — usually into the part.
- Forgetting to cancel cutter compensation (G40). A leftover G41/G42 shifts every later move by the tool radius.
- Arcs with a wrong centre. G02/G03 with an incorrect I/J value produces an arc that does not end where you expect. Simulate the program before running it — most controllers and free simulators show the tool path.
- Rapid moves through the workpiece. G00 ignores feed rate and takes the shortest path. Never rapid below the top of the part; approach in Z first, then move in X/Y, then plunge.
How to learn it properly
- Simulate first, cut second. Run every program in a simulator or the controller's graphics mode and watch the tool path. A wrong path on screen costs nothing.
- Single-block mode. Run the first program one block at a time, watching each move before the next executes.
- Dry run above the part. Set the work offset high or run with the tool raised, and confirm the X/Y path is right before cutting.
- Read real programs. CAM software generates G-code — open its output and match each block to the tool path. It is the fastest way to connect the codes to real motion.
- Keep a cheat sheet at the machine. The fifteen codes in this guide, plus your machine's specific M-codes, cover almost everything you will write by hand.
FAQ
Do I still need G-code if CAM software generates it?
Yes. CAM output is a starting point, not a finished program — you edit feeds, fix tool changes, and debug crashes by reading the code. Operators who cannot read G-code cannot prove a program is safe to run.
Is G-code the same on every machine?
The core G and M codes are standard, but dialects differ: Fanuc, Siemens, Haas and GRBL-based hobby controllers each have quirks, extra codes, and different canned-cycle formats. Always check your controller's manual for the exact format.
What is the difference between G-code and M-code?
G-codes command motion and geometry (moves, arcs, planes, offsets). M-codes command machine functions (spindle, coolant, tool change, program end). A program needs both.
Can I run G-code on a 3D printer?
Yes, in a limited sense: printers use a G-code dialect (Marlin, Klipper) for movement and extrusion, but it is not interchangeable with milling G-code — different codes, different assumptions, no spindle or tool changes.
How do I know what feed rate and spindle speed to use?
From the material, tool diameter and number of flutes, using the standard cutting-speed and chip-load formulas for your tool and material. The values in this guide are illustrative examples, not cutting data — look them up for your actual tool and material.
Limitations
- This guide covers the standard Fanuc-style dialect used on most teaching machines. Your controller may use different formats for canned cycles, tool changes or subprograms — check its manual.
- Hand-written G-code suits simple parts and edits. Complex 3D surfaces are generated by CAM software; writing them by hand is impractical.
- The worked program is a teaching example with illustrative cutting values. Real cutting data depends on material, tool, machine rigidity and coolant — verify before running.
- G-code has no safety interlocks of its own. Machine guards, emergency stops and your own simulation habit are what keep a wrong program from becoming a crash.
Where to go from here
- CNC Milling Basics — the machine, axes and tooling behind the code.
- Lathe Operations: Turning, Facing and Threading — the manual operations G-code automates.
- Gear Types Explained — a part you will eventually program, not just draw.
- Sheet Metal Bending Basics — another process where the drawing, not the code, does the work.
- More manufacturing topics in the Mechanical branch hub.
Stuck on a CNC program for your project? Message Projectech on WhatsApp with your machine controller (e.g. Fanuc, Siemens, GRBL), the part drawing, and your deadline — the reply is a quotation stating scope, price and delivery timeline.