cnc machine control panel

G and M Codes: The Differences Explained

Computer numerical control (CNC) machining, whether CNC milling, CNC lathe, or some other computer-controlled machine tool, has become the primary manufacturing process in modern machine shops. These wonders of technology can transform a rectangular workpiece into a complex and sophisticated finished product as if by magic.

However, it isn’t magic at work here because these beautiful machines sit uselessly until someone (or something) tells them what to do. And CNC programmers and machinists give the machines instructions through programming language that brings them to life and directs them to perform specific operations.

So, all these complicated processes cannot happen without CNC milling machines and lathes. And these machines cannot function until a computer and CNC programming tells them what to do in a language they understand. And we call that language G-Code.

What Does G-Code Stand for in CNC Machining?

The G in G-Code stands for geometry, giving the CNC machine directions on how and where to move. Although the system is called G-Code, other letters and numbers will designate various functions within the program. However, every G code contains directions informing the machine how to create variations of its essential functions. Typically, CNC machines have three basic operations that the system adapts to create the desired shapes:

  • Move in a straight line rapidly
  • Move in a straight line at a specific feed rate
  • Move in a circle at a specified feed rate

What are the Fundamental G-Codes?

Here are four of the most common G-codes and how they look in a G-code program:

  • G00 – Rapid positioning to a specific XY plane
  • G01 – Linear feed move
  • G02 – Clockwise circular interpolation move
  • G03 – Counterclockwise (CCW) circular interpolation move

As you can see, the lines of code are organized in blocks, with each block controlling one operation, such as a milling operation with a specific cutting tool. Each line of a block is labeled with a line number, typically the letter N and an even number (N2, N4, N6, etc.).

If you are writing a G-code program manually, you must do it correctly. Otherwise, one miswritten G-code could crash your workpiece and tooling. Fortunately, computer-aided manufacturing software is available to optimize tooling paths and generate G-code, offering instructions to the CNC machine tool. CAM software can create thousands of lines of code, so you don’t have to do it by hand.

How Many G-Codes are There?

Although we have access to about one hundred G-code commands to control CNC machines, there are a select few we call a “core group,” and they are familiar to practically every machine.

G00 Rapid Travel

Use this command for a rapid move when the tool does not touch the part. Use it only when the cutter or tool is not removing material, such as when you’re going for a tool change. When using a rapid move, be sure no clamps, parts, or vises are in the path. You can avoid a crash by moving to your XY plane selection in rapid first and then down the Z-axis next.

G01 Linear Interpolation

Use the G01 command to cut in a straight line, although it requires a feed rate command (F) before it will move. For example, the programmer can designate a starting point and move the cutting tool in a straight line along the X-axis or Y-axis to the endpoint.

G02 and G03 Circular Interpolation

G02 is used to machine an arc or radius in a clockwise direction, and G03 is counterclockwise. Remember that when you use G02 with G01 and G03, you can machine any shape, making these three G-codes the foundation of G-code programming.

G04 – Dwell

Occasionally, we must pause the cutting tool by inserting a G04 into the code for a short time. For example, you can add a dwell when a flat-bottom drill reaches the bottom of the hole, pausing the Z-Axis movement briefly, with the drill running, to clear up any chatter.

G40, G41, and G42 Cutter Compensation

Cutter compensation considers the diameter of the tool when the programmer creates a cutting path, offsetting the tool’s radius, depending on the cut’s direction. Here are the three G-codes controlling it:

  • G40 Cancels cutter compensation
  • G41 Left cutter compensation
  • G42 Right cutter compensation

G43 Tool Length Compensation

Tool length compensation enables the CNC machine to account for the length of each of your tools entered and stored in the machine. The machine calculates the changes based on the program locations and the tool length. If a G43 code is inactive, the machine will move the end of the spindle instead of the end of the cutting tool.

Canned Cycles

