All CNC machines rely on G-code for their operation. Contrary to what some people believe, G-code for CNC machine is not a difficult concept. It is very easy to understand the basics of G-code, how it works, and how to create a G-code for your next project. Read on to find out everything there is to know about G-code for CNC machines.
What is G-Code?
G-code is a type of CNC programming language used to control the functions of a CNC machine that relate to the movement of the cutting tool.
It stands for Geometric Code. G-code consists of instructions that the microcontroller in the CNC machine can read and interpret. The instructions are then sent to the appropriate part of the machine. A single line of G-code can contain several instructions. Each line of G-code is defined as a block. It is a very simple programming language that does not require complex logic or mathematical skills.
G-code programming works in conjunction with M-code. M-code stands for machine code and is considered by some to stand for miscellaneous codes. The M code controls the various functions of the CNC machine that are not directly related to motion. These functions include instructions such as the machine loading the program, pauses in the program, and coolant flow.
Modal vs. Non-Modal G-Codes
There are two types of G-codes used in CNC machining-modal and non-modal. A modal G-code remains in effect throughout the program until another G-code changes it. In contrast, non-modal G-codes only remain in effect for the block in which they are used.
Do all CNC machines use G-code?
Yes, all CNC machines use G-code. The G-code implementation is built into the microcontroller of every type of CNC machine.
However, manufacturers often change the format of the G-code in minor ways to make it work on their particular machines.
For example, some machines may have an extra ‘0’ in front of the G-code numbers. For example, while some machines have the rapid motion G-code as G00, others may have it as G0.
What is the role of G-code in CNC machining?
G-codes play a critical role in CNC programming. Automation in CNC machining comes with the interpretation of G-codes. CNC machines cannot understand conversational language. They operate on a specific set of machine language commands. Programmers compile these commands into a G-code file to instruct the CNC machines how to operate.
The CNC programming microcontroller is pre-programmed with the meaning of each G-code command. Therefore, when the microcontroller reads a particular command, it immediately knows what to do. If a G-code command is outside the dictionary of the CNC microcontroller, it will not work.
G-code commands work in conjunction with their counterparts – M-codes. G-codes control the movement of the CNC machine tool, while M-codes control the operation of the CNC machine, such as coolant flow or tool change. The M-code and G-code commands are used together to create a complete CNC programming file.
How many G-codes are there on a CNC machine?
There are over one hundred G-code commands for CNC programming. Most of the G-codes are common to all CNC programming operations. However, some G-codes are specific to the type of operation, such as milling, turning, drilling, etc.
There may be variations in the G-code list between different CNC manufacturers.
Not every machine supports every G-code. In addition, machines with unique features or multi-axis machining capabilities may have additional G-codes. The manufacturer may provide instructions on G-codes for CNC programming in the reference book that comes with the machine.
What are the most common G-code commands used in CNC machines?
There are over one hundred G-codes used in the CNC programming process. It can be difficult to remember all of them and what they mean.
Below is a G-Code list of the most commonly used commands in the CNC programming process.
You can use this list as a reference when writing any CNC programming file. It is useful to memorize important commands such as G00-G03 because they are used in every CNC programming project.
CNC Movement and Travel
The following codes are used to control the movement and tool path in CNC programming:
G00: Rapid Move of the Machine Tool
The rapid motion command moves the tool from one point to another without cutting the material. The movements are made at the highest possible speed. Therefore, the rapid traverse commands do not require a feedrate. It requires position coordinates in the X, Y and Z axes.
G01: Machine Tool Linear Interpolation
Linear interpolation moves the tool in a straight line from one point to another. The speed is according to a feedrate specified by ‘F’ in the G01 command block.
G02: Clockwise Arc Circular Interpolation
Instead of a straight line, G02 commands the cutting tool to cut in a clockwise arc. It requires a feedrate specified by the value ‘F’. It requires the center point (I, J, K) or radius (R) of the arc to be specified.
G03: Counterclockwise circular arc interpolation
Same as G02, but instead of cutting an arc clockwise, it cuts an arc counterclockwise.
G04: Dwell
Dwell indicates a pause in the program. It stops the machine movements, but the auxiliary functions remain active. For example, the spindle continues to move while the program is in Dwell mode. The duration of the dwell is indicated by the pause time ‘P’. The machine reads the P value in seconds.
G09: Exact stop
The Exact Stop G-code is used when a sharp corner is required. Conventional machining produces rounded corners due to the inertia of the cutting tool. G09 eliminates this problem by temporarily stopping the tool at the corner and then moving it again, resulting in perfectly sharp corners.
Plane selection
G-code programs specify the two-dimensional plane in the X, Y, Z axis Cartesian coordinate system. These commands are
G17 – XY Plane Select
G18 – XZ Plane Select
G19 – YZ Plane Select
Dimensioning
G-Code programs for dimensions specify which units of measurement are selected. These commands are
G20: Change unit of measurement to inch
G21: Change unit of measure to millimeters
Compensation Codes
Mill compensation codes take into account parameters such as tool length and tool radius. Using these commands can increase the accuracy of the entire CNC operation.
They are also called tool offsets. These commands are
G40 – Disable tool compensation
G41 – Tool compensation left
G42 – Right tool offset
G43 – Enable tool length compensation
G40 – Cancel tool length compensation
Work Offsets
Work offsets ensure that the workpiece is at the true zero position. The commands for work offset values are
G54 – Work Offset 1
G55 – Work Offset 2
G56 – Work Offset 3
G57 – Work Offset 4
G58 – Work Offset 5
G59 – Work offset 6
Canned Cycles
A canned cycle in CNC is a repetition of a specific machine operation such as drilling, reaming, tapping, boring, etc. Some of the common canned cycle G-code programs are
G73 – High speed peck drilling canned cycle. Drill holes while breaking chips
G74 – Peck drilling canned cycles generally used for face grooving. Use for tapping only.
G75 – High speed grooving canned cycle for CNC lathes.
G76 – Fine boring canned cycle and threading cycle
G81 – Standard drilling canned cycles
G82 – Standard drilling with dwell at the bottom of the hole
G83 – Deep hole peck cycle with full hole retraction
G84 – Rigid tapping cycle for tapping threads in pre-drilled holes
G85 – Reaming or boring cycle
G86 – Drill and stop canned cycle; spindle stops when tool reaches bottom of hole
G87 – Drilling cycle with a special tool to expand the diameter of the hole
G88 – Boring cycle with P instruction; P instructs the number of seconds to dwell.
G89 – Return drilling cycle with dwell
Abort codes
G50: Scaling off; on some machines it can be used to program the absolute zero center or to set the spindle speed limit.
G80: Cancel all active canned cycles
Positioning Modes
The positioning mode refers to how the CNC machine reads the positioning commands. The G-code program for different positioning modes are
G90 – Use absolute positioning mode
G91 – Use incremental mode for positioning
Speeds and Feeds
The speed and feed mode refers to how the machine interprets the units of value. These commands are
G94 – Feed per minute mode
G95 – Feed per revolution mode
G96 – Constant surface speed
G97 – Constant Spindle Speed
Plane Returns
Plane return commands consider the position of the cutting tool in different planes. Common plane return commands are
G98 – Return to Starting Plane
G99 – Return to Rapid Plane
Less Common G-Codes
Some of the G codes are not as common as those listed above. However, you may need them from time to time for specific program requirements. Here is a list of some of the less common G codes:
G10 – Programmed offset input
G22 – Stored stroke limit
G23 – Stored Stroke Limit Cancel
G27 – Zero return check
G28 – Zero Return
G29 – Return from home position
G30 – Second Position Zero Return
G31 – Skip Function
G44 – Negative tool length compensation
G45 – Single Offset Increase
G46 – Single offset decrease
G47 – Double Offset Increase
G48 – Double offset decrease
G51 – Scaling
G52 – Temporarily shift program zero
G53 – Return to machine zero
G60 – Move in one direction
G61 – Exact Stop Check (Modal)
G64 – Normal cutting mode
G65 – Custom Macro Call
G66 – Call Custom Macro Modal
G67 – Cancel Custom Macro Modal Call
G68 – Coordinate Rotation Mode
G69 – Cancel Coordinate Rotation Mode
G92 – Programming a Work Offset
Are there any safety considerations when programming G-code for CNC machines?
Yes, there are some safety considerations when programming G-code for a CNC machine. A CNC machine is capable of cutting extremely hard materials.
Therefore, an incorrect G-code can be a safety hazard to the machine, the operator, and the work area. The G-code should take into account factors such as work offsets and tool length offsets to ensure that tool breakage does not occur.
Tool breakage is a common accident caused by tool collisions. Running simulations in CAD and CAM software beforehand can eliminate the possibility of errors and mistakes in the G-code.
What are the other codes used in CNC machining?
M code is the other important code used in CNC programming besides G code. M codes control the various CNC machine functions that are not related to motion.
M codes are specific to the particular CNC machine control and the functions it supports. M Codes and G Codes are used together for complete CNC programming. Some common M code commands are
M00- Mandatory Program Stop
M01- Optional program stop
M02- Program end point
M30- Program stop and rewind
M06- Tool change
Conclusion
Understanding G-code is essential to learning CNC programming and producing any part through an automated machining process. The good thing is that learning how to read G code takes very little time.
There are many different G codes for different instructions. You can check the reference list above to use the G-codes when working on CNC programming projects.
An important thing to remember is that the G codes may vary slightly depending on the particular manufacturing model you are using. Therefore, refer to your machine operator’s manual to check for any differences in the CNC programming design of your equipment.
Disclaimer of Warranty
The contents of this website are provided for informational purposes only. MY Prototyping makes no representations or warranties of any kind, express 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 supplied by third party suppliers or manufacturers through MY Prototyping’s network. Buyers seeking instant quotes for parts are responsible for defining the specific requirements for those parts. Please see our Terms and Conditions for more information.