Featured image of CNC Milling Programming: Basic CNC G-Code Tutorial

CNC Milling Programming: Basic CNC G-Code Tutorial

Renato Calvinisti
Nov 20, 2019

G-code is everywhere, from 3D printing to CNC milling. Learn all about CNC milling and programming in this guide and tutorial!

All3DP is an editorially independent publication. Editorial content, on principle, can not be bought or influenced. To keep All3DP free and independent, we finance ourselves through advertising and affiliate revenues. When you purchase using a shopping link on our site we earn an affiliate commission. Learn more
Advertisement
Contents
CNC Milling Programming

Understanding the Basics

Manual G-code programming on a Haas Control
Manual G-code programming on a Haas Control (Source: helmancnc.com)

CNC stands for computer numerical control. It’s a process used to manipulate CNC mills and lathes as well as additive manufacturing machinery like 3D printers. G-code stands for “geometric code” and this is the language most CNC machinery uses.

Programming G-code might sound frightening at first, but we’re here to help with the basics. Programming is a fundamental skill for all types of CNC machinery, not just CNC mills. While programs used to design parts for CNC milling, such as Autodesk Fusion 360 and SolidWorks CAM, can create G-code without you ever touching the keyboard, understanding how to use G-code will not only let you write your own custom programs but also troubleshoot when a machine fails.

Every machine uses a slightly different type of G-code, so a Fanuc G-code won’t work on a Haas controller, however, the basics are the same. So let’s dive into the details of programming CNC milling G-code!

Advertisement

Back to Contents

CNC Milling Programming

Coordinate Systems

The G55 and G54 WCS offsets
The G55 and G54 WCS offsets (Source: Autodesk)

Additive machines like 3D printers build parts from the ground up, so they always start from the build plate no matter the part geometry. CNC mills, on the other hand, can start basically anywhere in the machine build space, so letting your machine know the location of the stock material is crucial, otherwise, your machine will just be lost.

CNC machines each have their own coordinate system with a fixed origin. “Homing” your CNC mill will hit the limit switches on the X-, Y-, and Z-axes, but this point is usually very tedious to work with.

In contrast, the work coordinate system (WCS) defines an origin point anywhere the programmer wants. This is usually set by the G54 command, which offsets the machine coordinate system.

Advertisement

Back to Contents

CNC Milling Programming

Plane Designations

The planes for the 3 axes
The planes for the 3 axes (Source: Siemens )

In CNC milling machines, you have three planes defined by the main three axes of the machine: X/Y, X/Z, and Y/Z. Yet, as the number of axes on a milling machine increases, things start to get more complex, with added rotational movements, such as on a 6-axis CNC machine. Defining the plane in which the machine will be working in is crucial for it to know where to mill and how to calculate offsets correctly.

Once the machine knows in which plane to work and the operator has dialed in the new WCS (via probing, an edge finder, or using dial indicators), the machining process can start. The action of dialing the WCS into the machine is commonly known as “zeroing” the machine.

Advertisement

Back to Contents

CNC Milling Programming

Command Structure

Some G-code samples
Some G-code samples (Source: Autodesk)

G-code is a variation of an alphanumeric pattern. A standard G-code command conforms to the following logic:

N## G## X## Y## Z## F## S## T## M##

  • N is the line number the program is in, it ensures the program follows a logical order.
  • G indicates a motion using the Cartesian coordinates that follow. There are many G commands (hence the name G-code) and they vary from machine to machine, so make sure to check your controller manual. Nevertheless, there are three basic types of G-codes:
    • G00 is used for rapid, non-cutting movements.
    • G01 is used for linear movements at a programmed feed speed, usually used to cut material.
    • G02 is used for circular movements at a feed speed.
  • X Y Z commands are used to define X, Y, and Z coordinates. The Z-axis is the “scariest” to program and the one we recommend being extra careful with, as crashing a machine spindle is always an expensive fix. This is where simulating comes in handy. (More on that later.)
  • F stands for feedrate, which is basically how fast should the machine should move from point to point. It’s usually expressed in inches/minute or mm/minute.
  • S is spindle speed, this determines how fast the spindle should rotate in rpm.
  • T is used for tool selection, most CNC mills now have 10 or more tools ready to use on the machine.
  • M are miscellaneous functions to turn things on and off, flood coolant, air blast, close machine doors, and more. Every machine has different M commands, so make sure to read your machine’s manual carefully.
  • I and J are used for arcs. The X and Y coordinates of where the arc ends are needed as well as the X and Y distances from the arc start point to the arc center point.

