G code for CNC: The Complete Guide

All manufacturers employ CNC programming to control machine tooling to produce parts and components. At the core of this automatic production operation, there is a series of commands guiding the movement of the CNC machine. The referred to commands are known as geometric code (G code).

What is G Code for CNC?

G Code describes special programming language applied in CNC machines to guide their motion and additional functions.

It is not an exclusive computer language but instead a comparably set of advanced languages that offer motor and switch/relay control commands to machine functions. Some of the commands include, spindle speed, axis-traverse motors, and physical orientations based on an incremental or absolute datum.

Gcode for CNC is the most popular programming language for driving computer-controlled manufacturing equipment.

The language can be relatively complex sometimes and differ from one machine to another. Nonetheless, the fundamentals are much uncomplicated than it initially seems and the better part observe an industry standard.

Differences betweenM Code and G Code in CNC

CNC Machine

CNC Machine

M and G code used in CNC perform distinct, yet contrasting purposes in running a CNC machine. There exist 3 essential differences between M and G code programming language:

  1. Gcode guides the mobility and running of a CNC machinery. Conversely, M code controls functions not related to motions.
  2. Gcode sets the CNC machine in motion, while M code triggers the programmable logic controller (PLC) of the equipment.
  3. Gcode commands normally range in CNC devices. On the other hand, majority of M code continue to be similar.

Function of G Code for CNC

Sample G-code Command

Sample G-code Command

The basic purpose of G code is to direct the motion and running of a CNC machine. It systemizes a 3D reasoning procedure, directing print heads, cutters, among other componentsthrough a trackway that sets them for the purposed operation and implements the command.

During the process, G code sets out coolant flows and spindle speeds and manages any required tool modifications. It enables writing of cut-and-move patterns that form detailed processes that CNC machines can stick to without supervision.

How G Code for CNC Works

The operation procedure of G code for CNC is a synchronized operation between the code programming by the operator and the machine functions.

All CNC machineries incorporate a microcontroller capable of decoding the G-code.Most machines operate on standardized G code.

However, certain machines include several axes or advanced properties that standard G code commands cannot control. Therefore, additional commands will be necessary to guide the extra functions.

The internal control system reads and decodes the commands based on the microcontroller signals. It then gives motion directions to the many CNC machine functions.

Structure of G Code for CNC Machine

G code for CNC is a blend of a letter and a number. Spacing between the letter and number differs depending on the specific CNC equipment.

Each G-code line, also referred to as G code block, can have several groups of instructions.The machine reads and executes these commands in a specific pattern from top to bottom and left to right.

Here are the common letters applied in G code for CNC machine programming language:

  • N: represents the line number
  • G: Indicates when the tooling moves and stops
  • X, Y, Z: Represents the tooling position in 3D (X-horizontal, Y-Vertical, and Z-depth).
  • F: Indicates the feeding rate of CNC machine
  • S: Determines the spindle speed
  • T: Determines the tooling to use
  • M: Instructs on miscellaneous machine functions or machine functions
  • I and J: Represents the incremental arc centers madeby the machine
  • R: Indicate the arc radius
  • A: Guide the tooling about the X-axis
  • B: Represents rotational value about the Y-axis
  • C: Represents positional value about Z-axis
  • D: Determines the extent the CNC machine offsets the tooling diameter.
  • L: Indicates repeating operations and the number of times they should loop
  • P: Commands G code for CNC to jump in delayed or in time

Other structures of G code for CNC are dependent on the abilities of the machine. The code might add extra direction coordinates for 4- or 5-axis equipment.

The Most Common G Code

You can categorize G code for CNC machine into various groups depending on its functions. The categories include:


G Code Instruction
Positioning Commands








Fast CNC Tooling Positioning

Linear Interpolation

Clockwise Arc Interpolation (Helical or Circular Interpolation)

Anti-clockwise Interpolation (Helical or Circular Interpolation)

Employ absolute coordinates

Speed Commands







Decremental or incremental speed

Linear feed value selection

Continual surface speed

Continual spindle speed

Machining Function Commands







Basic drilling

Basic drilling with dwell

In depth hole drilling





Offset Commands

G40 – G44




Tooling Offset Values

Zero Offset Value


Miscellaneous Commands






Wait time

Exact Stop Mode

Process Description

How to Generate G Code

Here, we will highlight the 4 essential procedures of G code generation applying CAD/CAM software:

Step One: Develop CAD Prototype

Developing the CAD prototype of the item to be manufactured is the initial step. You perform this employing a CAD software you deem fitting to your requirements. An alternative is scanning the physical product or importing the available CAD prototypes. Nevertheless, you should make sure that the prototype captures the exact shape, size, and characteristics of the end product.

Step Two: Outline CAM Specifications

Outlining the CAM specifications is essential in determining the way in which the CNC system will carry out the CAD prototype processing.  This stage can be performed utilizing stand-alone CAM software have the capacity to import CAD prototype or CAM software embedded with CAD software.

Material, CNC machine type, plasma cutting tool, tool trackway, feeding rate, spindle speed and machining operations are the vital CAM specifications to factor in. Moreover, simulation of the machining procedure is instrumental to confirm collisions or errors.

