G-Code: Definition, Function, and Different Types
Learn more about g-code and when it is used in CNC machining.
G-code (geometric code) is the most common CNC (computer numerical control) programming language used to drive computer-controlled manufacturing machines. It is not a single computer language but rather a closely related group of high-level (and theoretically human-readable) languages that provide motor and relay/switch control instructions to machine functions such as: axis-traverse motors, spindle speed, and physical positions relative to an absolute or incremental datum.
A CNC machining center is instructed by G-code commands to select from a variety of rotating cutters that remove material from highly specialized mechanical parts like crankshafts for internal combustion engines, for example. Additionally, G-code is the most common language used by additive manufacturing (3D printing) machines. In both cases, the process follows precise and repeatable paths that are each encoded as a series of motion instructions, positions, motor signals, and specialized machine commands. This article will further define G-code, outline its function, usage, and operation, and then detail some of the best G-code editing programs.
G-code is a programming language constructed from a series of precise commands intended to carry out exact functions. The machine's controller will convert the G-code commands into tailored motion instructions for axis, spindle, and coolant actions. Below are a few examples of G-code instructions:
- G21: Sets the measurement base to metric, mm
- G0 X34: The tool moves +34 mm along the X-axis from the previous position at the maximum traversing speed.
- G1 X34 F150: The tool moves +34 mm along the X-axis at a specified speed (150 mm/s).
The code also commands such aspects as: geometric positions in which the cutter is to be placed, the required spindle speeds and tool changes, and the speed and route to be used on the approach to the part. Many G commands are modal, meaning that the computer will consider them binding until they’re superseded by another modal command of the same type.
G-code is intended to tell machines what to do or how to move. It codifies a 3D reasoning process, guiding cutters, print heads, etc. through a path that positions them for the intended task and executes the command. Along the way, G-code specifies spindle speeds and coolant flows and controls any necessary tool changes. The language allows CNC programmers (or other computer-based machiners) to write cut-and-move sequences that create detailed procedures that their machines can follow without supervision.
G-codes are most often used in the programming of complex computer-aided manufacturing machines such as: CNC 3-axis mills, CNC 4- or 5-axis machining centers, CNC lathes, CNC jig borers and drills, and electrical discharge machining or other wire-cutting machining centers. It is also very common in 3D printing operations of all types.
The first G-code was created in 1958 at the MIT Servomechanisms Laboratory. Subsequent versions have been developed by various machine and controller manufacturers, generally using variations on the G-code standard, each tailored to the developers’ own equipment. The standard version was adopted by the Electronic Industries Alliance shortly after its development at MIT in 1980 and eventually became the current, standardized RS-274-D language.
G-code specifies a series of points and line-by-line instructions that are programmed into a CNC machine. It operates in an orthogonal (or occasionally polar) coordinate environment so that a programmer can write traversing movements, spindle speeds, cutting motions, and coolant commands. The commands are converted, within the machine controller, to control signals for parts such as motors and pumps. An instruction to, for example, move along a clockwise arc between two coordinates at a set speed must be converted to electrical signals that drive motors. A G-code, therefore, does not act as a direct machine control signal but instead provides instructions to a controller integrated into the machine. Much like machine code programming, high-level G-code commands are converted into a form of machine language for execution.
G-code can control machine tools by sending instructions to the CNC machine controller on a line-by-line basis. The controller is integrated into the machine functions which control the motors and drives that move the machine parts such as: drive motors, spindles, and coolant pumps. The machine control module is responsible for executing the programmed motions according to its G-code instructions.
There are several types of G-codes that are divided into functional groups or subclasses. Some commands are modal, meaning they are valid until superseded by another modal command. Others are non-modal, so they specify an action that happens only once. The basic categories are as follows:
- Rapid positioning: G00
- Interpolation types: G01, G02, G03, G06
- Selection of the reference plane for follow-on commands: G17, G18, G19
- Tool offset: G40, G41, G42, G43, G44
- Zero offsets: G53 - G59
- Uni-directional position: G60
- Exact stop mode: G61
- Description of the process: G80 - G89
- Dimensions: G90, G91
- Feed rates: G93, G94, G95
- Spindle rotation speed: G96, G97
- Wait time: G04
- Increment or decrement speed: G08, G09
- Thread (inner and outer) specifications: G63
- Set Work Coordinate Systems Shift Value: G92
The best G-Code editor depends on the user’s needs. There are two very different audiences for the language: regular programmers and light users. Regular CNC tool programmers — people who can essentially think in G-code — generally need a zero-frills text editor type of interface, a visualization tool, and a few bulk action shortcuts. Light users like beginners and home users, by contrast, may need more hand-holding. Below are listed some of the best G-code editors for users on both ends of the spectrum:
- NCPlot: NCPlot is a minimalist text editor environment that includes great tools for editing G-code. It also has a neat toolkit to generate G-code from text and has time-estimation tools.
- TextPad: TextPad is a low-cost but capable software with many of the tools needed for more expensive applications. It is basically a G-code-empowered version of Windows Notepad, but with some important differences including the ability to directly import and merge groups of G-code into a single file and the capability of coloring groups of code.
- G-Wizard Editor: G-Wizard Editor uses a very approachable method based on visual editing. This software, from CNCCookbook, is a collection of powerful tools and advisory information. The basic method is called “conversational G-code” and operates using high-level commands like "face mill the part," automatically generating a block of G-code for this action. It also has an error-checking algorithm that guards against faults such as cutters crashing into the part during traverse stages.
G-code is highly structured and easy to read at a high level. Even without knowing all the commands, a line of code can be understood in general terms. Each line of code is structured simply and consistently. For example:
G1 (move at specified speed) X60 Y30 Z80 (to these coordinates) F200 (at a feed rate of 200 mm/s)
G0 (move as fast as possible) X0 Y0 Z0 (to the origin)
With a cheat sheet of G-codes and an understanding of the workspace’s coordinate system, the code becomes easy to read.
Yes, G-code is a programming language in a very raw and real way. Programming generally consists of highly structured sequential instructions, written to achieve a virtual or real-world outcome by instructing a processor to take actions and integrate input and output information.
Yes, G-code is used on all CNC machines. It is a tool for translating design intent into cutter action.
In the simplest terms, G-code and M-code are “off the shelf” and “tailor-made” commands, respectively, that drive machine functions. Generally, G-codes describe positions and M-codes direct the machine's actions. Machining centers operate on 3, 4, or 5 axes and the motion is all driven by motors. Each machine has a spindle that holds the cutting tool. The computer must have an internal map of obstacles to be avoided when moving the cutter around the build space. It must also have controls with start and stop commands. The G-code is a nearly universal toolkit that can drive all these functions and more.
No two manufacturers use the same coolant pump systems, Z and diameter offset tools, vibration/chatter compensation methods, condition monitoring methods, or automated texturing tools. Functionality that is unique to an individual machine manufacturer requires proprietary commands. Collectively, these commands are called M-code.
This article presented g-code, explained what it is, and discussed when this programming language is used. To learn more about g-code, contact a Xometry representative.
Xometry provides a wide range of manufacturing capabilities, including CNC machining and other value-added services for all of your prototyping and production needs. Visit our website to learn more or to request a free, no-obligation quote.
The content appearing on this webpage is for informational purposes only. Xometry makes no representation or warranty of any kind, be it expressed or implied, as to the accuracy, completeness, or validity of the information. Any performance parameters, geometric tolerances, specific design features, quality and types of materials, or processes should not be inferred to represent what will be delivered by third-party suppliers or manufacturers through Xometry’s network. Buyers seeking quotes for parts are responsible for defining the specific requirements for those parts. Please refer to our terms and conditions for more information.