What are G-Codes in CNC? Programming and Commands


Computer Numeric Control (CNC) machines must be able to comprehend our design needs. A G-Code programming language helps to convey these designs in a machine's understandable language. Although modern CAD and CAM software are able to automatically translate instructions into G-Codes. But the knowledge of G-Codes is still relevant for CNC operators. Let's see why?
What is G-code?
G-Code basically bridges the gap between the CAD designer and the CNC machine. It facilitates the CNC machining process by programming the Computer Aided Design (CAD) into a set of instructions upon which the CNC machines manufacture parts automatically.
Definition of G-code
G-Codes also stands for “Geometric” Code. These are a set of commands to define, machining parameters related to the movement of the tool in the CNC machining process.
What are G codes Used for?
G Codes uses a special syntax for operators like G00 or G01 followed by some metrics to give instructions to the CNC in a sequential manner. For instance, a simple code might give instruction like: move 2 units in the x-axis direction at a depth of 1 unit in the z-axis direction and then stop.
The History of G-code
Post World War II era usher in the development of many advanced technologies. Development of CNC machines guides by G codes and M codes is one of them. Although, now-a-days it has become very easy but the earliest versions were not very user friendly. Let's explore more!
Origins and Development
A need was felt for automation in the lathe and milling machines to maintain consistency in quality and to boost up speed of production. It led to the development of Numeric Control (NC). It used card or tape punch system. Later it evolved into Computer Numeric Control (CNC)
Who Invented G-code and When?
In the 1950s engineers from MIT developed the first G-Codes. But these were not standardized and were not widely adopted. In less than a decade, in the 1960s G-Codes were standardized. We use pretty much the same version till now.
The Importance of G-code in CNC Programming
G- code is the most essential component in the CNC machining process. Even if you have a very perfect computer aided design, its almost useless without the G-Codes. The CNC would have a clue about what to do with this CAD.
Why G-code is Essential for Precision Manufacturing
Modern precision manufacturing is derived from CNC to a greater extent. CNC utilizes the G Codes and M Codes to act on specific commands.
G-code vs M-code: Key Differences
Both, G Code and M Code are the programming languages of CNC. There is a bit difference between the two. G Codes or Geometric Codes define instructions related to the movement of the tool. M Codes or Miscellaneous Codes define other parameters like pressure of the coolant for instance.
How Does G-code Work?
There are a set of operators like G00 or G03 which specify the task that the machine must do. As an example, if an operator wants the tool to come to the origin, he / she might use the G00 positioning operator of the G-Code.
The G-code Process at the Machine Level
In contrast to the early days when G-Codes were written by the CNC programmer, now a days the CAM software does it all. It gives the whole program in the form of M Codes and G Codes. The CNC machine acts on the instructions in a sequential way.
The Operator's Role in G-code Programming
Mostly, a G Code doesn't require any editing. At times, certain amendments might be required for customization or prototyping. An operator might then edit the G-Code written by the CAM software using an editor software.
Structure of G-code Commands
Now G-codes are almost universally standardized for common command operators. Though there are certain variations from manufacturer to manufacturer. There are printed data tables and also online ones which define the function of each command operator. Users can easily get expertise over time alongwith time.
Basic Syntax and Formatting
It starts with the letter G which represents the command type. It is followed by numeric digits like ‘00' or ‘01'. Then comes the parameter name and parameter value.
Key Elements of a G-code Command
As an example, operators like G01 for linear interpolation are preceded with a value. G01 defines that the machine tool should move and Z05 for instance defines that it should move 5 units along the z axis.
Types of G-codes and Their Functions
Knowledge of G-Codes and their functions helps the CNC operators a lot. Specific functions are being discussed in the tables below.
Positioning Commands (G00, G01, G02, G03)
G-Code |
Function |
G00 |
Rapid positioning |
G01 |
Linear interpolation |
G02 |
Circular interpolation Clockwise |
G03 |
Circular interpolation Anti Clockwise |
G04 |
(pause) |
G17-G19 |
Plane selection (XY, XZ, YZ) |
G53 |
Machine coordinates |
G54-G59 |
Work Offsets |
G90 |
Absolute positioning |
G91 |
Incremental positioning |
Speed and Feed Rate Commands (G94, G95)
G-Code |
Function |
G96 |
Constant surface speed |
G97 |
Cancel constant speed |
G98 |
Feed per minunte |
G99 |
Feed per revolution |
F |
Set feed rate |
S |
Set spindle speed |
Machining Operation Commands (G81, G82, G83)
G-Code |
Function |
G81 |
Simple drilling |
G82 |
Drilling with dwell |
G83 |
Peck drilling |
G84 |
Tapping cycle |
G85 |
Boring cycle |
G86 |
Boring with spindle stop |
G87 |
Back boring |
G88 |
Boring with dwell |
G89 |
Boring with slow feed exit |
Miscellaneous Commands (G4, G20, G21)
G-Code |
Function |
G04 |
Dwell |
G20 |
Inch unit selection |
G21 |
Metric unit selection |
G Codes Examples
As an example if a tool is to move 50 units in the X direction with a feed rate of 100 units per minute, “G01 X50 F100” block would be used.
General G-code List for CNC Milling
The below chart give a general G-Codes list for CNC milling.
G-Code |
Function |
G00 |
Rapid Positioning |
G01 |
Linear Interpolation |
G02 |
Circular Interpolation (CW) |
G03 |
Circular Interpolation (CCW) |
G20 |
Inch Mode |
G21 |
Metric Mode |
G28 |
Return to Home |
G40 |
Cutter Comp. Off |
G41 |
Cutter Comp. Left |
G42 |
Cutter Comp. Right |
G43 |
Tool Length Comp. |
G54 |
Work Offset |
G90 |
Absolute Positioning |
G91 |
Incremental Positioning |
CNC Machines and G-code Usage
The relationship between the CNC machines and the G codes is analogous to a car and a driver. CNC machine is like a car and G Code is like its driver. It's the G code that guides the CNC what to do and when to do.
CNC Milling Machines and Their G-codes
CNC Milling machines have a stationary workpiece and moving tool. Thus G-Codes guide the movement of the tool relative to the workpiece.
CNC Lathe and Turning Machines
CNC Turning machines have a moving workpiece and a stationary tool.
Other Machines Using G-code (Grinders, Drills, etc.)
Other CNC machines like grinders and drill have their relevant operators in the G-Codes list that define dwell time, hole depth etc.
Who Needs to Learn G-code?
G Codes are universally standardized now and modern tools like CAD and CAM can automatically give G- codes to the CNC machines. Still, G- Codes learning is relevant for customization task. So, a factory must have at least 1 or 2 people who can edit G-Codes.
CNC Operators and Programmers
Normal operators can handle CNC machines even without learning G-Codes but still it a plus to have knowledge of G-Codes. Programmer need to understand G- codes for editing purposes.
Engineers and Technicians in CNC Manufacturing
Engineers and technicians focus more on the R&D, designing and implementation of the manufacturing plan. Normally, design engineers don't necessarily have knowledge of G-Godes. CNC specialist mechanical and/or mechatronics engineers have knowledge of G- Codes.
How to Read G-code Commands and Interpret Programs
G-Codes are instructions given to a machine in a sequential manner. It gives complete instructions. Carefully note the alphabetic letters preceeding the number for example G01 would define a machine movement function while F100 would define the feed rate.
Understanding G-code Lines and Blocks
G-Codes consists of lines. As an example G01 X50Y50 F100 would mean that the tool should linearly interpolate to the (50,50) coordinates with a feed rate of 100 units/ minute.
Interpreting the Movement and Tooling Information
For the movement of tool the most relevant operators are G01, G02, G03 and F and S. For more details see the table given above.
The Role of Subprograms and Macros in CNC Programming
Subprograms and macros greatly enhance the operational efficiency by reducing repetition of redundant data. Macros help in automation.
How Subprograms Enhance Efficiency
Subprograms store the block codes thus reducing the requirement for storing information.
The Power of Macros for Automation
Macros use logical operators and M98 and M99 M codes for doing certain tasks automatically.
Avoiding Common G-code Errors in CNC Machining
Error |
Cause |
Solution |
Syntax Error |
Incorrect format or missing values |
Check and correct command structure |
Missing Tool Call |
Tool not selected before operation |
Use T and M06 to select tools |
Incorrect Feed/Speed |
Wrong F or S values |
Adjust settings based on material and tool |
Work Offset Error |
Missing or incorrect G54 to G59 |
Set the correct work coordinates |
Spindle Not Starting |
Missing M03 or M04 command |
Add spindle start command before cutting |
G-code Simulators and Editors
Simulators are need to check whether CNC programming is good to go. It helps to avoid any potential loss due to errors in G-Codes. Editors help to manually edit the G codes for customization.
Top G-code Simulators for CNC Programming
NC Viewer, CIMCO Edit and Fusion 360 are regarded as the top three G Code simulators.
Best G-code Editors and Their Features
Editor |
Features |
CIMCO Edit |
Syntax highlighting, simulation and error checking |
NC Corrector |
G-code editing accompanying 2D/3D simulation |
Fusion 360 |
CAD and CAM integration |
How to Write G Code
In contrast to the early years when G codes were manually written, modern CAM software can write G code on its own. Anyways, if need arises, the G-Codes operators can be checked from the CNC supplier's manual or even from a standardized version. As an example, G01, X50 would mean a liner interpolation of 50 units in the x-direction. If we change it to G01 X60 then it would mean linear interpolation of 60 units in the X-direction. Similarly, G Codes can be written using the standardized syntax. Simulators can verify the accuracy of G Codes.
Frequently Asked Questions (FAQs) About G-codes
What Are the Most Common G-codes?
Mose common G-Codes are G00 for rapid positioning, G01 for liner interpolation and G02/G03 for circular movements.
Is G-code Hard to Learn?
Not necessarily. It depends on your skillset. If you are fond for computers and programming you can learn easily.
Are G-codes Universal Across All CNC Machines?
G-codes are almost universal for common operators like G00, G01 etc. But its best to check the supplier's manual. Some suppliers like FANUC, Siemens, and Haas have variation in the syntax.