CNC Programming for Beginners: What are G-Code and M-Code?


Computer numerical control (CNC) is a computer-based program that’s used in tandem with automated machining tools. However, for the machine to work, it needs instruction from a coded program featuring G-code and M-code. The programming codes, along with the right calibration from the CNC machine center, are an essential part of a repeatable manufacturing process.

What is G-Code?

G-code is a programming language that controls a CNC machine. G-code uses multiple logical outputs to complete a function, no matter how complicated a machine is.

While looking at a dedicated listing of CNC machines, you may have noticed a grid in the space where an object will be manufactured. This grid uses a specific coordinate position, which is integral in moving the object that shapes and makes parts. In fact, the “G” stands for geometry.

In programming, G-code is represented by the letter “G” and two other numbers. The set end position uses X and Y coordinates for all selections. Some notable examples include:
  • G00 X5 Y12: G00 stands for rapid motion, which is a non-cutting movement. The X and Y coordinates represent where the machine will reposition itself to begin the job.
  • G01 X5 Y12 F200: G01 stands for linear interpolation, which is a code used for the main job. The F200 is the feed rate, which represents the velocity at which the cutter is fed.
  • G02 X5 Y12 10 J-5: G02 stands for clockwise circular interpolation or a circular cut. The I0 J-5 is used to determine the x offset (I0) and y offset (J-5) to the center point.
  • G03 X5 Y12 10 J-5: G03 stands for counterclockwise circular interpolation.
  • G17, G18, G19: Selects working plane. G17 for XY, G18 for XZ, G19 for YZ.
  • G20 and G21: Both are used for unit selection. G20 for inches, G21 for millimeters.
  • G28: Return home command. Places the tool back to its original position.
  • G90.G91: Positioning commands. G90 for absolute mode, G91 for relative mode.
G-code can repeat an action indefinitely until it’s stopped. All the above coordinates allow the CNC machine to perform flexible tasks that help to produce intricate parts consistently.

What is M-Code?

M-code stands for miscellaneous functions and is typically seen as non-geometry functions. Essentially, if you can’t program it with G-code, you can use M-code to fill in the blanks.

Whereas G-code is pretty self-explanatory and works for most machines, M-code is often machine specific. For example, an M03 command, which turns the Spindle ON in a clockwise position, will bring back an error if the machine you’re using doesn’t have a spindle attachment.

To generate a real full-fledged G-code program, you need to use M-code commands. Users will separate M and G-code commands on different lines to not confuse the machine.
  • M00 - Stop Program
  • M02 - Complete Program
  • M03/M04 - Spindle ON clockwise/counterclockwise
  • M05 - Stop Spindle
  • M06 - Change Tool
  • M08/09 - Flood Coolant ON/OFF
3D-printers also use M-code, but the program takes 3 numbers instead of two:
  • M104 - Start Extruder Heating
  • M106 - Set Fan Speed
  • M140 - Start Bed Heating
Many commands need additional parameters. For example, if I wanted to turn my spindle on (M03), I would also need to set the spindle speed. If M30 represents spindle speed and SXXXX represents the speed in RPMs, the correct code could read M30 S1200 for 1200 RPM.

As a positive, you don’t need to include speed in every single line. You only need to set another parameter if you decide to change it. This is true with other lines of code, like tool position.

While most of our examples involve horizontal and vertical machining center programs, CNC lathes and other machines that use the Z coordinate can also use G-code. Instead of G01 X5 Y12, a machinist would add an extra Z(0,0) designation like so: G01 X5 Y12 Z14.

Post a Comment

Previous Post Next Post