Step Three: Generate G-code for CNC

The CAM specifications are instrumental in the G code generation. It is possible to perform this through CAM software or manually.

Remember to ensure that the developed G code for CNC is in sync with the device and conforms to the benchmark configuration and syntax.For output customization or performance optimization, you can carry out G-code editing.

Step Four: Feed the G code into the Machine

The last phase is feeding and executing the developed G code in the CNC machinery. The feeding can be done through a wireless device, network connection, or USB drive. You should make sure that the CNC machine has the right cutting tools, coordinates, and material.

Moreover, it is advisable to test the G code on a dry run or sample product before running it. This helps in guaranteeing the safety and quality of the end product.

Interpreting G Code Commands

Reading G code for CNC machine commands is relatively simple with a bit of practice. Let’s look at the basic steps that will help you interpret G code commands:

  1. Begin by concentrating on the alphabetical letter of the G code command.
  2. Letter G associates with the CNC machine functioning. The numerical after the alphabet represents the machine operation or process that will be affected.
  3. Alphabets X, Y, and Z show the position with the coordinate system. The numerical following these letters give the exact position in every axes.
  4. Alphabets A, B, and C represents angular position same to X, Y, and Z. The numerical following the letters represents the angular rotation value in a specific direction.
  5. Alphabets such as F and S indicate the spindle speed and feed rate. The subsequent numerical after the letters representsthe speed of related values. For instance, F200 commands the CNC machine to employ 200 feed rates.
  6. You can add comments in the G code for CNC by including a semicolon (;) at the close of the block. Any written comment in a block subsequent to the semicolon will not impact the CNC machine operation.

Software for Creating G Code

G-code Command Sample

G-code Command Sample

Typically, the predecessor to G code for CNC machine is a CAD file. It forms a graphical model in 2D or 3D of the needed product. Afterwards, there are advanced software that are able to automatically convert CAD designs to optimal G code commands.

CAM software is the common and powerful tool for generating G code files for use in CNC machines. They allow for G code simulation and you can connect the program directly with the equipment.

The advantage of this procedure is that the computer system can perform automated calculations to determine the best tooling route and additional settings. The developed G code programming is capable of taking care of features like CNC tooling offsets.

G code editing software come in if there is need to make any changes in the G code for CNC. These G code editors are necessary when you want to perform CAD design customizations.

The created G code program is not standard for all CNC machines because of variations in equipment features and format variance. Consequently, the programming instructions undergo additional software referred to as post-processing.

This software systemize the generated G code precisely based on how the machine is intended to read. Doing this does away with any chance of bugs because of difference in several equipment controller software. It is this version of G code that you feed and activate in the CNC machine.

Machine types that Apply G Code for CNC

There are quite a number of CNC machines that use apply G code programming. The common machines include ethe following:

· CNC Milling Machine

CNC milling machinery employs a rotary cutting tooling on a static workpiece. It features a cutting tool that comes in varying shapes and designs. This results in a number of differing kinds of milling procedures.

· CNC Turning Machine

This type of CNC machine features a static cutting tooling on a spinning workpiece. It is instrumental in forming symmetrical marks on conical and cylindrical surfaces.

The equipment incorporates a helical tooling path about the workpiece. CNC turning machine helps in exterior surfaces machining during the shaping process. CNC lathes apply the working concept of turning machine.

· CNC Grinding Machine

The primary function of CNC grinding machine is to ensure fine surfaces machining. It removes negligible material from the workpiece to give it a smooth surface.

It comes as an auxiliary equipment subsequent to others like turning and milling machines. Moreover, CNC grinding machinery is also capable of eliminating the burrs formed following welding and related joining procedures.

· CNC Drilling Machine

Most drilling is carried out using CNC drilling machine. It bore holes into workpieces through a drill bit. The drilled hole may be for secondary assembly, fixing screws or aesthetics.

Generally, drilling presses come after other machining operations. The hole diameter is often limited. Thus, CNC drilling machine is applied when you require large hole diameter.

· CNC Routing Machine

This is a type of CNC machine that aids in cutting of various materials. It typically blends a handheld router with a CNC system.

The router has the capacity to eliminate a very regulated quantity of material from a workpiece surface. Because of this, it can make complex carvings.

· CNC Laser Cutting Machine

In this CNC machine type, an extremely focused laser beam produces heat that melts and cuts the material being machined. An optics system in the CNC laser cutting machine makes to have a very high-intensity heat.

Nonetheless, the equipment is limited with regard to the materials you can cut. Machining any delicate materials such as plastics produce hazardous gases that can destroy the optical system.

· CNC Water Jet Cutting Machine

This is a creative method of cutting workpieces by using high-pressure water force to cut across any object. The water stream thickness is lower than that of human hair.

G code for CNC machine programming makes it possible to shift the cutting head about. You can use the machine in any application since they can bore through materials of great thickness.

At TSINFA, we will help you get the right CNC machine for all your fabrications needs. Our team will help you in the entire programming process.

More resources:

What is M-code – Source: TSINFA

Post Processor in CNC Machining – Source: TSINFA

G-code – Source: IQS DIRECTORY

CNC Machine Types – Source: TSINFA

CNC Machine Tools – Source: TSINFA