Canned cycles allow the programmer to write G-code to drill and bore multiple holes with fewer lines of G-code. All the information is on a single line, with the following lines being positional. These are the cycles:

  • G80 Cycle
  • G81 Drilling Cycle
  • G82 Counter Bore Cycle
  • G83 Peck Drilling Cycle
  • G84 Rigid Tapping Cycle
  • G85 Boring Cycle (bore in, bore out)
  • G86 Boring Cycle (bore in, rapid out)

Check out the comprehensive G-code list here.

What are M Codes in CNC Milling?

M-code is another machine control language for CNC machining used with G-code to switch various machine functions off and on. The M in M-code commands informs the machine that a miscellaneous function follows. While G-code commands indicate positions using the Cartesian coordinate system, M-code directs the machine’s actions. Although M represents miscellaneous functions, some say it stands for “machine” code since it controls the operations of the machine tool.

G and M codes have specific uses in programming. For instance, you can direct a CNC machine’s cutting tools to a particular spot without M codes. Still, you can’t give it direct commands, such as an optional program stop, change cutting tools, turn on the flood coolant, or other actions beyond the three primary movements that G-codes control.

What are the Different M Codes?

M03 is a spindle on command and is typically preceded by an S code to set the spindle speed. The list of M-codes begins with M00 (program stop) and continues up to M99 (subprogram end), although every number is not assigned.

The following is a partial list of M-codes:

  • M00 Program stop
  • M01 Program stop–optional
  • M02 End of program
  • M03 Spindle start–forward, of clockwise rotation
  • M04 Spindle start–reverse or counterclockwise rotation
  • M05 Spindle stop
  • M06 Tool change

To see a list of M-codes for milling machines and lathes, check here:

What are the Differences between G and M Codes?

Three fundamental ways differentiate G and M codes from one another.

  1. G-code directs the motion and function of the CNC machine, while M-code controls the operations not involving movements.
  2. G-code activates the CNC machine, and M-code activates the machine’s programmable logic controller.
  3. G-code commands often differ in CNC machines, while most M-code commands remain the same.

In Conclusion

CNC machines have transformed the manufacturing industry and simplified the process of producing precise and consistent parts. However, these machine tools would not know what to do in the absence of G-code and M-code.

In essence, G-Code instructs the machine how to perform its operations, and M-code carries out the non-geometric activities of a machine. G-code and M-code must work together if the CNC machine is to function correctly.

About Peter Jacobs

Peter Jacobs is the Senior Director of Marketing at CNC Masters, a leading supplier of CNC mills, milling machines, and CNC lathes. He is actively involved in manufacturing processes and regularly contributes his insights for various blogs in CNC machining, 3D printing, rapid tooling, injection molding, metal casting, and manufacturing in general. You can connect with him on his LinkedIn.

Have Questions? Need a Quote?

Looking for more information about our CNC machines and services? Contact us today.

Contact

Slide 1

MX Software – Easy to Use, Easy to Learn – Included with your machine purchase
The MX software is designed to work seamlessly with your CNC Masters machine. It is made to work with Windows PC – desktop, laptop, or an all in one – on standard USB. Use it on Windows 8 or 10 64-bit operating systems.
No internal conversion printer/serial port to USB software or additional conversion hardware is used with the MX.

Slide 1

2. Clutter Free Interface
The MX is engineered for the CNC MASTERS machine so you do not have to fiddle with a detailed complicated configuration that can be overwhelming. Just load in the MX and start machining!2. Clutter Free Interface
The MX is engineered for the CNC MASTERS machine so you do not have to fiddle with a detailed complicated configuration that can be overwhelming. Just load in the MX and start machining!

Slide 1

3. Features Tour and Tutorials Included
The Features Tour will give you a quick run-down on all the features the MX can do for you. The Tutorials are easy to follow even for the first time CNC machinist.
Feel free to download the MX on any of your computers. We recommend downloading the MX along with your CAD and CAM software there at the comfort of your office computer to generate your tool path programs. You don’t need to be hooked up to the machine either to test your program in simulation mode.

