Essential G-Code: A Simple Introduction to CNC Mill Programming
Overview
G-Code is the programming language for the majority of CNC Machine tools, including CNC Mills and CNC Routers, as well as many other tools used in automated manufacturing, including CNC Lathes, Pen Plotters, Press Brakes, Grinders, 3D printers, and much more.
Even the simplest CNC milling machine understand several dozen G-codes; however, a complete knowledge of all available codes isn’t necessary to program simple CNC operations. This lesson focuses on simple program construction and syntax, basic motion commands (G0, G1, G2/G3), and Absolute/Incremental (G90/G91) motion definition. The lesson includes several example student activities, as well as a pen plotting exercise that can be easily adapted to any CNC Mill.
Lesson Concepts
- Basic Program Construction
- Absolute vs. Incremental Programming
- Learn Fundamental G Code Programming Commands
- CNC Pen Plotting
You can learn more about specific G -Codes here: Tormach Machine Code Reference.
For a complete treatment of G-code programming concepts, I recommend reading CNC Programming Handbook, by Peter Smid.

Helpful Prerequisites Concepts
- Cartesian (XYZ) Coordinates
Defining Location: Cartesian Coordinates
To move a CNC mill, a list of sequential points are given to the machine to program a tool path. This works much like a “Connect-the-Dots” puzzle. The locations of the points are given in Cartesian (XYZ coordinates) and can be represented on a sheet of graph paper. Each line (sometimes called “block”) of G-Code can be thought of as a instructions to connect the next “Dot” in the tool path sequence.*
*Each line of code often starts with an “N” number, such as “N005″ or “N010″. These are the line numbers. With many CNC controllers, line numbers are optional – they are displayed but not read. On long programs, they can be helpful for identifying a particular line of code.
Two Methods of Movement: Absolute vs. Incremental
The two methods of movement when using a CNC mill are absolute and incremental movement. Absolute movement uses the origin (X0Y0Z0) as a reference. A familiar analogy to absolute programming is GPS coordinates. If given instructions to move to 41° 51’0 N, 87° 39’ 0 W, everyone reading this material would meet at a single point.
Incremental programming uses the current location as a reference. Sticking with the same analogy, consider two people at different GPS locations. If both are told to move incrementally 1 mile to the east, the end location would not be the same, because the end point is dependent on the current position.
Both absolute movement and incremental movement have advantages and disadvantages in machining. Incremental programming is good in cases of patterns or repetitive movements, such as making twenty holes spaced 1-inch apart. One disadvantage of using incremental programming arises if a mistake is made in a program. If one movement is incorrect the rest of the movements in the sequence are also dimensionally incorrect. The majority of CNC programming is done with absolute programming; however, incremental has its place as well.
At the start of each G-Code Program, it needs to be decided whether the program will be in absolute (G90) or incremental (G91) mode.
G-Code 101
The most commonly used CNC programming language is G-code. The program consists of lines with G and M commands. G commands are known as movement commands or functions. M commands control other essential CNC machine functions, like spindle on/off. Each command starts with either the letter “G” or “M”, followed by a number, i.e. “G01″,”M08″, etc.
The table below lists a few of the most essential G code commands:
|
G Commands
|
M Commands
|
| G90 |
Absolute Programming |
M01 |
Optional Stop |
| G91 |
Incremental Programming |
M02 |
Program Stop |
| G20 |
Inches Program |
M03 |
Spindle On (CW) |
| G21 |
mm Program |
M04 |
Spindle On (CCW) |
| G00 |
Rapid Move |
M05 |
Spindle Off |
| G01 |
Linear Feedrate Move |
M06 |
Tool Change |
| G02 |
Clockwise Arc |
M30 |
Program End |
| G03 |
Counterclockwise Arc |
|
|
Let’s take a look at the three basic movement commands: Rapid Motion (G00), Linear Motion (G01), and Arc Motion (G02/G03)
G00: Rapid Motion
The machine will move rapidly using a G00 command. These rapid moves are intended for movement when the tool is not cutting the material. A sample line of rapid movement can be written as:
How fast is “Rapid”? It’s determined by the CNC machine designer. In the case of a Tormach PCNC 1100 Series 3, the rapid motion limit is set to 110 IPM. Any G00 command will move the mill at a straight line velocity of 110 IPM to the XYZ coordinate location specified.
G01: Linear Motion
Linear moves are specified as a G01 command. The program will perform a straight line from the current position to the desired end position. A linear move to the Cartesian position X=1.0 Y=1.0 should be written as:
N05 G01 X1.0 Y1.0
When executing the first G01 command in the program a feedrate (F) must be included. A linear move to the Cartesian position X=1.0 Y=1.0 at a speed of 20 inches/min can be written as:
N05 G01 X1.0 Y1.0 F20.0
Example 1: Star
Try writing a program that draws a star using both absolute (G90) and incremental (G91) programming modes
Example Star Program (Absolute Programming - Linear Moves Only)
%
N05 G90 G20 (Absolute & Inches)
N10 S6000 M03 (Spindle On, 6000 RPM)
N15 G00 Z0.25 (Rapid Move)
N20 X0.0 Y0.85 (Rapid Move)
N25 G01 Z-.125 F20. (Linear Move, Feedrate)
N30 X0.51 Y-0.68 (Linear Move)
N35 X-0.81 Y0.26 (Linear Move)
N40 X0.81 Y0.26 (Linear Move)
N45 X-0.51 Y-0.68 (Linear Move)
N50 X0.0 Y0.85 (Linear Move)
N55 G00 Z0.25 (Rapid Move)
N60 M05 (Spindle Off)
N65 M30 (End Program)
%
(Example Star Program Incremental Programming - Linear Moves Only)
%
N05 G91 G20 (Incremental & Inches)
N10 S6000 M03 (Spindle On, 6000 RPM)
N15 G00 Z0.25 (Rapid Move)
N20 X0.0 Y0.85 (Rapid Move)
N25 G01 Z-0.125 F20. (Linear Move, Feedrate)
N30 X0.51 Y-1.53 (Linear Move)
N35 X-1.02 Y0.94 (Linear Move)
N40 X1.62 Y0.0 (Linear Move)
N45 X-1.32 Y-0.94 (Linear Move)
N50 X0.68 Y1.53 (Linear Move)
N55 G00 Z0.25 (Rapid Move)
N60 M05 (Spindle Off)
N65 M30 (End Program)
%
G02/G03: Arc Motion
Programming an arc or circular move is slightly more complex than a linear move. First, the path taken by the arc must be specified as clockwise (G02) or counterclockwise (G03). Following the G command, there needs to be an ending point to the arc and a way to define the radius of the arc. To define the radius of the arc, the miscellaneous commands I and J must be introduced. The I-value is defined as the incremental move from the starting point of the arc to the arc center in the x-direction. Similarly, the J-value is defined as the incremental move from the starting point of the arc to the arc center in the y-direction. Starting at the origin, to draw a clockwise quarter- circle ending at X0.5 Y0.5:
N05 G02 X0.5 Y0.5 I0.5 J0.0
Example 2: Program with Arcs
(Example Program with Arcs)
%
O12345 (Program Name)
N05 G90 G20 (Absolute & Inches)
N10 S6000 M03 (Spindle On, 6000 RPM)
N15 G00 Z0.25 (Rapid Move)
N20 X-0.5 Y1.0 (Rapid Move)
N25 G01 Z-0.375 F20.0 (Tool Down, Linear Move, Feedrate)
N30 G02 X0.5 Y1.0 I0.5 J0.0 (CW Arc Move)
N35 G03 X1.0 Y0.5 I0.5 J0.0 (CCW Arc Move)
N40 G02 X1.0 Y-0.5 I0.0 J-0.5 (CW Arc Move)
N45 G03 X0.5 Y-1.0 I0.0 J-0.5 (CCW Arc Move)
N50 G02 X-0.5 Y-1.0 I-0.5 J0.0 (CW Arc Move)
N55 G03 X-1.0 Y-0.5 I-0.5 J0.0 (CCW Arc Move)
N60 G02 X-1.0 Y0.5 I0.0 J0.5 (CW Arc Move)
N65 G03 X-0.5 Y1.0 I0.0 J0.5 (CCW Arc Move)
N70 G00 Z0.25 (Tool Up, Rapid Move)
N75 M05 (Spindle Off)
N80 M30 (End Program)
%
Note: Once a G command is given the machine will stay in that mode, therefore the G command does not need to be repeated on every line. One G command is canceled by another G command. The feedrate will also remain the same until another feedrate (F) command is given. Programming languages that behave this way are called Modal languages.
Example 3: Fill in the Blanks
The following exercise is a use both Linear and Arc commands.
(Fill in the Blanks Program)
%
N05 G90 G___ (Absolute & Inches)
N10 S_____ M03 (Spindle On, 3000 RPM)
N15 G00 Z0.25 (Rapid Move)
N20 X0.4 Y1.0 (Rapid Move)
N25 G01 Z-0.125 F30.0 (Tool Down, Linear Move, Feedrate)
N30 G___ X_____ Y_____ (Point 1 to Point 2)
N35 _____ _______ ______ (Point 2 to Point 3)
N40 G___ X____Y____ I0.0 J-0.3 (Point 3 to Point 4, CW)
N45 G01 X0.4 Y1.0 (Point 4 to Point 5)
N50 _____ _______ ______ (Point 5 to Point 6)
N55 _____ _______ ______ (Point 6 to Point 7)
N60 ____ ______ ______ ______ ______ (Point 7 to Point 8, CCW)
N65 G__ Z0.25 (Tool Up, Rapid Move)
N75 M____ (Spindle Off)
N80 M____ (End Program)
%
STEM Activity: Program Your Initials
Given a 2”x 3”x 1” block, program your own initials onto the surface of the block. To keep coordinates simple assume the coordinate system origin is in the bottom –left corner of the block. Use the graph paper below to draw your design, and then create a program with G-code commands.
Download the Worksheet here.
You can turn any CNC mill into a pen plotter – all you need is a Sharpie marker and a tool holder or collet to hold it in the spindle. On a Tormach PCNC Mill, a 1/2″ TTS Set Screw Holder is just the right size for a Sharpie marker. Just remember to TURN THE SPINDLE OFF before running the program, or ink will be everywhere.
The Sharpie technique is a fantastic way to prove out simple engraving tool paths as well.
You can encourage advanced students to take things a step further by actually using an end mill to engrave into the surface of the part. With a flat pieces of stock, you’ll only need to program a cut depth of 0.005″ or so.
Special Thanks to Brian Johns from University of Iowa from whose work this lesson plan was derived from.