CNC Milling G-Codes and M-Codes

CNC Milling G-Codes and M-Codes
— · 0 votes

G-codes and M-codes are the address words a CNC milling machine reads to move and to switch functions. G-codes tell the machine how to move and where, while M-codes control the machine functions around that motion, such as spindle rotation and coolant. Together they form the program a machinist writes, loads, and verifies before the first cut.

  • G-codes govern motion, plane, units, offsets, and canned cycles; M-codes govern spindle, coolant, tool change, and program flow.
  • A block combines a G-code, coordinates, feed, speed, and an M-code in one line the control reads in order.
  • Modal codes stay active until changed, so the machine keeps a motion mode long after the line that called it.
  • Safe start-up blocks and single-block proving catch errors before the tool reaches the workpiece.
  • Each control builder implements its own code set, so you confirm every code against the machine documentation.

What G-codes and M-codes do

G-codes and M-codes split CNC milling commands into two families with different jobs. A G-code, called a preparatory code, prepares the machine for a mode of work, such as moving in a straight line or selecting a work offset. An M-code, called a miscellaneous function, switches a machine function on or off, like the spindle or the coolant.

The control reads each block of the program word by word and acts on the codes inside it. Motion codes carry the tool along the programmed path, and function codes change the state of the machine around that path. A program is readable when you can tell from the codes alone what the tool does and what the machine does at the same time.

Milling G-code and milling M-code differ in scope from the codes of a lathe or a router. A vertical machining center adds codes for tool changing, spindle orientation, and pallet or chip functions. The same letter address can mean something else on another machine type, so you read the code list for the machine in front of you.

Reading a program block

A program block is one line of code that the control executes as a unit. The block starts with an optional line number, then carries the codes and coordinates that belong to that step. The control processes the words in a defined order rather than strictly left to right.

A typical cutting block holds a motion G-code, X and Y coordinates, a Z depth, a feed rate, and an M-code for coolant. Speed and feed words sit beside the path so the tool arrives at the right rate. The block ends where the next line begins, and each line should describe one clear action.

Word order inside the block matters less than the control order, because the control sorts functions before it moves an axis. An M-code that starts the spindle and a motion block on the same line can run in the sequence the builder defines. Write the block so a reader can follow the action and so the machine has no reason to guess.

Modal codes and their memory

Modal codes stay in effect after the block that contains them ends. Once a program calls a motion mode, the control keeps that mode until another code of the same group replaces it. A feed rate written once runs the following motion blocks until a new feed appears.

Non-modal codes act only in the block that holds them and then expire. A single-block stop or a one-time function belongs in that second family. Mixing the two families by accident is a frequent source of a program that behaves differently than the drawing expects.

Safe practice cancels modal modes you no longer need. Writing a cancel code for cutter compensation or fixed cycles at the end of an operation leaves the control in a known state. The next operation then starts from a clean mode instead of inheriting an old one.

Motion, plane, and units codes

Motion G-codes define the path the tool follows between points. Rapid positioning, linear feed moves, and circular interpolation in arcs form the core of milling G-code. Arc moves need a plane selection so the control knows which two axes carry the circle.

Units and plane codes set the frame for every coordinate that follows. A program states whether coordinates are in millimeters or inches before the first move. Plane selection then tells the control where the tool normal and the arc center sit.

Coordinate system codes and work offsets decide where the program's zero point lies on the part. Offset registers hold the distance from machine home to that zero. A wrong offset moves the whole program, so the offset numbers are checked with the same care as the path itself.

Canned cycles and drilling

A canned cycle packs a repeating operation into one call and a set of parameters. Drilling, boring, and tapping cycles repeat at each position the program lists. The cycle call defines the depth, the retract height, and the feed for the operation.

A hole position line repeats the cycle at a new location until the cycle is canceled. The cancel code is a required step, because the cycle stays modal and would act at later coordinates. Machinists cancel the cycle as soon as the hole pattern finishes.

A fixed cycle removes repetitive code but hides detail, so the operator checks the parameter values before a run. Depth, clearance, and retract values decide whether the tool clears the part and the fixture. Verify the cycle on the first hole with the feed held down when the job allows it.

M-codes for spindle, coolant, and tools

M-codes control the machine functions that surround the cut. Spindle start, spindle stop, coolant on, and coolant off belong to this group. The direction of spindle rotation is part of the code, so a tapping or left-hand tool job depends on the right choice.

Tool change and program control also use M-codes. A tool change code lets the machine swap the active tool at a defined stop position. Program end and optional stop codes decide how the control leaves the run at a chosen point.

M-codes act on real hardware, so a wrong code can start the spindle in a bad condition or drop coolant where it is not wanted. The machine documentation defines the exact code numbers for VMC codes across builders. Treat an unfamiliar M-code as a stop point until you confirm it in that documentation.

Building and proving a program

A CNC milling program opens with a safe start block that sets units, plane, offsets, and a known feed state. The block places the tool in a clear position and cancels modes left over from a previous job. A deliberate start makes the rest of the program predictable to read and to run.

Each tool change brings a new set of length and diameter data into play. The program calls the tool, starts the spindle at a safe height, and moves toward the first feature with clearance. Feed engages only where the tool can cut safely.

Proving the program without stock in the vise catches mistakes before they cost a part or a tool. Single-block run, distance-to-go display, and a graphic simulation each show a different view of the path. Some checks and repairs on the machine need a trained specialist, and the machine builder's manuals define the safe limits.

Checking codes against the machine

Every control builder implements its own code set, and the same number can differ between builds. An option such as high-speed machining or a probing routine adds codes the standard list does not show. Confirm each code against the documentation supplied with the machine.

A code list from another machine is a guide at most, not a reference for your control. Compare the code number, the parameter order, and the cancel behavior before you reuse an old program. A single mismatch between two builders can turn a safe move into a crash.

Keep a working program that runs correctly on the machine as a reference point. When a new program fails, compare the block structure and the modal state with that known program. Learning the codes of your own machine pays off faster than memorizing a generic list.

Questions about milling G-codes and M-codes

What is the difference between G-codes and M-codes in CNC milling?

G-codes prepare the machine for a mode of work such as a linear move, an arc, or a drilling cycle. M-codes switch machine functions such as spindle rotation, coolant, and tool change on or off. G-codes shape the tool path, and M-codes manage the hardware around that path.

Do G-codes and M-codes stay active after the line runs?

G-codes in a modal group stay active until another code of the same group replaces them, and M-codes behave the same way. Codes outside a modal group act only in the block that holds them. Cancel the modes you no longer need so the next operation starts from a known state.

Can I use the same G-code list on every CNC milling machine?

No, each control builder implements its own code set, and the same number can mean something else on another build. Options and machine functions add codes that a general list does not cover. Confirm every code against the documentation for the machine you run.

Which M-codes handle the spindle and coolant?

M-codes handle spindle start, spindle stop, spindle direction, coolant on, and coolant off. The exact numbers depend on the control and the machine builder. Look up the spindle and coolant codes in the documentation before you write or edit a program.

Why is a canned cycle canceled after drilling?

A canned cycle is modal, so it repeats at every coordinate line until another code cancels it. Without the cancel code, the cycle would act at later positions you did not intend. Cancel it as soon as the last hole in the pattern finishes.

Read next

CNC Programming Fundamentals

Latest articles