G and M Codes: Understanding the CNC Programming Languages

G and M Codes- Understanding the CNC Programming Languages

G Code is the programming language of around 99% of machine tools, plus layer upon layer of increasingly complex machines that the original language developers could not have imagined.

If you use a CNC mill or lathe, 3D printer, EDM or wirecut machine, laser or waterjet cutter, router or any of the vast number of other machines that use this language, then G code is an essential tool and you simply have to understand its basics.

G code CNC programming is the language that drives the machines that make everything. Without this language, the whole manufacturing process just stops.

G Code instructs machines in long sequences of code that tell them:

  • Where to move to
  • What to do while axes move
  • What to do when they arrive

G code (and the associated M codes) are among the oldest programming languages. G code is the only language that has seen uninterrupted and continuous use for 70+ years.

In CNC (computer numerical code) lathes or mills, a spinning cutter is directly instructed in following a toolpath, removing material to extract a net-shape from a billet of material.

Similarly, in a 3D printer, laser cutter etc the machine is directed in precise geometric actions using G code. The operations it performs are specialist, and these are generally directed with M Codes, machine type/family specific instructions used within G code.

What is G Code and M Code?

What is G Code?

Is the Instruction set that tells machine tools what to do and how to do it. The G-code lines control how and where to move the machine’s functional devices (spindle cutters, lasers, knives, 3D print extruders etc.) following a predetermined route, or toolpath specifying all aspects of the machine’s orthogonal motion operations such as speed, direction, and cutter offset.

G code is written as a sequence of text commands, read sequentially. These are interpreted by the machine’s controller and executed to perform the planned operations in written order with no overlap.

G code programming

Each command line is completed before another is executed, throughout the entire program. G code commands are typically preceded by the letter “G” followed by a numerical value that specifies the particular operation to be performed, such as moving the tool to a location at a speed; or starting a cutting operation etc.

G-code is the most used machine control language, in a huge range of industries, from manufacturing and engineering to 3D printing and robots.

It is a powerful language for directing and controlling common CNC machine types and many other machine classes. It enables precise and efficient manufacture of complex parts and components.

What is M Code?

What is M Code

M code is not a programming language on its own. It is specialist codes used in conjunction with (embedded within) G code programming.

While G-code commands are used to control the movement and position of the machine’s functional end (cutters, lasers, extruders etc.), M-code commands drive auxiliary functions such as turning on and off the machine’s spindle, coolant, and other peripheral devices.

M code commands control machine type and manufacturer specific CNC functions, but not the basic positioning and speed of the axes.

M code commands are typically preceded by the letter “M” followed by a numerical value that specifies the function to be performed, for example turning on the laser, starting or stopping a tool spindle or activating a coolant pump.

M code commands can also be used to specify other machine-related functions, such as automated tool changers.

M codes are specific to each machine type/family/manufacturer and to classes of controller, so M codes are often quite specific to individual machine manufacturers, with the same code having entirely other meanings on other machines.

Differences

G code is written to direct the movement of the machine’s axes, carrying its functional tool(s) into position, where M codes are written within the G code, to control machine specific functions like auto tool change, laser power levels, coolant etc.

Together, these two programming aspects drive the powerful tools that are the basis of modern manufacturing – from vinyl cutting for automotive wrapping to stone cutters, from 6 axis CNC machining centers to small home FDM/FFF 3D printers.

The Role of Machine Programming in CNC Machining

The Role of Machine Programming in CNC Machining

Machine programming is the way in which a skilled programmer/operator turns a 2D or 3D model into a thing, by instructing a CNC machine, 3D printer or other tool/machining center.

Automated manufacturing machines exist to perform precise, complex and repetitive operations, and G/M codemachine programming is the means of providing the instructions to exactly instruct the machine in what to do, and how to do it.

The machine programming consists of writing a line-by-line read G code and M code sequence that the machine controller can translate into stepper motor, logic line and relay operations.

These drive the machine along a specific path, controlling speed, direction, acceleration, cutter actions, tool offsets etc.

The programming also encompasses instructions for machine auxiliary functions, such as starting and stopping the spindle or laser and starting the coolant pump.