Take a look at the following G-code example:

N24 G01 X10 Y20 Z5 F300 S3000 T1

Breaking it down, we can determine that

  • the program is in line 2;
  • the spindle is going to perform a feed move (G01) from (X0, Y0, Z0), the starting position, to (X10, Y20, Z5);
  • the selected tool is tool 1 (T1);
  • the feed speed is 300 mm/min (F300); and the spindle rpm is 3000 (S3000).

Easy, right? A great G-code guide can go a long way when you get lost on what each G-code segment means.

Something you may be wondering at this point is whether CNC milling G-code uses absolute or relative positioning. In fact, you can use either, and this is something we’ll have to tell the machine. (More on that later.) But for now, we’ll assume absolute positioning, writing the position (10, -20, 30.5), for example, as (X10, Y-20, Z30.5) and leaving out a coordinate if it remains unchanged from the previous position.

Advertisement

Back to Contents

CNC Milling Programming

Program Logic and Cycles

An example of a drilling cycle
An example of a drilling cycle (Source: Renato Calvinisti via ALL3DP)

To understand G-code, you need to know how a program is formatted. G-code programs follow the following structure:

  • Program header: This starting G-code basically tells the machine how to prepare itself for the upcoming job. Common aspects include stock dimensions, offsets, and planes.
  • Program blocks: Also called the body of the program, this is where the main part of the job is described, including axis moves, speeds, feeds, and tool changes. Codes following the logic described in the previous section go here. (Most errors also appear here.)
  • End of program: This part tells the machine that the work has ended so it can retract to a safe position and turn everything off.

The above image presents an example of these program sections, but it also shows something else. As you can see, the lines have very little variation, only the Z coordinates are changing, while everything else remains the same. This is called a cycle.

Basically, a cycle is a group of simple operations that together form complex movements, like 3D contours, drilling, ramping, and slotting. In this particular example, the code incorporates a center drilling cycle followed by a peck drilling cycle.

Advertisement

Back to Contents

CNC Milling Programming

Your First CNC Mill G-Code

A part drawing done in Inventor Professional 2017
A part drawing done in Inventor Professional 2017 (Source: Renato Calvinisti via ALL3DP)

Now that you’ve got the basics, writing a simple program should be easy. Let’s say we want to contour mill the part in the above drawing with a ½-inch mill. All measurements are expressed in inches (no conversion needed), so let’s get to it!

Advertisement

Back to Contents

1
CNC Milling Programming

Setting the Work Coordinate System

The Work Coordinate System location
The Work Coordinate System location (Source: Renato Calvinisti via ALL3DP)

First, locate the WCS in the easiest possible location. In this case, it’s the exact center of the part. What size mill is going to be used is very important as the programmer should offset that too, but more on that later.

Advertisement

Back to Contents

2
CNC Milling Programming

Ordering Your Path

The elected milling toolpath
The elected milling toolpath (Source: Renato Calvinisti via ALL3DP)

As with any other programming language, solving a problem with CNC mill G-code can be done in many different ways. That said, optimal machining paths, which make a program as efficient as possible, should always be the programmer’s goal. Generally speaking, efficiency equates to the fewest possible lines of code.

The way you order your path will directly affect what the program will look like. Using the drawing as a guide, we’ll program for the following path, where each line is an instruction that we’ll later translate to G-code:

  1. This is where the program will start. As the position (X0, Y0, Z0) is implied, no G-code will be necessary.
  2. Move rapidly and diagonally to the starting point, located at (X-0.75, Y-1.5).
  3. Plunge rapidly to Z-0.5, the desired depth of the contour.
  4. Move linearly up at feed speed to (X-0.75, Y0.75).
  5. Move at an arc to (X0.75, Y0.75). For this edge, we also need the X and Y distances from the beginning of the arc to the center of the arc. For X, this is 0.75 (I0.75), and for Y, this is 0 (J0).
  6. Move linearly down at feed speed to (X0.75, Y-1.5).
  7. Move linearly left at feed speed to the starting point (X-0.75, Y-1.5).
  8. Retract rapidly to a safe point Z0.
  9. Move rapidly to (X0, Y0).