Slide 1

4. Navigate and Edit Your Program through the MX interface with Ease
With a few clicks of the mouse or using touch screen technology, you can easily navigate through the MX interface importing saved programs into the Editor from the File drop down menu. Using standard windows features to edit your program you can then lock the Editor Screen to avoid accidental editing, and if you need to insert a line in the middle of a program, just click on [ReNum] to re-number your tool path list.
You can create a program or import CAM generated G-code tool paths into the Editor
The X Y and Z W arrow jog buttons are displayed from the point of view of the cutter to avoid confusion when the table and saddle are moving. You can also adjust your spindle speed and coolant control while jogging each axis.

Slide 1

5. Feed Hold – Pause in the Middle of your Program
Feed Hold lets you pause in the middle of a program. From there you can step through your program one line at time while opting to shut the spindle off and then resume your program.
You can also write PAUSE in the middle of your program and jog each axis independently while your program is in pause mode.

Slide 1

6. Hot Keys
Hot Keys is an alternative method to easily control your machine using your hard or touch screen keyboard. One can press P to pause a program, press S to turn Spindle On, G to run a program, Space Bar to Stop, J to record your individual movements one line at a time to create a program in teach mode.

Slide 1

7. Pick Menu – for conversational mode programming
Write FANUC style G-codes directly into the Editor or select commands off the [Pick] menu and write your tool path program in conversational mode such as what is written in the Editor box. You can even mix between conversation commands and G-codes in the same program.

Slide 1

8. Pick Menu List of Options
Use commands such as MOVE, SPINDLE ON/OFF, COOLANT ON/OFF, PAUSE, DELAY, GO HOME…. to write your tool path programs in conversational mode.

Slide 1

9. Draw the Tool Path to verify it before pressing Go
Hit Draw to view your tool path program drawing, check out its run time, or even simulate the tool path in 3D mode. This can be helpful to quickly verify your program before running it. You can also slow down or speed up the drawing or simulation process.
You can also hit Go within the Draw Window itself to verify the cutter’s position on the machine. The current tool path will be highlighted and simultaneously draw out the next path so you can verify what the cutter will be doing next on the program.

Slide 1

10. Run each tool path independently to study its movement
1. Run the machine on Trace mode. You can run each tool path independently, one line at a time to study the tool path movement on the machine to verify the position of the application and if any fixture/vise is in the way of the cutter’s path.

2. You can also verify your program by clicking on the Trace and Draw buttons together. This will allow you to view each tool path independently one line at a time in the Draw Window.

Slide 1

11. Counters display in Inches or Millimeters – Continuous Feed
1. When running a program, the counters will display a “real-time” readout while the machine is in CNC operation without counting ahead of the movement.
2. The current tool path is highlighted while the machine is in operation without causing slight interruptions/pauses as the software feeds the tool path to the machine. The MX internally interprets a program ten lines ahead to allow for “continuous machining” avoiding slight interruptions as the machine waits for its next tool path command.
3. “Run Time” tells you how long it takes to run your tool path program.

Slide 1

12. Use the “Go From Line” command to start in the middle of your program
If you ever need to begin your program from somewhere in the middle of it, use [Go From Line] which you can find under Tools. The Help guide will walk you through how to position the cutter without losing its position on the machine.

Slide 1

13. Exact Motion Distance without over-stepping on an axis while jogging
Use “Relative ON” to enter a specific coordinate to jog any of your axes to an exact location without having to write a program. It’s like using “power feed” but easier. You can jog an exact distance on any of the axes without needing to keep the key pressed down and mistakenly over-step the movement releasing your finger too slowly off the jog button.
Let’s say you need to drill a hole exactly 0.525” using the Z. So you enter 0.525 in the Z box. Next, adjust the JOG FEED RATE slider for the desired feed rate. Then “click once” on the +Z or -Z button to activate the travel. In this case you click once the -Z button first to drill the hole exactly 0.525”. Then click once on the +Z button to drive the axis back up 0.525”.

