G-code For CNC Machine: Commands & Uses

G-code For CNC Machine: Commands & Uses

All CNC machines rely on G-code for their functioning. 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. Keep reading to find out everything there is to know about G-code for CNC machines.

What is G-code?

What is G-code

G-code is a type of CNC programming language used for controlling 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 passed to the relevant machine part. A single line of G-code can contain multiple instructions. Each line of the G-code is defined as one block. It is a very simple programming language requiring no complex logic or mathematical skills.

G-code programming works in combination with the M-code. M-code stands for Machine code, and is thought by some to stand for miscellaneous codes. The M-code controls the various functions of the CNC machine that are not directly related to the movement. These functions include instructions like the machine loading the program, pauses in the program, and coolant flow.

Modal vs Non-modal G-codes

Two types of G-codes are used in CNC machining– modal and non-modal. A modal G-code remains in effect through the program until another G-code changes it. In contrast, non-modal G-codes remain in effect only for the block where they are used.

Do All CNC Machines Use G-code?

Yes, all CNC machines use G-code. The implementation of the G-code comes built-in with every type of CNC machine in the microcontroller.

However, manufacturers often change the format of the G-code in minor ways so it works on their particular machines.

For instance, some machines might have an added ‘0’ before the G-code numerical values. For instance, while some machines have rapid movement G-code as G00, others might have it as G0.

What is the Role of G-code in CNC Machining?

G-codes play a crucial role in CNC programming. The automation in CNC machining comes with the interpretation of G-codes. CNC machines cannot understand conversational languages. They work on a dedicated set of machine language commands. The programmers compile these commands in a G-code file to instruct the CNC machines on how to operate.

The CNC programming microcontroller is preprogrammed with the meaning of every G-code command. Therefore, when the microcontroller reads a particular command, it immediately knows what to do. If any G-code command is outside the dictionary of the CNC microcontroller, it will not work.

G-code commands work in combination with their counterpart- the M-codes. G-codes control CNC machine tool movement, while M-codes control CNC machines’ functioning processes, such as the coolant flow or the tool change. The M-code and the G-code commands are used together to make a complete CNC programming file.

How Many G-codes Are There in a CNC Machine?

There are over a hundred G-code commands for CNC programming. Most of the G-codes are common for every CNC programming operation. However, certain G-codes are specific to the type of operation like milling, turning, drilling, etc.

There can be variation in the G-code list between the different CNC manufacturers.

Every machine does not support every G-code. Additionally, machines with unique features or multi-axis machining capabilities might have additional G-codes. The manufacturer may provide instructions on the G-codes for CNC programming in the reference annual provided with the machine.

What are the Common G-code Commands Used in CNC Machines?

What are the Common G-code Commands Used in CNC Machines?There are over a hundred G-codes used in the CNC programming process. Therefore, it can be difficult to memorize all the individual codes and their meanings.

Below is a G-code list for the commonly used commands in the CNC programming process.

You can use this list as a reference point when writing any CNC programming file. It is useful to memorize important commands such as the G00-G03 as they are used throughout every CNC programming project.

CNC Movement and Travel

The codes below are used for controlling the movement and tool path in the CNC programming:

G00: Rapid Move of the Machine Tool

Rapid move command moves the tool from one point to another without cutting the material. The movements are done at highest speed possible. Therefore, no feed rate is required for the rapid move commands. It requires location coordinates in the X axis, Y axis, and Z axis.

G01: Linear Interpolation of the Machine Tool

Linear interpolation moves the tool from one point to another in a straight line. The speed is according to a feed rate 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 an arc in the clockwise direction. It requires a feed rate specified by the value ‘F’. It requires the specification of the center point (I, J, K) or the radius (R) of the arc.

G03: Counter Clockwise Arc Circular Interpolation

Same as G02. instead of clockwise direction, it cuts an arc in the counterclockwise direction.

G04: Dwell

Dwell indicates a pause in the program. It ceases the machine movements but the auxiliary functions stay on. For instance, the spindle keeps on moving while the program is in Dwell mode. The duration of the dwell is indicated by 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 creates rounded corners due to the inertia of the cutting tool. G09 elimiantes this problem by stopping the cutting tool temporarily at the corner and then moving it again, leading to perfectly sharp corners.

