What is G-code and M-code?

Short Answer:

G-code and M-code are special programming languages used to control CNC (Computer Numerical Control) machines. These codes give instructions to the machine about what action to perform. G-code stands for “Geometric code” and is used to control the movement of the machine like cutting, drilling, or positioning. M-code stands for “Miscellaneous code” and is used for other machine functions like turning coolant on or off, starting or stopping the spindle, etc.

In CNC machining, G-code and M-code work together to complete the operation step by step. They are written in a sequence and form the full program that the CNC machine follows to make a part automatically and accurately.

Detailed Explanation:

G-code and M-code

CNC machines do not understand human language. They work using specific codes called G-code and M-code which are written in a CNC program. These codes are a set of instructions that tell the machine how and when to perform tasks. Just like we follow directions in a recipe while cooking, the CNC machine follows these codes to perform machining processes like cutting, moving, turning, or drilling.

These codes are mostly written by CAM (Computer-Aided Manufacturing) software after designing a part in CAD (Computer-Aided Design). But they can also be written manually by experienced programmers. The combination of G-codes and M-codes makes the CNC machine work like a robot that follows every step correctly and without any mistake.

G-code (Geometric code)

G-codes are mainly used to control the movements and positions of the cutting tool or workpiece. These include straight-line motion, circular motion, setting feed rate, and defining coordinates. Each G-code starts with the letter “G” followed by a number (like G00, G01, G02, etc.).

Common G-codes:

  • G00 – Rapid positioning (fast move without cutting)
  • G01 – Linear cutting movement (straight line)
  • G02 – Clockwise circular interpolation
  • G03 – Counterclockwise circular interpolation
  • G20 – Set units to inches
  • G21 – Set units to millimeters
  • G90 – Absolute positioning
  • G91 – Incremental positioning

These codes tell the machine where to go and how to move during the machining process.

M-code (Miscellaneous code)

M-codes are used for auxiliary machine operations. They handle tasks that are not related to tool movement but are still important for the process. These include starting or stopping the spindle, turning coolant on or off, tool changing, or ending the program. M-codes start with the letter “M” followed by a number (like M03, M05, M30, etc.).

Common M-codes:

  • M03 – Spindle ON clockwise
  • M04 – Spindle ON counterclockwise
  • M05 – Spindle OFF
  • M06 – Tool change
  • M08 – Coolant ON
  • M09 – Coolant OFF
  • M30 – End of program and reset

These codes are like support commands that help the machine function smoothly along with the motion instructions.

How G-code and M-code work together

A CNC program is a list of instructions made up of both G-codes and M-codes. The program is read line by line by the CNC controller. For example, a program may first move the tool to a position using G-code, then start the spindle with an M-code, cut the material using another G-code, and finally turn off the spindle using another M-code.

These codes must be written in proper order and format, or else the machine might not work correctly. Together, they control the entire machining process from start to finish, ensuring the correct shape, size, and finish of the final product.

Benefits of G-code and M-code

  • Simple and universal – Used in almost all types of CNC machines.
  • Precise control – Allows accurate positioning and operation.
  • Customizable – Can be written or edited to suit specific parts.
  • Automation – Allows the full process to run automatically with minimal human input.
  • Repeatability – Same program can be used to make identical parts again and again.
Conclusion

G-code and M-code are the core languages of CNC machines. G-code handles movement and cutting paths, while M-code manages extra machine functions like spindle and coolant control. Both codes work together to guide the CNC machine step by step and ensure that the part is made correctly, safely, and efficiently. Understanding these codes is essential for anyone working in modern manufacturing or mechanical engineering.