新闻中心
Home > News Center > Industry news

CNC Programming: What Are the Basics of G-Code and M-Code?
2026-05-20 14:04:40

 CNC Programming: What Are the Basics of G-Code and M-Code?

CNC Programming: The Basics of G-Code and M-Code (Approx. 1000 words)

Computer Numerical Control (CNC) machines are widely used in manufacturing to produce precise parts. At the heart of CNC Machining is programming, and the most common programming language for these machines is G-code, supported by M-code. Understanding the basics of G-code and M-code is essential for anyone who wants to operate or program CNC equipment.

What Is G-Code?

G-code (also called preparatory code) is a set of instructions that tells the machine how to move. These commands control the tool’s path, speed, and the way material is removed. Most lines of a CNC program start with the letter “G” followed by a number, such as G00 or G01.

Although there are different dialects of G-code depending on the machine manufacturer (Fanuc, Siemens, Heidenhain, etc.), the general concepts are similar.

Structure of a Typical G-Code Line

A typical line in a CNC program (often called a “block”) may look like this:

N10 G01 X50.0 Y25.0 Z-5.0 F200

Common elements are:

- N10 – Block (line) number; often optional but useful for reference.

- G01 – G-code command (linear interpolation, or a straight cut).

- X50.0 Y25.0 Z-5.0 – Axis coordinates in millimeters or inches.

- F200 – Feed rate, usually in mm/min or inches/min.

Each block usually ends with a line break, and the machine executes the program line by line.

Common Types of G-Codes

G-codes are often grouped by function: motion, positioning, cycles, and so on. Here are some of the most important ones for beginners.

1. Motion and Positioning

These commands control how the tool moves.

- **G00 – Rapid Positioning**

Moves the tool quickly from one point to another without cutting. Used for moving safely above the workpiece.

Example: `G00 X0 Y0 Z5.0`

- **G01 – Linear Interpolation (Cutting Move)**

Moves the tool in a straight line at a defined feed rate. Used for cutting or machining operations.

Example: `G01 X50.0 Y20.0 F150`

- **G02 – Circular Interpolation, Clockwise**

Moves the tool in a clockwise arc. Requires a target point and either a radius (R) or center offset (I, J, K).

Example: `G02 X40.0 Y40.0 R20.0 F120`

- **G03 – Circular Interpolation, Counterclockwise**

Same as G02 but counterclockwise.

Example: `G03 X0 Y40.0 R20.0 F120`

2. Coordinate Systems and Position Modes

CNC machines need to know where “zero” is and whether moves are absolute or relative.

- **G90 – Absolute Positioning**

All coordinates are referenced from a fixed zero point (usually workpiece zero).

Example: `G90 G01 X100 Y50`

- **G91 – Incremental (Relative) Positioning**

Coordinates are relative to the current position of the tool.

Example: `G91 G01 X10 Y-5` (move 10 units in X, -5 in Y from the current point)

- **G54 to G59 – Work Coordinate Systems**

These define different work offsets or origins on the part or fixture. G54 is commonly used as the primary offset.

Example: `G54` (use work offset 1)

3. Units, Planes, and Feed Modes

- **G20 / G21 – Units**

- G20: Inches

- G21: Millimeters

- **G17 / G18 / G19 – Plane Selection**

- G17: XY plane (typical for milling)

- G18: XZ plane

- G19: YZ plane

Important for circular movements and canned cycles.

- **G94 / G95 – Feed Rate Modes**

- G94: Feed per minute (common in milling)

- G95: Feed per revolution (common in turning)

4. Canned Cycles (Basic Drilling)

Canned cycles simplify repetitive operations like drilling.

- **G81 – Simple Drilling Cycle**

Drills straight down to a set depth at each position.

Example:

```

G81 X10 Y10 Z-5 R1 F100

X30

X50

G80 (cancel cycle)

```

- **G83 – Peck Drilling**

Used for deeper holes; the tool drills in steps (“pecks”) to clear chips.

There are many other G-codes—tapping cycles, boring, thread cutting, etc.—but these are enough to understand the basic workflow.

What Is M-Code?

While G-code controls motion and path, M-code (miscellaneous code) controls machine functions that are not related to coordinated axis movement. M-codes handle actions like turning the spindle on or off, starting and stopping coolant, and ending the program.

Like G-codes, M-codes are written as a letter “M” followed by a number, such as M03 or M08.

Common M-Codes in CNC Programming

- **M00 – Program Stop (Unconditional)**

Stops the machine until the operator presses start again. Useful for inspections or tool checks.

- **M01 – Optional Stop**

Similar to M00, but only active if the machine’s “optional stop” switch is turned on. Allows the operator to decide whether to stop at that point.

- **M02 / M30 – Program End**

- M02: End of program (less commonly used today)

- M30: End of program and rewind (resets to program start)

- **M03 – Spindle On, Clockwise**

Starts the spindle rotating clockwise, usually combined with an S-word for spindle speed.

Example: `M03 S1500`

- **M04 – Spindle On, Counterclockwise**

Starts the spindle rotating counterclockwise; used for some operations and tools.

- **M05 – Spindle Stop**

Stops the spindle from rotating.

- **M06 – Tool Change**

Tells the machine to change to a specified tool number. Usually written with a T-code.

Example: `T3 M06` (change to Tool 3)

- **M07 / M08 – Coolant On**

- M07: Mist coolant (if available)

- M08: Flood coolant on

- **M09 – Coolant Off**

Turns off all coolant systems.

Other M-codes can control things like chuck clamps, pallet changers, and special accessories. These often vary more between manufacturers.

How G-Code and M-Code Work Together

In a real program, G-code and M-code are used together. G-codes define motion and machining paths, while M-codes handle support functions like turning on the spindle or coolant.

A simple milling example:

```

O1000 (Simple Pocket Example)

G21 G17 G90 G40 G49 G80 (MM units, XY plane, absolute, cancel offsets & cycles)

G54 (Use work offset 1)

T1 M06 (Tool 1, tool change)

M03 S1200 (Spindle on CW at 1200 rpm)

M08 (Coolant on)

G00 X0 Y0 Z5.0 (Rapid move above start point)

G01 Z-2.0 F100 (Feed down into material)

G01 X50.0 Y0 F200 (Cut to X50)

G01 X50.0 Y30 (Cut to Y30)

G01 X0 Y30 (Cut back to X0)

G01 X0 Y0 (Close the rectangle)

G00 Z10.0 (Rapid retract)

M09 (Coolant off)

M05 (Spindle stop)

M30 (End of program)

%

```

In this example:

- G-codes control positioning, feed, and cutting paths.

- M-codes control tool change, spindle, coolant, and program end.

Basic Tips for Beginners

1. **Always Know Your Zero**

Set your work coordinate system (e.g., G54) correctly and understand whether you are in absolute (G90) or incremental (G91) mode.

2. **Use Safe Moves**

Use G00 rapid moves to safe heights before moving in X and Y to avoid hitting the workpiece or fixtures.

3. **Check Units**

Confirm whether the program uses G20 (inches) or G21 (millimeters) to prevent serious errors.

4. **Simulate Before Running**

Use simulation software or a dry run above the part to check for mistakes in your code.

5. **Read the Machine’s Manual**

G-code and M-code may vary slightly between controls; always refer to the documentation for your specific machine.

---

Understanding the basics of G-code and M-code gives you the foundation to read, modify, and eventually write your own CNC programs. As you gain experience, you can explore advanced topics like subroutines, macros, probing cycles, and 5-axis programming, but all of them still rely on the same core concepts of motion control (G-code) and machine control (M-code).

Related tags:

Comment

(0)
*Captcha: