Connect with:
« Back to Blog home page

G-Code: The CNC Programming Language (2020 Update)



You know what they say about fundamentals: learn them first, and you’ll remember them forever. The same holds true for CNC programming. Even as new manufacturing technologies unfold, the fundamentals for how parts are machined with a CNC program will stay with you forever.


In this article, we’ll be talking about the core component of every CNC program – G-code.


G-Code at a Glance


Manufacturers all around the world use CNC programming to control a machine’s tools to produce parts. At the heart of this automated manufacturing process is a set of instructions that tells a CNC machine where – and how – to move. These instructions are called G-Code.



G-code was first established in the 1960s by the Electronics Industry Association (EIA). While the official language was documented as RS-274D, you’ll hear everyone refer to it as G-code. Why? Many of the words, or individual pieces of code, that make up this machine-based language start with the letter G.


While G-code is supposed to be a universal standard, you’ll find that many CNC machine companies have developed their own unique flavor. We’re all be eating ice cream at the end of the day, but a Haas might be strawberry and a Tormach chocolate. Because of this difference in G-code flavors, it’s imperative to understand how your own machine uses G-code.


Why the difference in G-code flavors? It really comes down to the capabilities of each machine. Take one machine that can process a coordinate system rotation based on probe inputs. You’ll need a set of G-code commands that can enable or disable this rotation. Another machine without this adjustment capability won’t require that G-code.


When in doubt, always refer to the documentation of your CNC machine as you work through the rest of this article. We’ll be walking through the basics, but you never know if your machine might have taken a slightly different path to the same end destination.


G-Code Blocks


The G-code standard was published back in the days when machines had small amounts of memory. Because of this memory limitation, G-code is an extremely compact and concise language that might almost seem archaic at first glance. Take, for example, this line of code:


G01 X1 Y1 F20 T01 M03 S500


In this single line, we’re giving the machine a series of instructions:


  • G01 – Perform a linear feed move
  • X1/Y1 – Move to these X and Y coordinates
  • F20 – Move at a feed rate of 20
  • T01 – Use Tool 1 to get the job done
  • M03 – Turn the spindle on
  • S500 – Set a spindle speed of 500


Multiple lines of G-code like these combine to form a complete CNC program. Your CNC machine will then read the code one line at a time from left to right and top to bottom, like reading a book. Each set of instructions is on a separate line or a block.


G-Code Programs


The goal of every G-code program is to produce parts in the safest and most efficient way possible. To achieve this, you’ll typically find G-code blocks arranged in a particular order like this:


  1. Start the CNC program
  2. Load the required tool
  3. Turn the spindle on
  4. Turn the coolant on
  5. Move to position above a part
  6. Start the machining process
  7. Turn the coolant off
  8. Turn the spindle off
  9. Move away from the part to a safe location
  10. End the CNC program


This flow is an extremely simple program using only one tool for one operation. In practice, you will typically rinse and repeat steps 2 through 9. For example, the G-code program below encompasses all of the code blocks above with repeating sections where needed:


Example of a CNC G-code program with explanations of each code block


Modals and Address Codes


Like other programming languages, G-code can repeat an action indefinitely until stopped. This looping process uses modal code, which performs an action until you either turn it off or modify it with another modal code. For example, M03 is a modal code that will run a spindle indefinitely until you tell it to stop with M05. Now, wait a second. That word (remember: a word is a little piece of code) didn’t start with a G, but it’s still G-code. Words that start with an M are machine codes, and they turn machine functions like coolant, spindle, and clamps on or off. I’ll list some common ones in the next section, but you can find a list of your machine’s M-codes in its documentation.


G-code also includes a complete list of address codes. You can think of these as the dictionary for G-code that defines particular behaviors. Address codes begin with the letter designation, like G, and then with a set of numbers. For example, X2 defines an X-coordinate address code, where 2 is the value on the X-axis to move the machine to.


The complete list of address codes includes:


Examples of address code parameters


There are also several special character codes that can be added to a G-code program. These are typically used to start a program, comment out text, or ignore characters and include:


  • % Begins or ends a CNC program
  • () Defines a comment written by a CNC operator, occasionally these must be in all caps
  • / Ignores all characters that come after the slash
  • ; Determines when a block of code ends, not shown in a text editor