Advertisement

Back to Contents

3
CNC Milling Programming

Translating to G-code

A simulation of the written G-code
A simulation of the written G-code (Source: Renato Calvinisti via ALL3DP)

Essentially, the body of the program should have a total of eight lines for the desired contour. Once our path has been chosen, translation from instructions to G-code language will look as follows:

  1. G00 X-0.75 Y-1.5 This line moves the spindle rapidly (G00) from (X0, Y0) to (X-0.75, Y-1.5).
  2. G01 Z-0.5 F3 This line plunges the spindle (G01) from Z0 to Z-0.5 at a feed speed of 3 in/min (F3).
  3. G01 X-0.75 Y0.75 F30 This line moves the spindle (G01) from (X-0.75, Y-1.5) to (X-0.75, Y0.75) at a feed speed of 30 in/min (F30).
  4. G02 X0.75 Y0.75 I0.75 J0 F30 This line moves the spindle in an arc movement (G02) from (X-0.75, Y0.75) to (X0.75, Y0.75) with a radius of 0.75 inches (I0.75) at a feed speed of 30 in/min (F30).
  5. G01 X0.75 Y-1.5 F30 This line moves the spindle (G01) from (X0.75, Y0.75) to (X0.75, Y-1.5) at a feed speed of 30 in/min.
  6. G01 X-0.75 Y-1.5 F30 This line moves the spindle (G01) from (X0.75, Y-1.5) to (X-0.75, Y-1.5) at a feed speed of 30 in/min (F30).
  7. G00 Z0 This line moves the spindle rapidly (G00) from Z-0.5 to Z0.
  8. G00 X0 Y0 This line moves the spindle rapidly from (X-0.75, Y-1.5) to (X0, Y0).

Now it’s time to simulate! Numerical control (NC) simulation is super important, as you need to test what you’ve done in order to know that the movements are correct (and that you won’t run into dangerous or expensive problems). If you’re not familiar with NC simulators, we recommend you take a look at NC Viewer, which is a great free online NC simulator.

Advertisement

Back to Contents

4
CNC Milling Programming

Setting the Tool Offset

On the left is the basic program, on the right is the same program with the correct offset
On the left is the basic program, on the right is the same program with the correct offset (Source: Renato Calvinisti via ALL3DP)

Once the base G-code is done, applying the ½-inch mill offset should be the next step. Currently, our program is written under the assumption that the mill is at the center of the contour, which would be incorrect, as half of the tool will be working inside the contour (as shown above).

In order to fix this, an offset of 0.125 inches (half the tool width) to the corresponding sides of all coordinates is necessary. Contours on the left need to be offset 0.125 inches to the left, contours on the right need to be offset 0.125 inches to the right, and contours up and down need to be offset 0.125 inches up and down, respectively. Applying this, the resulting program should look as follows:

  1. G00 X-0.875 Y-1.5 (Rapid diagonal movement)
  2. G01 Z-0.5 F3 (Plunge movement at 3 in/min)
  3. G01 X-0.875 Y0.75 F30 (Linear movement up at 30 in/min)
  4. G02 X0.875 Y0.75 I0.875 J0 F30 (Arc movement at 30 in/min)
  5. G01 X0.875 Y-1.625 (Linear movement down at 30 in/min)
  6. G01 X-0.875 Y-1.625 (Linear movement left at 30 in/min)
  7. G00 Z0 (Rapid retraction)
  8. G00 X0 Y0 (Rapid diagonal movement back to the WCS)
Advertisement

Back to Contents

5
CNC Milling Programming

Writing Cycles

A simuation of the contour cycle
A simuation of the contour cycle (Source: Renato Calvinisti via ALL3DP)

