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

Get Instant Quote

CNC programming guide

How to Write Programs for CNC Machine

This guide is for engineers and machinists who need to turn a CAD model into a safe, repeatable cutting program. It covers the full path from drawing review to dry run, with the numbers we actually use on the floor and the errors that cost the most time. By the end you can judge whether a job belongs in G-code written by hand or in CAM output.

±0.005 mm tolerance16 five-axis centersRa 0.2–0.8 μm finish12-hour DFM review
how to write programs for cnc machine
Quick answer

Key takeaways

Model before codeIf the CAD model is wrong, the program is wrong. Check geometry and tolerances first.
Hand-code only simple partsTwo-axis turning and simple profiles are fine by hand. Complex 3D geometry is not.
Cut air before metalRun the dry run at rapid override 0% until every approach clears the stock.
Feeds come from the tool, not the partStart from the insert or end mill supplier data, then adjust for radial engagement.
Step 1 of the job

Read the drawing and the model before writing a line

Most programming errors start before the editor opens. Open the STEP or IGES file and the 2D drawing side by side. Confirm the revision letter matches on both. We have scrapped parts because the model was rev C and the drawing was rev B, and the programmer trusted the file name.

Mark every tolerance tighter than ±0.05 mm. Those are the features that decide your setup count, your tool list, and whether you need a fourth or fifth axis. A bore held at ±0.005 mm cannot share a roughing pass with a pocket held at ±0.2 mm.

Look for features you cannot reach. A 4 mm deep slot with a 3 mm corner radius needs a 6 mm end mill, and the tool holder may hit the wall before the cutter does. Check the holder body diameter against the wall height before you commit to a tool.

Decide the datum now. Pick a face and two edges, or a bore and a face, and write them on the setup sheet. Every operation after that references the same zeros. Changing datums mid-program is the fastest way to lose 0.1 mm between operations.

  • 1
    Revision checkModel and drawing must carry the same rev letter before programming starts.
  • 2
    Tight tolerance listAnything under ±0.05 mm gets its own operation and its own tool.
  • 3
    Holder clearanceHolder body diameter plus 1 mm must clear the wall before the cutter reaches depth.
Choosing the method

Hand-written G-code or CAM output

Hand-written G-code works for simple work: face and turn a shaft, drill a bolt circle, cut a rectangular pocket. You can write it at the machine, prove it in a few minutes, and change one number when the operator calls. The whole program might be 40 lines and you can read it in one pass.

CAM output wins as soon as the geometry stops being planar. A curved surface with variable depth, a 5-axis swarf cut, or a part with 60 pockets will take days by hand and still be wrong. CAM software also gives you rest material tracking, so you know the tool is not cutting air for 20 minutes.

The practical rule on our floor: if the part needs more than three tools or has any non-planar surface, it goes to CAM. If it is a turned part with a couple of grooves and a thread, hand-code it. Mixing the two is normal, and a good programmer will hand-edit the CAM output for the first approach move.

Whichever path you take, the output is the same format: a text file of G and M codes with feed, speed, and tool call blocks. The machine does not care who wrote it. It cares that the numbers are consistent and the rapid moves clear the fixture.

The numbers

Speeds, feeds, and depth of cut

Start from surface speed, not spindle rpm. For 6061 aluminium with a carbide end mill, we run 300–500 m/min surface speed. That puts a 10 mm cutter near 9,500–16,000 rpm, which most of our spindles reach. For 304 stainless, drop to 100–150 m/min, because the material work-hardens if you rub it instead of cutting it.

Feed per tooth is the number that breaks tools. In aluminium, 0.05–0.15 mm per tooth is normal for a 10 mm three-flute cutter. In stainless, use 0.03–0.08 mm per tooth and keep the chip load steady. If the chip looks like dust, you are rubbing. If it looks like a comma and comes off blue, you are close.

Depth of cut depends on radial engagement. Full-width slotting in aluminium is limited to about 0.5 × tool diameter axial depth. If you step over at 30% of the diameter, you can push axial depth to 1.5 × diameter or more. That is where high-efficiency milling comes from, and it is also why the same tool can run at two very different feed rates in one program.

Always check the machine limit before you post. A 16,000 rpm command on a spindle rated to 12,000 rpm will either alarm out or run at the wrong speed and burn the tool. Our 4,000 × 400 × 150 mm travel machines and our 500 × 500 × 450 mm compact machines have different spindle ranges, so the same program does not always transfer.

  • 1
    Aluminium 6061300–500 m/min, 0.05–0.15 mm per tooth, full-width axial depth 0.5 × D.
  • 2
    Stainless 304100–150 m/min, 0.03–0.08 mm per tooth, never dwell in the cut.
  • 3
    Titanium Ti-6Al-4V40–60 m/min, 0.03–0.06 mm per tooth, flood coolant always on.
Structure

Program structure that survives a shift change

Put the setup sheet inside the program as comments. Tool list, work offset, stock size, and the datum face go at the top. The operator running the second shift has never seen your setup notes on paper, but they will read the first 20 lines of the program.

Use one work offset per setup. G54 for op 1, G55 for op 2, and so on. Do not shift the same offset mid-program unless you also comment why. When the part comes back for op 2, the operator can verify G55 against the setup sheet without guessing.

Call the tool, start the spindle, and move to the first position in separate blocks. It costs three lines and saves a crash. The pattern is: T5 M06, then S9500 M03, then G00 Z50, then G00 X Y. If the tool length is wrong, the machine alarms before it moves in X or Y.

Cancel cutter compensation and coolant at the end of every tool. G40, M09, then G00 Z50. A program that leaves G41 active and hands off to the next tool will cut a taper on the first pass, and the operator may not catch it until the part is out of tolerance.

Before the spindle turns

Common mistakes and how to avoid them