Plane Selection

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 Selection
  • G18 – XZ Plane Selection
  • G19 – YZ Plane Selection

Dimensions

G-code programs for dimensions indicate which measurement units are chosen. These commands are:

  • G20: Change unit measurement to inches

  • G21: Change unit measurement to millimeters

Compensation Codes

Cutter compensation codes consider parameters such as the tool length and tool radius. Using these commands can increase the precision of the overall CNC operation.

They are also known as tool offsets. These commands are:

  • G40 – Turn off tool compensation
  • G41 – Cutter compensation left
  • G42 – Cutter compensation right
  • G43 – Tool length compensation
  • G40 – Cancel tool length compensation

Work Offsets

Work offset ensures 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 particular 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- Quick grooving cycle for CNC lathes
  • G76 – Fine Boring Canned Cycle and threading cycle
  • G81 – Standard Drilling Canned Cycles
  • G82 – Standard Drill with Dwell at the bottom of the hole
  • G83 – Deep Hole Peck Drilling Cycle Retracting All Through the Hole
  • G84 – Right-Hand Tapping Cycle For Machining Threads Into Pre-drilled Holes
  • G85 – Reaming Cycle or Boring Cycle
  • G86 – Bore and Stop Canned Cycle; Spindle Stops When Tool Reaches the Bottom of the Hole
  • G87 – Boring cycle with a special tool for expanding diameter of hole
  • G88 – Boring Cycle with P instruction; P instructs the number of seconds to dwell
  • G89 – Back Boring Cycle with Dwell

Cancel Codes

  • G50: Scaling off; in some machines it may be used for programming absolute zero center point or for setting spindle speed limit

  • G80: Cancel all active canned cycles

Positioning Modes

The positioning mode refers to how the CNC machine will read the position commands. The G-code program for various positioning modes are:

  • G90 – Use absolute mode for positioning
  • G91 – Use incremental positioning

Speeds and Feeds

Speed and feed mode refer to how the machine interprets the value units. These commands are:

  • G94 – Feed per Minute Mode
  • G95 – Feed per Revolution Mode
  • G96 – Constant Surface Speed
  • G97 – Constant Spindle Speed

Plane Return

Plane return commands contemplate the cutting tool location in various planes. Common plane return commands are:

  • G98 – Return to Initial Plane
  • G99 – Return to Rapid Plane

Lesser Used G Codes

Some of the G-codes are not as common as the ones listed above. However, you may need them now and then for specific program requirements. Here is a list of some of the lesser used G-code:

  • G10 – Programmed Offset Input
  • G22 – Stored Stroke Limit
  • G23 – Stored Stroke Limit Cancel
  • G27 – Zero Return Check
  • G28 – Zero Return
  • G29 – Return From Reference 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 Position
  • G60 – Single Direction Move
  • G61 – Exact Stop Check (Modal)
  • G64 – Normal Cutting Mode
  • G65 – Custom Macro Call
  • G66 – Custom Macro Modal Call
  • G67 – Cancel Custom Macro Modal Call
  • G68 – Coordinate Rotation Mode
  • G69 – Cancel Coordinate Rotation Mode
  • G92 – Program a Work Offset

Are There Any Safety Considerations When Programming G-code For CNC Machines?

Are There Any Safety Considerations When Programming G-code For CNC Machines

Yes, some safety considerations exist when programming G-code for a CNC machine. A CNC machine is capable of cutting material of extreme hardness.

Therefore, incorrect G-code can be a safety hazard for the machine, the operator, and the work area. The G-code should consider factors such as the work offsets and tool length offsets to ensure there is no tool breakage.

Tool breakage is a frequent accident that occurs due to tool collisions. Running prior simulations in CAD and CAM software can eliminate any chances of errors and bugs 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 the G-code. M-codes control the various CNC machine functions unrelated to the movement.

M-codes are specific to the particular CNC machine controller and its supported features. 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 for learning CNC programming and making any part through an automated machining process. The good thing is that learning how to read G-code takes a very short while.

There are many different G-codes for various 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 can vary slightly based on the particular manufacturing model you are using. Therefore, refer to the machine’s user manual to check for any differences in your equipment’s CNC programming design.

Custom CNC Machining Services for Metal and Plastic Parts