August 02, 2025

G Codes and M codes list: What you Need to Know?

G-code and M-code are the foundational languages that drive CNC (Computer Numerical Control) machinery and 3D printers. Whether you’re operating a mill, lathe, or FDM 3D printer, these command systems dictate how and when machines perform specific tasks. G-code controls tool paths and motions, while M-code governs auxiliary functions like spindle activation and temperature adjustments. Understanding these codes is crucial for programming efficient workflows, debugging operations, or optimizing machine performance. This article explains what these codes are and how they function and provides a detailed list of commonly used commands. By mastering them, users can significantly enhance control and productivity in digital fabrication.

1. What is G-code?

G-code, short for “Geometric Code,” is a numeric control programming language used to dictate the movement and behavior of CNC machines. Every G-code line instructs the machine on what to do—whether that’s moving in a straight line, carving a curve, or drilling at a specific depth.

Each command typically starts with a “G” followed by numbers (e.g., G0, G1, G28), representing different actions. These instructions are often accompanied by parameters such as X, Y, Z coordinates, F for feed rate, and S for spindle speed. The structure enables precise control over positioning, speed, and operation of the tool head.

G-code Example:

G1 X50 Y20 Z0.3 F1200 ; move to X=50, Y=20 at Z=0.3mm with a speed of 1200mm/min

This command directs the machine to move linearly to the specified coordinates with a defined feed rate.

2. What is M-code?

While G-code deals with motion, M-code (Miscellaneous Code) governs machine behaviors and auxiliary operations. M-code commands typically begin with an “M” followed by a number and are crucial for activating spindles, adjusting temperatures, changing tools, or pausing execution.

For example, M3 starts the spindle in a clockwise direction, while M104 sets the temperature of a 3D printer's hotend. These codes are often used in conjunction with G-codes to create seamless workflows. Without M-codes, many operational aspects of machines like fans, heaters, and end-of-program actions would remain uncontrolled.

3. G-code and M-code: Working Together

Most CNC programs combine G-code and M-code instructions to form a complete sequence. The interplay between these codes allows the machine to:

  • Move to specific coordinates (G-code)

  • Activate or deactivate tools (M-code)

  • Adjust temperatures or speeds (M-code)

  • Return to home position (G-code)

  • Stop the job safely (M-code)

A typical workflow might start with G28 to home the machine, followed by M104 to heat the extruder, G1 to begin movement, and M104 S0 at the end to cool the nozzle.

4. G-code Syntax: The Essentials

A G-code command often includes:

  • Command: G0, G1, etc.

  • Coordinates: X, Y, Z

  • Feed Rate: F (e.g., F1500)

  • Spindle/Speed: S (e.g., S200)

  • Comments: Typically denoted with ; or within parentheses

Example Line:

G1 X100 Y75 Z0.2 F1200 ; Draw line to (100, 75) at 0.2mm height

Good syntax and well-commented lines make troubleshooting easier and enhance collaboration among multiple users.

5. Common G-code Commands and Their Functions

G-code commands are crucial instructions that regulate the motion and functions of 3D printers and CNC machines. Each command serves a specific purpose, such as positioning the toolhead, setting speeds, or defining motion paths. Understanding these common commands allows users to fine-tune operations and ensure accurate, efficient results. Mastery of G-code enhances both performance and troubleshooting in digital fabrication.

G-code

Function

G0

Rapid linear movement

G1

Controlled linear movement

G2

Clockwise circular interpolation

G3

Counter-clockwise circular interpolation

G4

Pause (dwell)

G20

Set units to inches

G21

Set units to millimeters

G28

Return to home position

G90

Use absolute positioning

G91

Use relative positioning

G92

Set current coordinates without moving

Detailed Breakdown:

  • G0 is used when speed is the priority—rapid positioning to another point.

  • G1 is employed when precision and controlled motion are needed, particularly during printing or cutting.

  • G2/G3 define arc paths and are critical for CNC machines milling curves.

  • G90 tells the machine to interpret coordinates as absolute, while G91 uses them relative to the current position.

6. Common M-code Commands and Their Functions

M-code commands are vital in CNC and 3D printing operations, managing non-movement tasks such as turning spindles on or off, setting temperatures, or changing tools. While G-code handles positioning, M-code complements it by controlling the machine’s auxiliary functions. Understanding these commands ensures smoother workflows and better automation. A collection of frequently used M-codes and their functions can be found here.

M-code

Function

M0

Pause or stop until user resumes

M1

Optional stop

M2

End of program

M3

Start spindle (clockwise rotation)

M4

Start spindle (counter-clockwise rotation)

M5

Stop spindle

M6

Tool change

M104

Set extruder temperature (non-blocking)

M109