The most expensive mistake is a wrong work offset. The program is perfect, the tool is perfect, and the machine cuts 50 mm into the fixture because G55 was set on the wrong corner. Always verify the offset with a dial indicator or a probe before the first cut.

The second is a missing G40 or a leftover G41. If cutter compensation stays active, the next tool cuts a part that is offset by the compensation value. On a 10 mm cutter with a 5 mm offset, that is a 5 mm error on every profile. Cancel compensation at the end of every operation.

The third is a feed rate that is too high for the radial engagement. A program that runs well at 30% stepover will break the tool at 100% stepover with the same feed per tooth. Recalculate the feed when you change the stepover, not just the spindle speed.

The fourth is forgetting to check the tool length after a change. A 0.2 mm difference in tool length shows up as a 0.2 mm step on the floor of a pocket. Measure every tool on the presetter or with a touch-off gauge, and update the offset before the program runs.

  • 1
    Verify the offsetProbe or indicate the work offset before the first cut. Never trust the number from the last job.
  • 2
    Cancel compensationG40 at the end of every operation, before the next tool call.
  • 3
    Recheck feed after stepover changeFeed per tooth is not constant when radial engagement changes.
Workflow

Step by step: writing the program

  • 1
    Set the datum and zero pointsChoose a face and two edges, or a bore and a face. Write the zero position and the work offset number (G54–G59) on the setup sheet. Do this before any geometry is imported.
  • 2
    Build the tool listList every cutter, its diameter, corner radius, flute count, and holder. Minimum tool diameter should be at least 1.5 × the smallest internal corner radius. If a corner is 3 mm, the smallest practical cutter is 5 mm.
  • 3
    Define stock and clearance planesSet the stock 0.5–1 mm larger than the finished part on faces you will machine. Set the rapid plane 5–10 mm above the highest stock point. Set the retract plane 50 mm above the fixture.
  • 4
    Program roughing firstUse the largest tool that reaches the deepest pocket. Leave 0.3–0.5 mm radial and 0.1–0.2 mm axial for finishing. Do not leave 1 mm and expect the finisher to clean it up; that is how you get chatter marks.
  • 5
    Program finishing with the right toolMatch the finishing tool to the corner radii and the surface finish callout. For Ra 0.8–1.6 μm, use a sharp carbide with a 0.4–0.8 mm corner radius and a light stepover of 0.1–0.2 mm.
  • 6
    Add the drilling and tapping cyclesUse G81 for through holes, G83 for holes deeper than 3 × diameter, and G84 for rigid tapping. Set the peck depth at 1 × diameter for stainless and titanium, 2 × diameter for aluminium.
  • 7
    Post and check the codePost the program, then read the first 30 lines and the last 30 lines. Confirm the units (G21 for mm, G20 for inch), the plane (G17), and the feed mode (G94). One wrong G20 in a metric program will crash the machine.
  • 8
    Dry run at reduced rapidRun the program with the tool 100 mm above the part and rapid override at 0%. Watch every tool change and every approach. Only after a clean pass do you lower the tool and cut air at the stock height.
Decision table

Hand-code or CAM: which fits the job

Use this table to pick the programming method before you start.

Job characteristicHand-written G-codeCAM output
2-axis turned part with one threadBest fitWorks but slower to set up
Planar pocket with straight wallsFine for 1–3 toolsBetter if many pockets
Non-planar 3D surfaceNot practicalRequired
5-axis simultaneous motionNot practicalRequired
More than five toolsError-proneBetter tool management
One-off repair or reworkFaster at the machineOverhead too high
Production run over 500 partsOnly for simple partsBetter for repeatability
Feature tolerance tighter than ±0.01 mmPossible with careBetter with rest tracking
FAQs

Frequently asked questions

Can I write a CNC program directly from a 2D drawing?

Yes, for turned parts and simple prismatic parts. You define the profile, the tool path, and the offsets by hand. The risk is in the geometry you cannot see: a 3D blend or a drafted wall will not appear on a 2D view.

For anything with a curved surface, start from the 3D model. The 2D drawing is a reference, not the source.

What speed and feed should I start with for aluminium?

For 6061 with a carbide end mill, start at 300–500 m/min surface speed and 0.05–0.15 mm per tooth. Set axial depth at 0.5 × tool diameter for full-width cuts.

If the chip is thin and powdery, increase the feed per tooth. If the cutter squeals, reduce the radial stepover before you reduce the feed.

How do I know if my program is safe to run?

Run the dry run with rapid override at 0% and the tool 100 mm above the highest point of the stock. Watch every rapid move, every tool change, and every approach.

Then lower the tool and run the program at the stock height with the spindle on. Only after both passes are clean do you cut the part.

Why does my part come out with a step between operations?

The work offset or the tool length changed between operations. A 0.05 mm difference in tool length shows up as a visible step on a finished face.

Re-measure the tool and re-verify the offset. If the step is larger than 0.1 mm, check the fixture for movement or chips under the part.

Do I need a fifth axis to program a part with angled holes?

Not always. If the angle is on one face and the part can be repositioned, a 3-axis machine with a sine plate or a vice angle will do the job.

A fifth axis makes sense when the part has angled features on multiple faces and you want one setup. It also removes the repositioning error.

How long should a CNC program take to write?

A simple turned part takes 30–60 minutes including the dry run. A 3-axis milled part with three setups takes 2–4 hours. A 5-axis part with surfacing can take a full day.

The programming time is not the bottleneck. The setup and the first-article inspection usually take longer.

Send us the model and we will review the program path

Upload your STEP file and we will return a DFM analysis and a quotation within 12 hours. No minimum order quantity, from one prototype to 10,000+ parts.

12-hour quote100% inspectionNDA on request

Follow our work

More from the 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