No history yet

G-code M-code Basics

The Language of the Machine

A CNC machine needs a precise set of instructions to turn a digital design into a physical part. This language, a series of commands that control every action, is called G-code.

Think of it as a recipe for a robot. Each step must be explicit, from how fast the tool moves to when the coolant turns on. The language is structured into two primary types of commands: G-codes and M-codes. They work together, but they have distinct jobs.

G-codes tell the machine tool what to do (move, cut, etc.), while M-codes tell the machine tool how to do it (speed, coolant, etc.).

G-codes are preparatory commands. They prepare the machine for a certain kind of action, mostly related to movement. They answer the questions 'Where should the tool go?' and 'How should it get there?'

M-codes are miscellaneous or machine commands. They control the machine's auxiliary functions, the things that aren't direct movements. M-codes handle actions like starting the spindle, turning on the coolant, or stopping the program.

Anatomy of a Command

G-code is read by the machine one line at a time. Each line is called a block, and it contains a series of commands that tell the machine what to do at that specific moment. A block might look complicated, but it's just a collection of simple instructions grouped together.

Let's break down a typical block:

N10 G01 X100 Y50 F200 S1500 T01 M03

This single line gives the machine a complete set of instructions for one step of an operation. Each part of the block is called a word, and each word starts with a letter that defines its function.

WordNamePurpose
N10Sequence NumberIdentifies the block's line number. Helps with editing and troubleshooting.
G01G-codeA preparatory command. In this case, 'Linear Interpolation' (move in a straight line).
X100 Y50Axis CoordinatesThe target destination for the tool on the X and Y axes.
F200Feed RateThe speed at which the tool moves to the target. (e.g., 200 mm/minute).
S1500Spindle SpeedThe speed at which the tool rotates (1500 revolutions per minute).
T01Tool NumberSelects tool number 1 from the tool changer.
M03M-codeA miscellaneous command. Here, it tells the spindle to start rotating clockwise.

The machine executes all commands in a block at the same time. So, in block N10, the machine will select tool 1, start the spindle spinning clockwise at 1500 RPM, and begin moving towards the coordinates X100, Y50 at a feed rate of 200 mm/minute, all at once. The order of words within a block generally doesn't matter, but the sequence of blocks is critical.

Command Categories

While there are dozens of specific G-codes and M-codes, they can be grouped into a few logical categories. Understanding these helps in reading and writing programs more effectively.

G-Code Categories:

  • Motion: These are the most common codes. They command the machine to move in different ways, such as in a straight line (G01) or a rapid, non-cutting move (G00).
  • Coordinate System: These codes set the reference point for all movements. Commands like establish the 'work offset', telling the machine where the raw material is located on its table. Others, like G90, set the machine to mode.
  • Tool Compensation: These codes adjust the tool's path to account for its size. This allows you to program the part's final geometry without manually calculating the tool's offset.

M-Code Categories:

  • Spindle Control: These are fundamental commands that turn the spindle on (M03 for clockwise, M04 for counter-clockwise) and off (M05).
  • Coolant Control: These manage the flow of coolant to the cutting tool. M08 typically turns on the main flood coolant, while M09 turns all coolant off.
  • Program Control: These codes manage the flow of the program itself. M00 causes a program to stop until the operator presses start again, while ends the program and resets it to the beginning.

Understanding these basic blocks and categories is the first step in translating a design into a set of instructions the machine can follow. Every complex machining program is built from these simple, logical components.