Basic Methods of Manual G-Code Programming for CNC Machining - ST
  • Over
  • Blog
  • Contact

Basic Methods of Manual G-Code Programming for CNC Machining

CNC Machining Manual G-Code Programming: The Foundation Every Machinist Needs to Actually Write Code

Most machinists learn to operate a CNC machine long before they learn to program one. They push buttons, they load programs, they hit cycle start. But the day comes when the program crashes, the post-processor throws an error, or the part geometry is just unusual enough that no canned cycle handles it. That is the day you need to write G-code by hand. And if you have never done it, it feels like learning a foreign language where one wrong letter ruins the entire part. The good news is that manual G-code programming is not magic. It is a set of rules that, once you internalize them, become second nature. This is how you start from zero and write your first program without burning anything down.

Why Learn Manual G-Code When CAM Software Exists

CAM software is powerful. It generates toolpaths, it optimizes feeds and speeds, it simulates the entire cut before you ever touch the machine. So why bother learning G-code?

Because CAM does not always give you what you need. A CAM program might generate a toolpath that is inefficient for your specific machine. The post-processor might not support a particular canned cycle your controller uses. You might need to make a quick edit on the shop floor and opening the CAM file, regenerating the toolpath, and re-post-processing takes 20 minutes when you could fix the code in 30 seconds.

Manual G-code also teaches you what the machine is actually doing. When you write G01 X50.0 F200, you understand that the tool is moving in a straight line at 200 mm per minute. When CAM generates that same line, it is abstract. You do not feel the machine. Learning manual programming builds an intuition that makes you a better machinist regardless of whether you ever write a full program by hand again.

The Core G-Codes You Cannot Avoid

There are over 100 G-codes in the full standard. You do not need all of them. You need about 15 to get started, and another 10 to handle most real-world situations.

Movement Codes: G00, G01, G02, G03

G00 is rapid traverse. The machine moves at maximum speed from one point to another. No cutting happens during G00. You use it to position the tool above the part, to clear the cut, or to move between features.

G01 is linear interpolation. The tool moves in a straight line at the feed rate you specify with F. This is the workhorse of every program. Every cut you make is a G01 move unless you are using a canned cycle.

G02 is clockwise circular interpolation. G03 is counterclockwise. You use these for arcs, pockets, and any curved feature. The arc is defined by either the radius (R) or the endpoint coordinates (I, J, K). I and J are the incremental distances from the start point to the center of the arc. K is used for helical moves.

The trick most beginners miss is that G02 and G03 require the arc to be less than 180 degrees if you use R. If the arc is more than 180 degrees, you must use I, J, K because R only works for arcs up to 180. This is not intuitive until you have crashed a tool into a part because you used R for a 270-degree arc.

Positioning Codes: G90, G91, G54, G55, G56

G90 is absolute positioning. Every coordinate you write is relative to the program zero. G91 is incremental positioning. Every coordinate is relative to the current position. G91 is useful for drilling patterns or repetitive features where you do not want to calculate every absolute position.

G54, G55, G56 are work offset selectors. G54 is usually the main vise offset. G55 might be a second vise or a fixture offset. G56 could be a third setup. You set these offsets on the machine by probing a reference point on the part and entering the values into the offset table. The program then calls G54 and the machine knows where zero is.

Never assume G54 is set correctly. Always verify the offset before running a new program. A G54 that is off by 0.5 mm will move the tool 0.5 mm away from where you think it is. That is enough to crash into a vise or leave material uncut.

Feed and Speed Codes: F, S, M03, M04, M05

F sets the feed rate in mm per minute for G01 moves or mm per revolution for G94 mode. S sets the spindle speed in RPM. M03 starts the spindle clockwise. M04 starts it counterclockwise. M05 stops the spindle.

The feed rate you choose depends on the operation. For roughing steel, you might run F300 to F500. For finishing aluminum, you might drop to F100 to F200. The spindle speed depends on the material and the tool diameter. A good starting point for a 10 mm end mill in aluminum is S8000 to S12000. In steel, drop to S2000 to S4000.

These numbers are starting points, not gospel. Adjust based on how the cut sounds, how the chips look, and how the tool wears. But having a starting point is better than guessing.

Structuring a Program: The Skeleton That Holds Everything Together

A G-code program is not a random list of commands. It has a structure. Break the structure and the program will not run, or worse, it will run and do something you did not expect.

Program Start and Safety Lines

Every program should start with a safety block. This is the first thing the machine reads and it sets the initial state.