Set and wait for extruder temperature

M140

Set bed temperature

M190

Wait for bed to reach set temperature

M106

Turn fan on

M107

Turn fan off

M84

Disable steppers

Examples:

M104 S200 ; Set extruder to 200°C

M109 S200; Hold off till the extruder reaches 200°C.

M140 S60 ; Set heated bed to 60°C

M190 S60 ; Wait for bed to reach 60°C

These commands are essential for temperature-sensitive materials and process stability.

7. G-code and M-code in Cura (3D Printing)

Slicers like Cura allow users to add custom G-code and M-code commands at the beginning and end of a print job. These are called start G-code and end G-code, often found in printer settings.

Start G-code Example:

 

G28 ; Home all axes

G1 Z15.0 F6000 ; Raise bed

M104 S200 ; Set extruder temp

M140 S60 ; Set bed temp

End G-code Example:

 

M104 S0 ; Turn off extruder

M140 S0 ; Turn off bed

G1 X0 Y200 ; Move print head to corner

M84 ; Disable motors

This automation helps prime the printer at the beginning and cool it down at the end, increasing print quality and machine longevity.

8. Advanced G-code & M-code Tips

Mastering basic G-code and M-code is only the beginning; advanced techniques can significantly improve efficiency, precision, and machine performance. By fine-tuning commands and optimizing sequences, users can reduce print times, enhance tool paths, and prevent common issues. These expert tips are especially useful for custom workflows, automation, and troubleshooting. Whether you're working with CNC machines or 3D printers, a deeper understanding of these codes can unlock greater control and productivity.

  1. Use simulation tools: Visualize tool paths before running on actual hardware.

  2. Comment generously: Notes improve readability and simplify future editing.

  3. Modularize G-code: Keep repetitive sequences in macros if your firmware supports them.

  4. Start small: Test on basic shapes before executing complex jobs.

  5. Backup settings: Keep a safe copy of your default Cura start/end scripts before modifying.

9. Real-world Applications

G-code and M-code are not just theoretical commands—they are actively used in a wide range of industries for precision control of machines. From CNC milling and laser cutting to 3D printing, these codes ensure accuracy, repeatability, and automation. Understanding their practical use helps bridge the gap between digital design and physical manufacturing.

  • CNC Milling: Using G-code (G1, G2, G3) for shaping metal or wood and M-code (M3, M5) to control spindle direction.

  • 3D Printing: Combining G-code for layer-by-layer movement and M-code to control nozzle and bed temperatures.

  • Laser Cutting: G1 for controlled motion, M106/M107 for laser intensity via fan speed output.

Different industries like aerospace, automotive, and consumer electronics depend heavily on these command languages for precision manufacturing.

10. History and Evolution

G-code was standardized by the Electronic Industries Alliance (EIA) and first introduced in the 1960s. Over time, machine builders added their custom extensions. While many machines support a standard set of commands, some have proprietary G/M-code implementations that add special features or limitations.

Today, modern open-source firmware like Marlin and RepRap has adapted G-code for 3D printers, making it accessible and customizable for hobbyists and professionals alike.

11. Key Differences Between G-code and M-code

 

Feature

G-Code

M-Code

Purpose

Motion control

Machine operation control

Example

G1 (Linear move)

M104 (Set extruder temp)

Axes Involved

Yes (X, Y, Z)

Usually no

Execution frequency

Continuous motion commands

Intermittent machine controls

Critical role

Defines the path

Manages how the machine behaves

SelfCAD: The Best 3D Printing Software

SelfCAD is an all-in-one 3D modeling software ideal for both beginners and professionals looking to create or edit models for 3D printing. It allows users to build 3D models from scratch or import and modify existing files with ease. The software provides tools to fix common issues such as holes or mesh errors, ensuring your model is print-ready. One of SelfCAD’s standout features is its built-in online slicer, which allows users to generate G-code directly for most FDM 3D printers without needing third-party software. It supports both online and offline use, making it highly accessible. SelfCAD also offers a wide range of learning materials to help users get started. These include interactive tutorials, and YouTube videos such as the “3D Modeling 101” and “3D Modeling for Beginners” series. Overall, SelfCAD simplifies the 3D design process and encourages creativity through its user-friendly tools and educational resources.

Conclusion: 

Mastering G-code and M-code is essential for operating CNC machines and 3D printers effectively. These languages enable precise control over movement and machine behavior. By learning common commands and their uses, users can enhance production quality, reduce errors, and take full advantage of digital manufacturing technologies.



Tags: News
Categories: News



PREVIOUS ARTICLE

The Red Affair Returns: A Penfolds x folly Brasserie Brunch

NEXT ARTICLE

The Mood–Food Connection: How What You Eat Affects Your Day