Machine programming enables extremely complex parts to be manufactured in a sequence of simple and highly regulated steps. By this means, the machine operator can quickly create parts with intricate shapes and geometries that would be months of extremely skilled work to make by hand.

The greatest strength of this type of manufacturing equipment is that, once set up and validated in output component quality, sequences of additional parts require almost no labor or monitoring. Mass production without hands is how costs plummet and repeatability/quality rise.

Without G code CNC programming, the machines are unable to move or produce any part.

How Does CNC Programming Control CNC Machines?

How Does CNC Programming Control CNC Machines

CNC provides a set of instructions in G code and M code that instructs the machine as to how to follow the required tool path and control ancilliary operations.

G code, with machine specific M code instructions included, is a sequenced instruction set that the machine controller translates into motor, switch and relay controls that drive positioning and actions, within the machine.

The G-code commands control the speed, direction, tool offsets and depth of cut, allowing the operator to instruct machine motion and actions to deliver the required cuts.

The M-code commands are generally specific to machine types, classes and manufacturers and do not transfer well between machines. they control auxiliary functions and are generally embedded in the G code as it is written.

With a program uploaded to the machine controller and material appropriately placed in the work area, machining can begin.

The machine controller reads and executes the program, following the move and auxiliary commands to perform the required operations. This extracts the part from the material billet efficiently and repeatably.

G Code and M Code Commands List

G code commands are common to types of machines and largely interchangeable in understanding between machine types.

M codes are generally specific to a class of machines and often to a particular manufacturer, controlling custom functions within their machines.

G Code List – G Codes for CNC Lathe

Lathes use a different group of G codes, essentially a different version of the programming language:

  • G00 – Rapid Motion Positioning
  • G01 – Linear Interpolation Motion
  • G02 – CW Circular Interpolation Motion
  • G03 – CCW Circular Interpolation Motion
  • G04 – Dwell
  • G09 – Exact Stop
  • G10 – Set Offsets
  • G12 – Circular Pocket Milling CW
  • G13 – Circular Pocket Milling CCW
  • G14 – Secondary Spindle Swap
  • G15 – Secondary Spindle Swap Cancel
  • G17 – XY Plane
  • G18 – XZ Plane
  • G19 – YZ Plane
  • G20 – Select Inches
  • G21 – Select Metric
  • G28 – Return To Machine Zero Point
  • G29 – Return From Reference Point
  • G31 – Skip Function
  • G32 – Thread Cutting
  • G40 – Tool Nose Compensation Cancel
  • G41 – Tool Nose Compensation (TNC) Left
  • G42 – Tool Nose Compensation (TNC) Right
  • G43 – Tool Length Compensation + (Add)
  • G50 – Spindle Speed Limit
  • G50 – Set Global coordinate Offset FANUC
  • G52 – Set Local Coordinate System FANUC
  • G53 – Machine Coordinate Selection
  • G54 – Coordinate System #1 FANUC
  • G55 – Coordinate System #2 FANUC
  • G56 – Coordinate System #3 FANUC
  • G57 – Coordinate System #4 FANUC
  • G58 – Coordinate System #5 FANUC
  • G59 – Coordinate System #6 FANUC
  • G61 – Exact Stop Modal
  • G64 – Exact Stop Cancel G61
  • G65 – Macro Subprogram Call Option
  • G68 – Rotation
  • G69 – Cancel G68 Rotation
  • G70 – Finishing Cycle
  • G71 – O.D./I.D. Stock Removal Cycle
  • G72 – End Face Stock Removal Cycle
  • G73 – Irregular Path Stock Removal Cycle
  • G74 – End Face Grooving Cycle
  • G75 – O.D./I.D. Grooving Cycle
  • G76 – Threading Cycle, Multiple Pass
  • G80 – Canned Cycle Cancel
  • G81 – Drill Canned Cycle
  • G82 – Spot Drill Canned Cycle
  • G83 – Normal Peck Drilling Canned Cycle
  • G84 – Tapping Canned Cycle
  • G85 – Boring Canned Cycle
  • G86 – Bore and Stop Canned Cycle
  • G89 – Bore and Dwell Canned Cycle
  • G90 – O.D./I.D. Turning Cycle
  • G92 – Threading Cycle
  • G94 – End Facing Cycle
  • G95 – Live Tooling Rigid Tap (Face)
  • G96 – Constant Surface Speed On
  • G97 – Constant Surface Speed Off
  • G98 – Feed Per Minute
  • G99 – Feed Per Revolution
  • G100 – Disable Mirror Image
  • G101 – Enable Mirror Image
  • G103 – Limit Block Lookahead
  • G105 – Servo Bar Command
  • G107 – G107 Cylindrical Mapping
  • G110 – Coordinate System #7
  • G111 – Coordinate System #8
  • G112 – XY to XC Interpolation
  • G113 – Cancel G112
  • G114 – Coordinate System #9
  • G115 – Coordinate System #10
  • G116 – Coordinate System #11
  • G117 – Coordinate System #12
  • G118 – Coordinate System #13
  • G119 – Coordinate System #14
  • G120 – Coordinate System #15
  • G121 – Coordinate System #16
  • G122 – Coordinate System #17
  • G123 – Coordinate System #18
  • G124 – Coordinate System #19
  • G125 – Coordinate System #20
  • G126 – Coordinate System #21
  • G127 – Coordinate System #22
  • G128 – Coordinate System #23
  • G129 – Coordinate System #24
  • G154 – Select Work Coordinates P1-99
  • G156 – Broaching Canned Cycle
  • G167 – Modify Setting
  • G170 – Cancel G171/G172
  • G171 – G171 Radius Programming Override
  • G172 – G172 Diameter Programming Override
  • G184 – Reverse Tapping Canned Cycle For Left Hand Threads
  • G186 – Reverse Live Tool Rigid Tap (For Left Hand Threads)
  • G187 – Accuracy Control
  • G195 – Forward Live Tool Radial Tapping (Diameter)
  • G196 – Reverse Live Tool Radial Tapping (Diameter)
  • G198 – Disengage Synchronous Spindle Control
  • G199 – Engage Synchronous Spindle Control
  • G200 – Index on the Fly
  • G211 – Manual Tool Setting
  • G212 – Auto Tool Setting
  • G234 – Tool Center Point Control (TCPC)
  • G241 – Radial Drill Canned Cycle
  • G242 – Radial Spot Drill Canned Cycle
  • G243 – Radial Normal Peck Drilling Canned Cycle
  • G245 – Radial Boring Canned Cycle
  • G246 – Radial Bore and Stop Canned Cycle
  • G249 – Radial Bore and Dwell Canned Cycle
  • G250 – Cancel Scaling
  • G251 – Scaling
  • G254 – Dynamic Work Offset (DWO)
  • G255 – Cancel Dynamic Work Offset (DWO)
  • G266 – Visible Axes Linear Rapid %Motion
  • G268 – Enable Feature Coordinate System
  • G269 – Disable Feature Coordinate System
  • G390 – Absolute Position Command
  • G391 – Incremental Position Command

M Codes for CNC Lathes

M Codes for CNC Lathes

M codes are generally manufacturer specific, with some commonality.

The codes for the FANUC CNC lathes are shown to illustrate:

  • M00 – Program stop
  • M01 – Optional program stop
  • M02 – End of program
  • M03 – Spindle start forward CW
  • M04 – Spindle start backward CCW
  • M05 – Spindle stop
  • M08 – Coolant on
  • M09 – Coolant off
  • M29 – Rigid tap mode
  • M99 – End of program

