Basic G&M Code for CNC Milling
This page covers the G and M codes that do most of the work in a 3-axis milling program, and the offset and modal rules that decide whether a part comes out at size. It is written for design engineers and buyers who read programs, review setups or quote machined parts. After it, you can tell whether a drawing is machinable as programmed, and where a code choice costs time or tolerance.

What a milling program actually controls
A CNC program is a list of positions, feeds and switch commands. The geometry lives in your CAD model; the code is the machine's route to that geometry.
The G codes that define where the tool goes
Most of a milling program is motion. The controller reads a block, moves one or more axes, then reads the next block. Two words repeat on nearly every line: G and M. G words set the mode and the path. M words switch machine functions on and off, like the spindle or coolant. The address letters X, Y, Z, I, J, K and R carry the numbers that place the tool in space.
Machining aluminum at 6061 and steel at 4140 use the same motion vocabulary. What changes is speed, feed and cutter choice, not the letters. That is why the basic G&M code CNC operators learn on a 3-axis mill still applies on a 5-axis center. The rotary axes add words, not a new language.
Read a program from the top and you see the order of operations: safe start, tool change, touch off, rough, finish, retract, end. Each of those steps maps to a small set of codes. Once you can name them, you can estimate cycle time and spot a setup that will not hold ±0.005 mm.
- 1G0 rapidNon-cutting move at maximum feed. Use only in clear air, never into stock.
- 2G1 linearStraight cut at the programmed feed rate. The workhorse of roughing and finishing.
- 3G2 / G3 arcClockwise and counterclockwise arcs, defined by I, J, K or R.
- 4G17 / G18 / G19Selects the arc plane: XY, XZ or YZ. G17 is the 3-axis default.
What the M codes switch on and off
M codes are the switches. They do not move the tool, they change the state of the machine. The spindle is the clearest example: M3 runs it clockwise, M4 counterclockwise, M5 stops it. A stop command that ignores the spindle will scrap a part fast, so the order of M words in a block matters.
Coolant is the other common group. M8 floods, M7 mists, M9 shuts it off. On deep pockets in 7075 or 17-4PH, coolant choice and direction affect both tool life and chip evacuation. A program that cuts dry in a deep slot will rub, not cut.
Two M codes end a program in different ways. M30 stops the spindle, cancels offsets and rewinds. M0 is a planned stop, used for flipping a part or checking a dimension. M0 waits for the operator. M1 is the same, but only if the optional stop switch is on. These are the codes that let a machinist run the same program across a batch without rewriting it.
Common G and M codes at the machine
A short list that covers most 3-axis milling work. Your controller may add or rename codes; check the manual before editing a post.
| Code | Function | When to use it |
|---|---|---|
| G0 | Rapid positioning | Clear-air moves between cuts |
| G1 | Linear interpolation | Straight cuts at programmed feed |
| G2 / G3 | Arc clockwise / counterclockwise | Radii, bosses and fillets |
| G17 / G18 / G19 | Arc plane selection | XY default; XZ or YZ for side work |
| G20 / G21 | Inch / metric units | Set once at the top of the program |
| G28 | Return to machine home | Before tool change or at the end |
| G40 / G41 / G42 | Cutter compensation off / left / right | Profile control without reposting |
| G43 / G49 | Tool length offset on / cancel | After every tool change |
| G54–G59 | Work coordinate systems | One per vise, fixture or part |
| G73 / G83 | High-speed / deep-hole peck drilling | Holes deeper than about 3× Ø |
| G81 / G82 | Standard / dwell drilling | Through holes and spot faces |
| G90 / G91 | Absolute / incremental positioning | Absolute for most work; incremental for patterns |
| M3 / M4 / M5 | Spindle clockwise / counter / stop | Around every cutting move |
| M7 / M8 / M9 | Mist / flood / coolant off | Deep pockets and hard materials |
| M0 / M1 | Program stop / optional stop | Part flips and in-process checks |
| M30 | End program and rewind | Last line of the program |
Work offsets, tool length and why the first part is often scrap
The code tells the tool where to go, but it needs a zero to measure from. That zero is the work offset. G54 is the most used of the six standard offsets, and a shop running three vises on one table can assign G54, G55 and G56 to them. The machine holds the distance from machine home to each part origin, so one program can run three setups without a rewrite.
Tool length offset is the other half. Every cutter sits at a different length in the holder. G43 reads the H value and shifts the Z axis by the measured tool length. Forget G43 and the first rapid move drives the tool into the stock. This is the most common crash in a manual setup, and it is why a single-block dry run is standard practice.
Cutter compensation, G41 and G42, shifts the path left or right of the programmed line by the radius in the offset table. That lets a machinist adjust a bore or boss by a few microns at the control instead of reposting the file. On a part held to ±0.005 mm, that adjustment is often the difference between a pass and a rework.
When the code is not the bottleneck
A clean program will not rescue a bad setup. If the vise lifts the part, or the stock moves under a heavy roughing pass, no G code fixes it. Rigidity, workholding and tool stick-out set the ceiling. The program only decides how close to that ceiling you get.
Fixturing is usually the first limit on thin walls and long parts. A 4,000 mm rail with a 150 mm section will chatter no matter how smooth the toolpath looks. On parts like that, we slow the feed, add a support, or split the operation. The code changes as a result, not the other way around.
Tool choice also caps the result. A 3-flute carbide end mill in 6061 can run fast and leave Ra 0.8–1.6 μm. The same cutter in 316L stainless needs lower surface speed and a different chip load. If the finish callout is Ra 0.2–0.8 μm, plan on a separate finishing pass with a new cutter and a lighter radial step.
For a one-off prototype, hand-checking the program is fine. For a 10,000-part run, the program is frozen, tool life is tracked, and any edit goes through a change record. That is where the basic G&M code CNC language stops being a lesson and becomes a production document.
Common questions from engineers
Do I need to write G code to get a part machined?
No. Send a STEP or IGES file with a drawing that carries tolerances and finish callouts. Our CAM team posts the program and runs a simulation before the first cut.
If you already have a proven program, send it with the setup sheet. We can run it or adapt it to our machines.
Which codes change between a 3-axis and a 5-axis program?
The motion codes stay the same. Five-axis work adds rotary axis addresses, usually A, B or C, and often G68.2 for tilted work planes or TCPC to keep the tool tip on path.
The setup logic also changes. Five-axis parts are usually held in a single setup, so work offset assignment and collision checking get more attention.
How do you hold ±0.005 mm across a batch?
Tolerance is held by the machine, the tool and the temperature, not by the code alone. We use in-process probing on critical features and check the first article against the drawing before the run continues.
Every part gets a final inspection before shipment. Reports are available on request.
Can the program be adjusted after the first part?
Yes. Cutter compensation and work offsets let the operator shift a dimension at the control. Small tweaks do not need a new CAM post.
For a production run, any change is recorded so the revision stays traceable.
What materials do these codes cover?
The codes are material-independent. Feeds and speeds are not. Aluminum 6061, 7075, stainless 304 and 17-4PH, titanium TC4 and Inconel each need their own cutting data and often their own cutter geometry.
Tell us the material and we set the parameters from the shop's cutting data.
How do you keep drawings and programs confidential?
Uploads are secure and confidential. We sign an NDA on request, and we hold ISO 27001:2022 for information security.
Programs and models are stored per project and not shared outside the build team.
Send a drawing, get a program and a price
Upload a STEP file and we come back with a quotation and a free DFM analysis within 12 hours. We machine from one prototype to 10,000+ part runs, with 100% inspection before shipment.
12-hour quote100% inspectionNDA on requestNo minimum order