CNC G-Code Programming: The Ultimate 2026 Handbook (Free Cheat Sheet)
Master CNC G-code from A to Z. Download our FREE printable cheat sheet with all G-codes and M-codes. Perfect for beginners and operators. Start your first program today!
Table of Contents
Introduction
In the high-stakes world of industrial manufacturing, the bridge between digital design and physical reality is built on code. For facility owners and production managers investing in precision equipment, the efficiency and safety of your shop floor depend not just on the hardware, but on the operator’s ability to communicate with it. While modern CAM software automates much of the programming process, a fundamental understanding of Basic G-code for CNC remains a critical, non-negotiable skill for competent operators.
Reliance solely on automated software without understanding the underlying language is a significant risk. A single misread line of code can lead to catastrophic machine crashes, thousands of dollars in damaged tooling, ruined workpieces, and unacceptable downtime. Conversely, an operator proficient in Basic G-code for CNC can quickly troubleshoot errors at the controller, optimize program efficiency on the fly, and ensure safe setups.
At YIHAICNC, we believe empowered operators lead to maximized productivity. This guide provides a technical, (authoritative) deep dive into the 10 most essential commands that form the bedrock of CNC operation, moving beyond mere definitions into practical application and industrial context.
Download our free G-code cheat sheet at the end of this article.
| G/M Code | Function | Example | Common Use |
|---|---|---|---|
| G00 | Rapid positioning (fast move, no cutting) | G00 X50 Y25 | Move tool quickly to start position |
| G01 | Linear interpolation (cutting in straight line) | G01 X100 F1500 | Profile cutting, straight edges |
| G02 | Circular interpolation – clockwise | G02 X50 Y0 R25 | Cutting arcs, circles (CW) |
| G03 | Circular interpolation – counter-clockwise | G03 X-20 Y20 R20 | Cutting arcs, circles (CCW) |
| G17 | Select XY plane | G17 | Standard 3-axis routing |
| G20 | Inch units | G20 | Imperial mode programming |
| G21 | Metric units (millimeters) | G21 | Metric mode programming |
| G28 | Return to machine home | G28 Z0 | Safe retract at program end |
| G90 | Absolute positioning | G90 | Move to fixed coordinates |
| G91 | Incremental positioning | G91 | Move relative to current position |
| M03 | Spindle on – clockwise | M03 S18000 | Start cutting |
| M05 | Spindle stop | M05 | Stop spindle before tool change |
| M06 | Tool change | T02 M06 | Swap to tool #2 |
| M08 | Coolant / air blast on | M08 | Chip evacuation |
| M30 | Program end & reset | M30 | End of main program |
📄 Free Download: To help you master CNC programming, we’ve created a one‑page printable G‑code cheat sheet. It lists the 10 most essential G‑codes and 5 key M‑codes with examples. Keep it next to your CNC control panel for quick reference.
👉 [Click here to download the PDF (free, no email required)] – or right‑click and save.
The Foundation: The Language of Precision Manufacturing
G-code, officially known as RS-274, is the alphanumeric programming language used to tell computerized machine tools how to make something. It is the industry standard for controlling CNC (Computer Numerical Control) machines, including routers, mills, and lathes.
While it’s termed “G-code,” a complete program consists of two primary language types working in harmony:
- G-Codes (Geometric Commands): These tell the machine how to move. They define positioning, feed modes, coordinate systems, and plane selection. They are the “action” verbs of the program.
- M-Codes (Miscellaneous Functions): These control the machine’s auxiliary functions, such as turning the spindle on or off (e.g., M03 spindle on), activating coolant, or ending the program. They are the switches that manage the machine environment.
For a B2B buyer, ensuring your team masters Basic G-code for CNC is an investment in operational resilience. It ensures that your expensive machinery is being driven with intent and understanding, rather than blind faith in software post-processors.
Prerequisite Knowledge: Coordinates and Positioning Modes
Before executing any movement commands, an operator must understand the virtual space in which the machine operates. The effectiveness of any Basic G-code for CNC is entirely dependent on the coordinate system it references.
The Cartesian Coordinate System (XYZ Coordinates)
Almost all CNC machining is based on the Cartesian coordinate system. This system uses three perpendicular axes to define any point in 3D space:
- X-Axis: Typically represents horizontal movement from left to right (longitudinal travel).
- Y-Axis: Typically represents horizontal movement from front to back (cross travel).
- Z-Axis: Represents vertical movement, up and down. The Z-axis is critical as it controls tool depth. A positive Z value usually moves the tool away from the workpiece, while a negative Z value plunges into it.
Mastering XYZ coordinates is the first step in preventing collisions. The operator must always know where the “machine zero” (the machine’s inherent origin) is relative to the “workpiece zero” (the program’s starting point on the material).
G90 (Absolute Positioning) vs. G91 (Incremental Positioning)
These two preparatory G-codes fundamentally change how the machine interprets coordinate data. Mixing them up is one of the most common causes of programming errors.
- G90 – Absolute Positioning: When G90 is active, all coordinate values refer to a fixed origin point (usually the workpiece zero). If the code reads X100.0 Y50.0, the machine moves to the exact coordinate (100, 50) regardless of where it is currently located. This is the standard mode for most modern CNC programs because it is predictable and easier to troubleshoot.
- G91 – Incremental Positioning: When G91 is active, coordinate values are interpreted as distances and directions from the current tool position. If the tool is at X10.0 and the code reads X20.0, the machine will move 20 units in the positive X direction, ending up at X30.0. Incremental programming is useful for subroutines or repeating patterns but requires careful tracking of the previous position.
For beginners learning Basic G-code for CNC, sticking to G90 Absolute Positioning is highly recommended to maintain clarity and safety.
The Core 10: Essential Basic G-code for CNC Operators
We have categorized these essential commands into Motion Commands, Setup Commands, and Auxiliary Functions. A thorough grasp of these ten forms the foundation of competent manual programming and code verification.
Part 1: Motion Commands – Controlling Movement
These commands dictate how the tool moves from point A to point B.
1. G00 – Rapid Positioning Motion
- Function: The [G00 rapid move] command instructs the machine to move the tool from its current position to a specified coordinate at the machine’s maximum possible speed.
- Industrial Application: G00 is used exclusively for non-cutting movements. Its purpose is to minimize “air time”—the time the tool spends traveling between cuts without engaging the material. Reducing air time is crucial for cycle time reduction in mass production.
- Critical Safety Warning: G00 moves are dangerous because they happen fast. The machine does not travel in a straight line between points if multiple axes are moving; it often performs a “dog-leg” move, where each axis races to its destination independently. Operators must ensure the path is completely clear of clamps, fixtures, and the workpiece before executing a G00 rapid move. Never use G00 to cut material; the forces involved will break the tool or stall the machine.
- Example Block: N10 G00 X50.0 Y25.0 Z10.0 (Rapidly move to position X50, Y25, Z10 safe height).
2. G01 – Linear Interpolation Motion
- Function: The [G01 linear move] is the workhorse of CNC machining. It moves the tool in a perfectly straight line from the current position to the target coordinate at a specific controlled speed, known as the “Feed Rate.”
- Industrial Application: This is the primary command used for actually cutting material in straight lines, preparing flat surfaces, or cutting profiles. Unlike G00, G01 requires an “F” address (Feed Rate) to tell the machine how fast to move (e.g., millimeters per minute or inches per minute).
- Key Considerations: Calculating the correct feed rate for a G01 move is essential. It depends on the material hardness, cutter type, spindle speed, and depth of cut. An incorrect feed rate during a G01 linear move leads to poor surface finish, rapid tool wear, or tool breakage.
- Example Block: N20 G01 Z-5.0 F1200.0 (Move linearly down to Z-5.0 at a feed rate of 1200 units/minute to begin cutting).
3. G02 (Clockwise) and G03 (Counter-Clockwise) – Circular Interpolation
- Function: These commands allow the machine to cut precise arcs and circles. G02 moves the tool in a clockwise (CW) direction, while G03 moves it counter-clockwise (CCW).
- Industrial Application: Used for cutting circular pockets, drilling large holes via helical interpolation, and creating radiused corners or complex curved profiles on parts like gears or aerospace components.
- Technical Nuance (I, J, K vs. R): To define an arc, the machine needs the endpoint (XY coordinates) and the center of the circle.
- R-method: The simplest way is to specify the Radius (R). E.g., G02 X50. Y0. R25. (Move to X50, Y0 in a CW arc with a radius of 25).
- I, J, K Vectors: This is the more robust, professional method used in complex Basic G-code for CNC. I, J, and K define the distance from the start point of the arc to the center point of the arc along the X, Y, and Z axes, respectively. It requires more understanding of coordinate geometry but is less prone to errors in certain CAM outputs.
- Example Block: N30 G03 X-20.0 Y20.0 R20.0 F800.0 (Cut a counter-clockwise arc ending at X-20, Y20 with a radius of 20).
Part 2: Setup and Plane Commands – Defining the Environment
Before motion begins, the machine needs to know the context of its operation.
4. G17, G18, G19 – Plane Selection
- Function: These commands tell the CNC controller which geometric plane it is currently operating on for circular interpolation (G02/G03) and cutter compensation.
- G17: Selects the XY Plane (Most common for standard 3-axis CNC routers and vertical mills).
- G18: Selects the XZ Plane.
- G19: Selects the YZ Plane.
- Industrial Application: On a standard 3-axis YIHAICNC router, G17 is almost always active by default. However, if you are performing complex 3D surfacing where arcs are executed vertically (like machining a dome shape), the program must switch to G18 or G19. Failing to select the correct plane before attempting a G02/G03 command will result in an alarm or unpredictable movement.
5. G20 (Inches) and G21 (Millimeters) – Unit Selection
- Function: These commands establish the unit of measurement for the entire program. G20 sets the machine to Imperial mode (inches), and G21 sets it to Metric mode (millimeters).
- Industrial Application: This is perhaps the most critical setup command. In a globalized manufacturing environment, shops often handle drawings in both metric and imperial units.
- The Danger of Error: Running a metric program (e.g., move 100mm) on a machine set to G20 mode (move 100 inches) will cause a massive, destructive over-travel. Professional operators always verify that the G20/G21 code at the program header matches the print specifications and the tooling setup. It is best practice to place this command at the very beginning of the Basic G-code for CNC file.
6. G28 – Return to Reference Point (Machine Home)
- Function: G28 instructs the machine to move to its inherent “Home” or reference position. This is a physical location defined by limit switches or absolute encoders on the machine frame.
- Industrial Application: G28 is typically used at the start and end of a program.
- At Start: Ensures the machine is calibrated and knows exactly where it is physically located before establishing a workpiece zero.
- At End: Moving to G28 moves the spindle and gantry safely away from the finished part, allowing for easy loading and unloading of material without obstruction.
- Usage Tip: Often used with an intermediate point to ensure a safe retraction path. E.g., G91 G28 Z0. (Incrementally move Z to zero, then go to Z home). This ensures the tool retracts straight up before moving horizontally to the home position, avoiding collisions with clamps.
Part 3: M-Codes (Miscellaneous Functions) – The Essential Companions
While not technically “G” codes, these M-codes are inseparable from Basic G-code for CNC and are vital for machine control.
7. M03, M04, M05 – Spindle Control
- Function: These commands control the CNC machine’s cutting spindle.
- M03: Starts the spindle rotating in a clockwise (CW) direction. This is the standard direction for most end mills and drill bits. It is almost always followed by an “S” command defining the RPM (e.g., M03 S18000).
- M04: Starts the spindle rotating in a counter-clockwise (CCW) direction. Used for specific tooling like left-hand taps or specialized cutters.
- M05: Stops the spindle rotation.
- Industrial Application: The M03 spindle on command must be executed before the tool enters the material (G01 move). Entering material without a rotating spindle is a guarantee of broken tooling. M05 is used before tool changes or at the end of the program.
8. M08 and M09 – Coolant Control
- Function: Controls the delivery of cutting fluids or air blasts.
- M08: Turns the coolant system ON (Flood coolant, mist coolant, or air blast depending on machine setup).
- M09: Turns the coolant system OFF.
- Industrial Application: Coolant is vital for machining metals or abrasive materials to manage heat generation, lubricate the cutting interface, and evacuate chips. For materials like wood or plastic processed on YIHAICNC routers, M08 might activate a compressed air nozzle to keep the cutting area clear of debris, preventing re-cutting of chips which ruins surface finish.
9. M06 – Tool Change
- Function: Instructs the machine to perform a tool change sequence. It is usually preceded by a “T” code identifying which tool to select.
- Industrial Application: Essential for Automatic Tool Changer (ATC) machines. A block might read T02 M06, telling the machine to swap the current tool for Tool #2 defined in the machine’s library.
- Operator Responsibility: Even with ATC, the operator must ensure that the physical tool loaded in slot #2 matches the tool parameters (diameter, length offset) defined in the CAM software. A mismatch here can lead to crashes during the subsequent G01 linear move.
10. M30 – Program End and Reset
- Function: This command signifies the absolute end of the main CNC program. It stops all machine motion, turns off the spindle and coolant (like M05 and M09), and crucially, resets the program pointer back to the first line of code.
- Industrial Application: M30 is essential for production environments. It prepares the machine for the next cycle immediately. Once the finished part is removed and new material loaded, the operator simply needs to press “Cycle Start” again, as M30 has already rewinded the program.
Manual Programming vs. CAM Software: The Role of Basic G-code Knowledge
In modern manufacturing, very few complex parts are programmed using [manual programming] from scratch. CAM (Computer-Aided Manufacturing) software generates thousands of lines of code automatically from 3D CAD models.
So, why does an operator still need to know Basic G-code for CNC?
- Verification and Safety: Before running a new CAM-generated program, a skilled operator will “dry run” or read through the header code. They check for correct units (G20/G21), safe initial rapid moves (G00 rapid move), and correct spindle speeds (M03 spindle on). They are the final safety barrier.
- On-the-Fly Edits: If a prototype part is slightly out of tolerance, it is far faster for an operator to manually adjust a G01 coordinate or a feed rate directly at the controller than to send it back to the programming department, regenerate the CAM file, and reload it.
- Troubleshooting: When a machine alarms out mid-program, it points to a specific line of G-code. An operator who understands the language can immediately diagnose if it’s an impossible arc move (G02/G03 error), a limit switch trigger during a rapid move, or a syntax error.
Best Practices for Safe CNC Machine Operation and G-Code Verification
Integrating G-code knowledge into your daily workflow is key to maintaining high standards of CNC Machine Operation.
- Always Read the Header: Check the first 10-20 lines of any new program. Verify the G90/G91 status, G20/G21 units, and initial safety height Z moves.
- Use “Single Block” Mode: When running a program for the first time, use the machine’s “Single Block” function. This executes one line of G-code at a time, requiring a button press for the next. This allows the operator to visually verify every move before it happens at full speed.
- Know Your Work Coordinates: Ensure the G-code program’s origin matches where you physically set the XYZ zero point on the material.
- Visualize the Path: Before hitting start, mentally trace the major movements defined by the G00 and G01 commands to ensure they clear clamps and fixtures.
Why YIHAICNC Equipment Integrates Seamlessly with Standard G-Code
At YIHAICNC, we design our CNC equipment to be robust, reliable, and compliant with industry standards. Our controllers are fully compatible with standard ISO G-code generated by all major CAM software packages. This ensures that the Basic G-code for CNC commands outlined in this guide work exactly as expected on our machines, providing a predictable and high-performance operator experience.
Whether you are running high-speed production using optimized G00 rapids or requiring intricate precision with G02/G03 circular interpolation, YIHAICNC routers provide the mechanical rigidity and control system accuracy to execute the code flawlessly.
FAQ
1. What is the most important G-code for beginners?
G01 (linear interpolation) is the most essential because it controls actual cutting. Master G00, G01, G90, and M03 first.
2. What is the difference between G90 and G91?
G90 (absolute) moves the tool to fixed coordinates from a zero point. G91 (incremental) moves the tool a specific distance from its current position. Beginners should use G90 for safety.
3. Can I use G-code from any CAM software on YIHAICNC routers?
Yes. YIHAICNC routers use standard ISO G-code compatible with all major CAM software like Mastercam, PowerMill, Vectric, and Fusion 360.
4. How do I avoid crashing my CNC machine when testing new G-code?
Always use “Single Block” mode and “Dry Run” (air cutting) first. Verify your G20/G21 units and G90/G91 modes before the first cut.
5. Where can I download a free G-code cheat sheet?
Scroll up or click the button above to download our free printable PDF cheat sheet with all essential G-codes and M-codes.
Conclusion
While technology advances, the fundamental language of manufacturing remains rooted in G-code. For B2B manufacturers, ensuring your operators possess a strong grasp of these 10 essential Basic G-code for CNC commands is not just about technical skill—it’s about safeguarding your investment, maximizing uptime, and ensuring consistent production quality. By mastering the basics of motion commands like the G01 linear move, setup codes, and critical functions like M03 spindle on, your team gains the confidence and competence to handle any machining challenge.