Basic Knowledge of CNC Programming
This page explains how a CNC program is built and read: work coordinates, G-code and M-code structure, tool offsets, feeds and speeds, and the checks that keep a first article in tolerance. It is written for design engineers, manufacturing engineers and buyers who review programs or quote machined parts. After reading it you can tell what belongs in a program, what a CAM post must output, and when a part needs a different programming approach.

What a CNC program actually contains
A program is a list of positions, tool changes and machine states. Everything else is support around that list.
Program structure: coordinates, words and blocks
A CNC program is read one block at a time. Each block is a line of words, and each word is a letter plus a number. G addresses the machine's motion and state, M handles on/off events like spindle and coolant, and X, Y, Z carry position data.
Most controllers read the same core address set. Fanuc and Haas dominate job shops, while Siemens 840D and Heidenhain TNC appear on larger or high-end machines. The logic is portable, but the exact syntax is not. A post processor converts CAM toolpaths into the dialect your controller accepts.
The top of a program usually carries a safety block. A typical line cancels cutter compensation, selects the work offset, sets absolute mode, and starts the spindle. Skipping that block is a common cause of a crash on the first rapid move.
Sequence numbers (N words) are optional on most controllers. They help when you need to restart mid-program after a broken tool or when a machine alarm points to a specific block number. Keep them if your shop uses restart-by-block.
- 1BlockOne line of the program, ended by a line feed.
- 2WordA letter plus a value, such as G01 or X25.4.
- 3ModalA word that stays active until another word of the same type cancels it.
- 4Safety blockThe first lines that set modes before any motion.
Work coordinates, offsets and the WCS
Every program is written in a coordinate system. The machine has its own home position, and the part has its own zero point. The gap between them is stored in a work offset, usually G54 through G59. That offset is what tells the control where the part sits on the table.
Machine zero is fixed by the builder. Part zero is a choice. Put it on a corner, a datum hole or the center of a bore, whichever gives the cleanest dimension chain on the drawing. If your drawing dimensions from a bore center, set zero there and save the operator a calculation.
For a 5-axis program, the control must also know where the rotary axes sit in space. This is a kinematic model, sometimes called RTCP or TCPM depending on the controller. Get the model wrong and the tool tip drifts even though the program looks correct on screen.
A rough check before cutting: dry-run the program with the tool raised, or use the control's simulation. Watch the Z clearance at each tool change. Most first-run crashes happen at a tool change or a rapid move, not in the cut itself.
G-codes, M-codes and what they control
G-codes fall into groups. G00 positions at rapid, G01 moves at a programmed feed, G02 and G03 cut arcs. Only one motion code is active at a time, which is why a modal G01 keeps cutting straight until a G02 or G00 replaces it.
Compensation codes matter as much as motion. G41 and G42 offset the tool path left or right of the programmed line, so the CAM system can program the part edge and let the control adjust for tool radius. G43 applies tool length, usually from the tool offset table.
M-codes handle discrete events. M03 starts the spindle forward, M08 turns coolant on, M06 changes the tool, M30 ends the program. Exact M-code numbers vary between builders, so check the machine manual before you reuse a program on a different machine.
Canned cycles compress repetitive work. G81 drills a hole, G83 pecks to clear chips, G84 taps. A single G83 line replaces dozens of blocks of feed and retract moves, which makes the program shorter and easier to read.
Common G-codes and M-codes at a glance
Syntax follows the ISO/Fanuc convention. Always confirm against the controller manual.
| Code | Function | Notes |
|---|---|---|
| G00 | Rapid positioning | No cutting. Watch clearance. |
| G01 | Linear feed move | Requires a programmed F value. |
| G02 / G03 | Circular arc CW / CCW | Needs I, J, K or R. |
| G41 / G42 | Cutter compensation left / right | Cancel with G40 before tool change. |
| G43 | Tool length compensation | Reads from the H offset register. |
| G54–G59 | Work coordinate systems | Set by the operator at setup. |
| G81 / G83 | Drill / peck drill cycle | G83 clears chips in deep holes. |
| G84 | Tapping cycle | Feed equals pitch times spindle speed. |
| M03 / M05 | Spindle on forward / stop | Direction matters for tapping. |
| M06 | Tool change | Often paired with T word. |
| M08 / M09 | Coolant on / off | Flood or mist per setup. |
| M30 | Program end and reset | Returns to the start block. |
Feeds, speeds and how they change with material
Cutting speed is the surface speed at the tool edge, measured in meters or feet per minute. Feed is how fast the tool advances per tooth or per revolution. Both depend on the material, the tool coating and the rigidity of the setup.
Aluminium runs fast. A 6061 part on a 3-axis mill might cut at 300–500 m/min surface speed with a two- or three-flute carbide cutter, flooded with coolant. Stainless 316 wants a lower surface speed and a heavier feed per tooth, so the tool cuts instead of rubbing.
Titanium and Inconel sit at the other end. They conduct heat poorly, so the heat stays in the tool edge. Use a lower surface speed, a generous feed, and a rigid setup. If the tool squeals, the feed is too light, not too heavy.
Roughing and finishing rarely share a recipe. Rough passes take a deep axial cut with a moderate radial step. The finishing pass removes a small stock allowance, often 0.2–0.5 mm, at a higher surface speed to hit the required finish.
Finish targets drive the last choices. Ra 1.6–3.2 μm is normal as-machined. Ra 0.8–1.6 μm needs a sharp tool, a stable setup and a lighter finishing pass. Ra 0.2–0.8 μm usually means a dedicated finishing strategy or a secondary operation.
When a part does not suit a standard program
Some features cannot be cut from one setup. A deep pocket with a thin wall may need a roughing pass, a stress-relief step, and a finishing pass in a later session. Programming it as one continuous toolpath invites distortion.
Thin floor sections behave the same way. A floor under 1 mm thick will deflect under the tool pressure, no matter how good the program looks. Add support, reduce the radial engagement, or move the feature to a later operation.
A 5-axis program only helps when the geometry needs it. If a part is reachable in three axes, a 3-axis program is faster to write, faster to verify, and easier to inspect. Reach for rotary axes when the feature demands them, not by default.
Small batches and one-off prototypes often favor a conversational or shop-floor program over a full CAM build. When the drawing is simple and the quantity is one, a short hand-written program can beat the setup time of a CAM session.
Frequently asked questions
Do I need to know G-code if I use CAM software?
You can generate a program without writing a single block, but you cannot debug one. When a tool breaks or a dimension drifts, reading the code tells you whether the issue is in the post, the offset or the setup.
Engineers who can read G-code catch mistakes before the first cut. That usually saves more time than it costs to learn.
What is the difference between G54 and G55?
Both are work coordinate systems. G54 is usually the first setup and G55 the second, or the first position on a fixture with multiple parts.
The offset values are stored separately. Switching between them moves part zero without editing the program.
How do I choose a feed rate for a new material?
Start from the tool supplier's recommended surface speed for that material and coating. Convert it to spindle rpm using the tool diameter.
Then set feed per tooth so the chip is thick enough to carry heat away. Test on a scrap block before you commit the program to a production part.
When should I use a canned cycle instead of writing each pass?
Use one whenever the operation repeats: drilling, pecking, tapping, boring. A canned cycle reduces block count and makes the program easier to read and edit.
Skip it when the hole pattern is irregular or the depth changes per hole. In those cases explicit blocks are clearer.
What tolerance can a standard CNC program hold?
At GreatLight we work to ±0.005 mm (±0.0002 in) on qualifying features. The achievable value depends on the feature, the material and the setup, not on the program alone.
Tell us the critical dimensions at the quote stage so the process plan matches them.
Can you review my program or drawing before machining?
Yes. Upload the CAD file or the drawing and we return a DFM analysis with the quotation, usually within 12 hours.
We flag features that need a different setup, a tighter tolerance callout or a change to the finish spec.
Send us your part and we will review the program plan
Upload a drawing or STEP file. We return a quotation and a free DFM analysis within 12 hours, then start production within 24 hours.
12-hour quoteFree DFM analysis100% inspectionNDA on request