A typical start block looks like this: G21 (metric units), G90 (absolute positioning), G17 (XY plane), G40 (cutter compensation cancel), G49 (length compensation cancel), G80 (canned cycle cancel), G54 (work offset). Then M03 S2000 to start the spindle.

This block ensures that no matter what state the machine was left in from the previous program, your program starts clean. If you skip this and the previous program left G41 active, your tool will move off by the radius of the cutter the moment it starts cutting. That is a crash waiting to happen.

Tool Change and Tool Offset

After the start block, you load the tool and set the length offset. T01 M06 loads tool 1 and changes the tool. G43 H01 Z50.0 applies the length offset from register 01 and moves the tool to Z50.0 clearance height.

The H number must match the offset register where you stored the tool length. If you measured the tool and stored it in H05 but the program calls H01, the Z position will be wrong by the difference between the two offsets. Always double-check the H number against the offset table.

The Cutting Block and Retract

After positioning, you make your cut with G01 or G02/G03. Then you retract with G00 to a safe height. The retract is just as important as the cut. If you retract to Z0.0 instead of Z50.0, the tool might drag across the part on the way out and leave a scratch.

A simple cutting block: G00 X10.0 Y10.0 (rapid to start point), G01 Z-5.0 F100 (plunge), G01 X50.0 Y10.0 F300 (cut), G00 Z50.0 (retract). That is a complete profile cut in five lines of code.

Program End and Reset

End every program with a reset block. G00 Z50.0 (retract to safe height), M05 (stop spindle), M30 (end of program and rewind). M30 tells the controller the program is done and resets the cursor to the beginning. If you use M02 instead, the program ends but the cursor stays at the end. For manual programming, always use M30 so you can re-run the program immediately.

Canned Cycles: The Shortcuts That Save Hours

Canned cycles are pre-programmed sequences that handle drilling, pecking, boring, and tapping without you writing every individual move. They are the single biggest time-saver in manual programming.

Drilling Cycle: G81

G81 is the simplest drilling cycle. You give it an X, Y position, a Z depth, an R clearance plane, and a feed rate. The cycle drills to Z, retracts to R, and moves to the next position.

G81 X10.0 Y10.0 Z-15.0 R2.0 F100 means drill at X10 Y10 to a depth of 15 mm, retract to 2 mm above the part, and feed at 100 mm per minute. One line of code replaces what would be five or six lines of individual G00 and G01 moves.

The R plane is critical. If you set R too close to the part surface, the tool will crash into the part on retract. Set R at least 2 to 3 mm above the surface for safety.

Peck Drilling Cycle: G83

G83 is for deep holes where chips need to clear. The tool pecks down a small amount, retracts to clear chips, then pecks again. This prevents chip packing and tool breakage.

G83 X10.0 Y10.0 Z-30.0 R2.0 Q5.0 F100 means drill to 30 mm deep, pecking 5 mm at a time, retracting to R between each peck. The Q value is the peck depth. Set it based on the hole diameter — typically 2 to 3 times the diameter for steel, up to 5 times for aluminum.

Boring Cycle: G76

G76 is a fine boring cycle with a dwell at the bottom. The dwell lets the tool finish cutting the bottom of the hole for a better surface finish and tighter diameter control.

G76 X10.0 Y10.0 Z-20.0 R2.0 Q0.1 F80 means bore to 20 mm deep with a 0.1 mm cut at the bottom and a dwell at full depth. The Q value is the finish cut amount. A small Q gives a better finish but takes more time. For clearance holes, Q0.5 is fine. For precision bores, drop to Q0.05.

Tool Compensation: G41, G42, G40

Tool compensation lets you program to the part geometry and let the controller adjust for the tool radius. Without it, you have to calculate the tool center path manually, which is tedious and error-prone.

How Cutter Compensation Actually Works

G41 is compensation to the left of the programmed path. G42 is compensation to the right. G40 cancels compensation.

When you write G41 G01 X50.0 Y0.0 F200, the controller does not move the tool to X50 Y0. It moves the tool to a position that is one tool radius to the left of that point, so the cutting edge follows the programmed path.

The direction matters. G41 and G42 are determined by the direction of travel. If you are moving in the positive X direction, G41 puts the tool below the path (negative Y side). If you are moving in the negative X direction, G41 puts the tool above the path (positive Y side). This is counterintuitive at first. The rule is: imagine you are walking along the programmed path in the direction of travel. G41 puts the tool on your left. G42 puts it on your right.

Lead-In and Lead-Out with Compensation

You cannot engage G41 or G42 mid-cut. The tool will jump to the offset position instantly, which can gouge the part. You need a lead-in move — a short linear move that brings the tool into the compensated path smoothly.