Slide 1

14. Teach Mode – Jog Input
You can create a tool path program by storing each point-to-point movement by simply jogging an axis one at a time. Click on either of the Jog Input buttons to store each movement on the Editor Screen. You can then add Spindle ON, feed commands, and press GO to run the new program as needed. This is a great feature to help you learn to create a program by the movements you make on the machine without necessarily writing out an entire program first.

Slide 1

15. Override on the fly to adjust the Jog Feed to Rapid or the Spindle Speed during the middle of a program
1. Jog Feed and Rapid with Override: You can adjust feeds using the slider from slow minimum 0.1″ per minute to a rapid of 100″ per minute of travel. You can even micro-step your jog as low as 0.01”/min. The [-][+] buttons allow you to fine tune feeds in 5% increments while the program is in motion.
2. Spindle Speed with Override: You can adjust speeds using the slider from a slow minimum RPM to the max RPM according to the machine setup. The [-][+] buttons allow you to fine tune feeds in 5% increments while the program is in motion.

Slide 1

16. Adjust Counters using Pre-Set if you cannot begin the program from 0.00
In a situation where you cannot begin your cutter at it’s 0.00 location, you can “Pre-Set” directly into the counters by typing in your beginning coordinate. You can press Go from here to run your program. You can also “zero all” or “zero” your counters independently. With one click of the [Return to 0.0] button, all axes will travel back to its respective 0.0 on the machine.

Slide 1

17. Set and Save your 0.00 position for future runs
Set and save your 0.00 position on the machine. These coordinates will be recorded as the first line of the program in the Editor Screen. Should you desire to return to this program at a later date, you only have to click on the Set Zero Return button. This will command the machine to automatically jog each axis to its saved “set” 0.00 position according to the recorded coordinates at the first line of the program.

Slide 1

18. Create a “Home” position to clear your application and run multiple times
Let’s say you need to machine one application times 100 pieces. This usually requires a jig to retain that physical 0.00 position. But in this case, you want the program to end with a clearance of the axes to easily switch out the next piece of stock and start again. With Save Home, you have the ability to save this offset (home) position while still retaining your Set Zero position where the machine will mill your part out. Pressing [Save Home] will record this new position under the Set Zero line in your program.
Pressing [Go Home] will jog your axes back to your “saved home” position where you originally pressed the Save Home command. You can also input GO_HOME from the Pick Menu as its own tool path in your program. At the completion of your program the axes will end at your Home position. Replace your part, then press [Return to 0.0] button to allow the axes to return to its zero position, and press Go to start your next run.

Slide 1

19. Disable the axis motors to manually hand crank each axis into place
Easily de-energize the axis motors by clicking [Disable Motors] to crank each axis by hand, and then press [Reset Control] to re-energize the axis motors.

Slide 1

20. Change up to 30 tools with compensation, and store your tool offsets for other programs
The MX supports…

Slide 1

21. Use the optional ATC rack up to 8 tools for milling, drilling, and rigid tapping applications
The CNC Masters Automatic Tool Changer Rack and Tools (US Patent 9,827,640B2) can be added to any CNC Masters Milling Machine built with the rigid tapping encoder option. The tutorial will guide you through the set-up procedure using the ATC tools.

Slide 1

22. Use the optional Rigid Tapping Wizard without the need for tapping head attachments
When you order your CNC Masters machine, have it built with the optional rigid tapping encoder. You can take any drill cycle program and replace the top line with a tapping code created by the wizard to tap your series of holes up to 1/2” in diameter.

Slide 1

23. Use the optional Digital Probe to scan the profile and/or pockets of your fun/hobby type designs to write your tool path program and machine out a duplicate of your original design To “surface” scan an object, you can program the probe along the X or Y plane. The stylus will travel over the part starting on the left side front corner of the object and work its way to the end of the part on the right side. Depending on how the stylus moves, it will record linear and interpolated movements along the X, Y, and Z planes directly on the MX Editor.
To “pocket” scan an object containing a closed pocket such as circles or squares, the scan will start from the top front, work its way inside of the pocket, and scan the entire perimeter of the pocket.
Under the Setup of the MX software you will find the Probe Tab which will allow you to calibrate and program your probe. Your “Probe Step”, “Feed”, and “Data Filter” can also be changed on the fly while the probe is in the middle of scanning your object.

Slide 1

24. Use work offsets G54-G59 for nesting applications
The work offsets offer you a way to program up to six different machining locations. It’s like having multiple 0.0 locations for different parts. This is very useful especially when using sub-routines/nesting applications.

Slide 1

25. Create a Rectangular Pocket / Slot with our selection of Wizards to help you build a tool path program
The Cycle Wizards for the mill or lathe makes it easy to create a simple tool path without needing to use a CAD and CAM software.
On this Wizard, the Rectangular Pocket / Slots, can be used to form a deep rectangular pocket into your material or machine a slot duplicating as many passes needed to its total depth.

Slide 1

26. Create a Circular Pocket Wizard
Input the total diameter, the step down, and total depth and the code will be generated.

Slide 1

27. Do Thread Milling using a single point cutter Wizard

Slide 1

28. Cut a gear out using the Cut Gear Wizard with the optional Fourth Axis

Slide 1

29. Create a Peck Drilling Program in Circular or Rectangular Patterns
Using the Circular or Rectangular Drilling Wizards, you can program the machine to drill an un-limited series of holes along the X and Y planes. Program it to drill straight through to your total depth, use a high-speed pecking cycle, or deep hole pecking cycle. You can program the cut-in depth and return point for a controlled peck drill application to maximize chip clearance.

Slide 1

30. The MX interface can easily be interchanged from Mill Mode to Lathe Mode
Use this interface for your CNC Masters Lathe. It contains all the same user-friendly features and functions that comes in Mill Mode. Simply go to the Setup page and change the interface.

Slide 1

31. Use Tool Change Compensation or the optional Auto Tool Changer Turret if your application requires more than one tool in a single program
You can offset the length and angle of each tool and record it under Tools in your Setup. The program will automatically pause the lathe’s movement and spindle allowing you to change out your tool, or allowing the optional ATC Turret to quickly turn to its next tool and continue machining.
On the MX interface, you also have four Tool Position buttons. Select your desired T position, and the auto tool post will quickly turn and lock itself to that position.

Slide 1

32. Use the Lathe Wizard Threading Cycle to help you program your lathe’s internal or external threads in inches or metric

Slide 1

33. Use the Lathe Wizard Turning / Boring Cycle to help you program simple turning and boring cycles without having to go through a CAM or writing a long program with multiple passes

Slide 1

34. Use the Lathe Wizard Peck Drilling Cycle to help you program your drill applications or for face grooving

Slide 1

35. Facing / Grooving / Part Off Cycle Wizards – with Constant Surface Speed
These cycles can be used with Constant Surface Speed allowing the spindle speed to increase automatically as the diameter of the part decreases giving your application a consistent workpiece finish. With CSS built into the wizard, there is no need to break down the cycle into multiple paths and multiple spindle speed changes.

Slide 1

36. This is our list of supported G and M codes which can be found under Tools > G Code/ M Code List in the MX
If you plan to use a third-party CAM software to generate your tool path program, use a generic FANUC post processor and edit it to match our list of codes. As an option, we also sell Visual mill/turn CAM software which comes with a guaranteed post processor for our machines to easily generate your tool path programs based on your CAD drawings.

Slide 1

37. Our pledge to you…

previous arrow
next arrow