Let’s say that, instead of a contour depth of 0.5 inches, a 0.7-inch contour is desired, 0.3 inches deeper than the original program. All that’s needed is to repeat the program and add -0.3 to the Z-coordinate in Line 2, resulting in G01 Z-0.8 F3. Everything else remains the same!

  1. G00 X-0.875 Y-1.5 (Rapid diagonal movement)
  2. G01 Z-0.5 F3 (Plunge movement at 3 in/min)
  3. G01 X-0.875 Y0.75 F30 (linear movement up at 30 in/min)
  4. G02 X0.875 Y0.75 I0.875 J0 F30 (arc movement at 30 in/min)
  5. G01 X0.875 Y-1.625 (linear movement down at 30 in/min)
  6. G01 X-0.875 Y-1.625 (linear movement left at 30 in/min)
  7. G00 Z0 (Rapid retraction)
  8. G00 X0 Y0 (Rapid diagonal movement back to the WCS)
    • (Next cycle, 0.3 inches deeper)
  9. G00 X-0.875 Y-1.5 (Rapid diagonal movement)
  10. G01 Z-0.8 F3 (Plunge movement at 3 in/min)
  11. G01 X-0.875 Y0.75 F30 (linear movement up at 30 in/min)
  12. G02 X0.875 Y0.75 I0.875 J0 F30 (arc movement at 30 in/min)
  13. G01 X0.875 Y-1.625 (linear movement down at 30 in/min)
  14. G01 X-0.875 Y-1.625 (linear movement left at 30 in/min)
  15. G00 Z0 (Rapid retraction)
  16. G00 X0 Y0 (Rapid diagonal movement back to the WCS)
Advertisement

Back to Contents

6
CNC Milling Programming

Adding the Final Touches

The simulation of the final G-code
The simulation of the final G-code (Source: Renato Calvinisti via ALL3DP)

Now that the body of the G-code is done, the header and end of program G-code can be added. Some trimming also doesn’t hurt if it makes the G-code more efficient.

One thing to note, here: A G-code will stay “on” unless another G-code turns it off. This means that repeating the same G-code every single time is unnecessary. For this example, we’re going to be adding the G-code header and ending for a Mach4 controller as well as doing some final trimming.

Header

  1. G90 G94 G91.1 G40 G49 G17 (Machine-specific starting G-code)
  2. G20 (Use inches as units)
  3. G28 Z0 (Go to predetermined “Home” Z0 position)
  4. G90 (Use absolute positioning)
  5. T1 M6 (Perform tool change to tool number 1)
  6. S1528 M3 (Start spindle clockwise at 1528 rpm)
  7. G54 (Use WCS offsets saved in G54)
  8. M8 (Turn cooling on)

Body

  • (First cycle for 0.5-inch deep contour)
    1. G00 X-0.875 Y-1.5
    2. G01 Z-0.5 F3
    3. Y0.75 F30
    4. G02 X0.875 Y0.75 I0.875 J0
    5. G01 Y-1.625
    6. X-0.875
  • (Sext cycle for 0.3-inch deeper contour)
    1. Y-1.5
    2. Z-0.8 F3
    3. G01 Y0.75 F30
    4. G02 X0.875 Y0.75 I0.875 J0
    5. G01 Y-1.625
    6. X-0.875
    7. Y-1.5

Ending

  1. M9 (Turn cooling off)
  2. G28 Z0 (Go to home position in Z)
  3. G28 X0 Y0 (Go to home position in X and Y)
  4. M30 (End of program, rewind and reset modes)
Advertisement

Back to Contents

CNC Milling Programming

Wrapping Up

Happy milling!
Happy milling! (Source: Renato Calvinisti via ALL3DP)

G-code knowledge is very useful either to program or to troubleshoot an existing program. Nowadays, awesome post-processors and CAD/CAM software take a lot of the load off of programmers, however, understanding the basics helps us understand why CNC machines work the way they do, in some cases allowing us to overcome issues or make work processes more efficient.

There are literally thousands of G-codes, and we only scratched the surface of this language. Experimenting in an NC simulator is a good way to learn your way around and advance your G-code programming skills.

Whether you’re writing your own G-code or troubleshooting an existing program, we hope this article helped you!

(Lead image source: dmgmori.com)

Advertisement

Back to Contents

License: The text of "CNC Milling Programming: Basic CNC G-Code Tutorial" by All3DP is licensed under a Creative Commons Attribution 4.0 International License.

Subscribe to updates from All3DP

You are subscribed to updates from All3DP

You can’t subscribe to updates from All3DP. Learn more… Subscribe

You can’t subscribe to updates from All3DP. Learn more…

Advertisement
Recommended for you
Topics
Follow us

Advertisement