G Code List for CNC Milling

  • G00 – Rapid traverse
  • G01 – Linear interpolation
  • G02 – Circular interpolation CW
  • G03 – Circular interpolation CCW
  • G04 – Dwell
  • G17 – X Y plane selection
  • G18 – Z X plane selection
  • G19 – Y Z plane selection
  • G28 – Return to reference position
  • G30 – 2nd, 3rd and 4th reference position return
  • G40 – Cutter compensation cancel
  • G41 – Cutter compensation left
  • G42 – Cutter compensation right
  • G43 – Tool length compensation + direction
  • G44 – Tool length compensation – direction
  • G49 – Tool length compensation cancel
  • G53 – Machine coordinate system selection
  • G54 – Workpiece coordinate system 1 selection
  • G55 – Workpiece coordinate system 2 selection
  • G56 – Workpiece coordinate system 3 selection
  • G57 – Workpiece coordinate system 4 selection
  • G58 – Workpiece coordinate system 5 selection
  • G59 – Workpiece coordinate system 6 selection
  • G68 – Coordinate rotation
  • G69 – Coordinate rotation cancel
  • G73 – Peck drilling cycle
  • G74 – Left-spiral cutting circle
  • G76 – Fine boring cycle
  • G80 – Canned cycle cancel
  • G81 – Drilling cycle, spot boring cycle
  • G82 – Drilling cycle or counter boring cycle
  • G83 – Peck drilling cycle
  • G84 – Tapping cycle
  • G85 – Boring cycle
  • G86 – Boring cycle
  • G87 – Back boring cycle
  • G88 – Boring cycle
  • G89 – Boring cycle
  • G90 – Absolute command
  • G91 – Increment command
  • G92 – Setting for work coordinate system or clamp at maximum spindle speed
  • G98 – Return to initial point in canned cycle
  • G99 – Return to R point in canned cycle

M Codes for CNC Milling

  • M00 – Program stop
  • M01 – Optional program stop
  • M02 – End of program
  • M03 – Spindle start forward CW
  • M04 – Spindle start reverse CCW
  • M05 – Spindle stop
  • M08 – Coolant on
  • M09 – Coolant off
  • M29 – Rigid tap mode
  • M30 – End of program reset
  • M40 – Spindle gear at middle
  • M41 – Low Gear Select
  • M42 – High Gear Select
  • M68 – Hydraulic chuck close
  • M69 – Hydraulic chuck open
  • M78 – Tailstock advancing
  • M79 – Tailstock reversing
  • M94 – Mirror image cancel
  • M95 – Mirror image of X axis
  • M98 – Subprogram call
  • M99 – End of subprogram

Who Needs to Learn G-Codes and M-codes?

Who Needs to Learn G-Codes and M-codes

G code and M code together make the programming language used in most automated manufacturing machines. Anyone involved in using such machines, from operators and machinists to engineers and designers, should understand the basics of machine programming.

This is true across almost all manufacturing industries such as manufacturing, engineering, prototyping, and even hobbyists and DIY enthusiasts. The skill is equally important across many other fields as diverse as cake decorating and scientific instrument operation.

Those who are responsible for setting up and running G code driven machines need to know how to read and interpret the commands that operate their machines.

They must be familiar with the commands and their functions and the adjustment of the machine’s operating parameters, capabilities and limitations, in order to deliver the required results.

Anyone involved in CNC and automated machine operation, from operators and machinists to engineers and designers, require an understanding of G/M code.

Conclusion

No programming skill is more widely used across more sectors than G/M code programming. It is in some regards the most universal skill across all manufacturing sectors – wherever a machine requires precision orthogonal (or polar) positioning commands and performs operations on materials as it follows a tool path, the programming of G code (and associated M codes) is universally required.

As an individual working in operational manufacturing planning/management, a basic understanding of G code programming and machine operation offers a career path that can lead in many fruitful directions – from machine operations through to senior management, following well worn promotional pathways.

Kemal CNC Machining Services

Kemal CNC Machining Services

Kemal has built an extensive and highly capable skillset in all forms of CNC machining services. We offer in-house 3-4 axis CNC machining and seamless outsource management for access to the more specialist skills in multi axis machining.

Our team has the skills and knowledge to support your production needs

We will be delighted to hear from you and understand your project requirements so we can together figure out how we can help you achieve excellence.

FAQs

G code and associate M codes are virtually universal across manufacturing machines.

There are 100 basic commands in the G code language.

With some understanding of 3 dimensional reasoning and some imagination combined with a systematic approach to problem solving, basic G code programming is very easy to learn – and the skills can develop quickly, once practiced in an operational environment.

G code is the most common language for programming CNC machines.

Put your parts into production today

Content in this article

Request A Quote

*We respect your confidentiality and all information are protected.