A typical engagement: G00 X0.0 Y0.0 (rapid to start), G01 X10.0 Y0.0 F100 (lead-in along the path), G41 D01 (engage compensation, D01 is the tool radius offset register), G01 X50.0 Y30.0 F200 (cut the profile), G40 G01 X60.0 Y30.0 (cancel compensation on a linear move away from the part), G00 Z50.0 (retract).

The lead-in and lead-out must be long enough for the controller to build up the compensation gradually. A lead-in of at least 2 times the tool diameter is a safe rule of thumb.

Common Mistakes That Crash Parts on the First Run

Every programmer crashes something. The difference between a good programmer and a bad one is that the good programmer crashes something small and learns from it. Here are the mistakes that catch beginners every single time.

Forgetting the Decimal Point

G-code is picky about decimals. If you write X50 instead of X50.0, some controllers interpret it as 0.050 mm instead of 50 mm. That is a 1000x error. Always use the decimal point. X50.0, Y25.0, Z-10.0. Make it a habit.

Mixing Modal and Non-Modal Codes

Modal codes stay active until you change them. G01 is modal — once you write it, every subsequent move is linear until you write G00 or G02. Non-modal codes execute once and then deactivate. G04 (dwell) is non-modal.

The danger is forgetting that a modal code is still active. If the previous block ended with G02 and you start the next block with G01 without realizing G02 is still active, the first move of your new block will be an arc, not a line. That arc can drive the tool straight into a clamp.

Always assume the machine is in the last modal state you left it. Explicitly set G01, G17, G90 at the start of every program to clear any lingering modal codes.

Wrong Plane Selection: G17, G18, G19

G17 selects the XY plane. G18 selects XZ. G19 selects YZ. If you are cutting a profile on the top of a part, you need G17. If you accidentally leave G18 active from a previous program, your G01 X and Y moves will actually be X and Z moves. The tool will plunge into the part instead of moving across the top.

Always include G17 in your start block. It takes one line of code and it prevents a crash that could cost you a tool and a part.

Debugging Your First Programs: A Practical Approach

You wrote the code. It looks right. Now you need to run it without destroying anything.

Single Block and Dry Run

Put the machine in single block mode. The controller will execute one line of code at a time, waiting for you to press cycle start after each line. This lets you watch every move and catch errors before they happen.

Run the program with the spindle off and the Z axis raised. Watch the tool position on the machine display. Verify that every X, Y, Z move goes where you expect. If a move looks wrong, hit the feed hold, check the code, fix it, and continue.

Override and Rapid Traverse

Use feed override to slow the machine down to 5 percent or less. Use rapid override to slow rapid moves. This gives you time to react if something goes wrong. Full-speed runs are for production, not for debugging a new program.

The Air Cut Test

Before cutting actual material, run the program with the tool 50 mm above the part. This is called an air cut. The tool follows the entire path without touching anything. Watch the motion. Does it look smooth? Are there any sudden jumps? Does the tool retract to a safe height between features?

If the air cut looks good, lower the tool to 5 mm above the part and run it again. Watch closely. If that looks good, start cutting. Three steps instead of one. It takes five extra minutes and it saves you from a crash that could take an hour to clean up.

Building Simple Programs: A Walkthrough

Let us put it all together with a simple part. A 50 mm square pocket, 5 mm deep, in a 60 mm by 60 mm aluminum block.

Start block: G21 G90 G17 G40 G49 G80 G54. Safety line, check.

Tool load: T01 M06. G43 H01 Z50.0. Tool is loaded and positioned at safe height.

Spindle: M03 S8000. Spindle on at 8000 RPM for aluminum.

Rapid to start: G00 X5.0 Y5.0. Position above the pocket corner. We start 5 mm in from the edge because the tool has a radius. If we start at X0 Y0, the tool center would be at the edge and the cutter would overcut by its radius.

Plunge: G01 Z-5.0 F100. Cut down to 5 mm depth at 100 mm per minute.

Cut the pocket: G01 X55.0 F300. G01 Y55.0. G01 X5.0. G01 Y5.0. Four lines cut the square. The feed rate is 300 mm per minute for aluminum finishing.

Retract: G00 Z50.0. Pull the tool up.

End: M05 M30. Stop and rewind.

That is a complete pocket program in about 15 lines of code. No CAM. No post-processor. Just G-code and a clear understanding of what each line does. Once you can write this, you can write anything. The rest is just more lines and more features.

E-mail
Email: [email protected]
WhatsApp
WhatsApp QR-code
(0/8)