G & M-Codes Explained

G & M-codes will make up the bulk of your CNC program. Codes that begin with G prepare your machine to perform a specific type of motion. The most common G codes that you’ll encounter time and time again in every CNC program include:


G0 – Rapid Move

This code tells a machine to move as fast as possible to a specified coordinate position. G0 will move the machine axis by axis, meaning that it will first move along both axes and finish the move on whichever axis is not in positions. You can see an example of this motion in the image below:

Diagram showing the motion of a G00 rapid move


G1 – Linear Move

This code tells a machine to move in a straight line to a coordinate position with a defined feed rate. For example, G1 X1 Y1 F32 will move the machine to coordinates X1, Y1, at a feed rate of 32.


G2, G3 – Clockwise Arc, Counter-clockwise Arc

These codes tell the machine to move in an arc to a coordinate destination. Two additional coordinates, I and J, define the arc’s center location as shown below:

Diagram showing CW and CCW arc interpolation using G02 and G03


G17, G18, G19 – Plane Designations

These codes define what plane an arc will be machined on. By default your CNC machine will use G17, which is the XY plane. The other two planes are shown in the image below:

Plane selection diagram showing XY, YZ, and ZX planes


G40, G41, G42 – Cutter Diameter Compensation

These codes define the cutter diameter compensation, or CDC, which allows a CNC machine to position its tool to the left or right of a defined path. A D-register stores the offset for each tool.


Diameter offset table with example values in tool 1.


G43 – Tool Length Compensation

This code defines the length of individual tools using a Z-axis height. This allows the CNC machine to understand where the tip of a tool is in relation to the piece it is working on. A register defines the tool length compensations, where H is the tool length offset and Z is the length of the tool.


Tool Length offset table with tool length value examples


G54 – Work Offset

This code is used to define a fixture offset which determines the distance from a machine’s internal coordinates to the datum on a workpiece. In the table below only G54 has an offset definition. However, you can program multiple offsets if a job requires machining multiple parts at once.


Work Offset table with values for a work offset included in the G54



M codes are machine codes that might differ between CNC machines. These codes control functions on your CNC machine like coolant and spindle directions. Some of the most common M-codes include:


Sample of common machine codes or m-codes


Canned Cycles


The last aspect of G-code to touch on is canned cycles. These are similar to methods or functions in computer programming. They allow you to perform a complicated action in only a few lines of code without having to type out all of the details.


Take for example the canned cycle below. Here we are telling the CNC tool to create a hole with a peck drill in only two lines of code on the left. This same action takes over 20 lines of regular G-code.


Canned Cycle vs Expanded G Code



Some common drill cycles includes:


G81 – Simple Drill Cycle

This cycle will make a hole by plunging to a specific Z-axis coordinate and then retracting. Programming this cycle requires a depth, feed rate, XY coordinates, and plane to drill on.


G83 – Peck Drill

This cycle is used for quickly drilling deep holes. A tool will first drill a defined distance and then retract, which clears any material out of the hole and allows coolant to flush away chips. The simplest implementation of this cycle requires an initial height, feed plane, peck increment, and depth.

Diagram showing peck drilling motion and parameters


G98 – Return to Initial Rapid Height

This cycle will retract a tool to a clearance plane between holes which helps to avoid clamps. Programming this cycle requires an initial height and feed plane to drill on.



Fundamentals First


Even if you never end up writing your own CNC program by hand, understanding the fundamentals of G-code will give you a leg up in your CNC career. The core behavior remains intact across manufacturers, even as the actual G-code differs slightly. At the end of the day, you’re combining coordinates, feed rates, and a set of definable actions to successfully move a tool and machine a part.


Some guides recommend memorizing the most common G and M codes. While this might work, practice makes perfect! Continuously use and refresh your G-code fundamentals throughout your career and they’ll stick with you. Here’s a quick quiz to see where you’re at.


Ready to put your CNC programming and G-code knowledge to work? Try Fusion 360 today!




« Back to Blog