Introduction to G-Code — The Fundamental Language of CNC Machines
Introduction
G-code is the universal language that controls CNC (Computer Numerical Control) machines. Whether you’re working with a CNC mill, lathe, plasma cutter, laser engraver, or even a 3D printer, the machine ultimately follows a set of G-code commands to move, cut, drill, shape, or engrave material.
Every move the machine makes—every cut, every arc, every plunge—is defined by numbers and letters in a structured code. Learning G-code gives you a superpower: the ability to tell a machine exactly what to do.
This guide is designed for beginners and provides:
-
Clear explanations
-
Practical examples
-
Diagrams (text-based)
-
Tables of essential commands
-
Mathematical formulas
-
A complete sample program
-
A starter CNC project
By the end of this article, you’ll understand how G-code works, how to read it, and how to write simple programs with confidence.
1. What Is G-Code?
G-code (also known as RS-274) is the programming language used to instruct CNC machines on how to move and operate. Each line of a G-code program tells the machine what to do next—move in a direction, cut material, activate the spindle, or perform a drilling cycle.
Every line of G-code is called a block, and machines read these blocks from top to bottom.
A typical G-code block might look like this:
This means:
-
Move in a straight line
-
To position X=20, Y=10
-
At a feed rate of 100 mm/min
Even complex CNC operations are built from hundreds or thousands of these small steps.
2. Structure of a G-Code Block
Most G-code lines follow a predictable structure.
Let’s analyze a typical block:
Meaning of Each Element
| Code | Meaning |
|---|---|
| N10 | Block number (optional) |
| G01 | Linear interpolation (cutting move) |
| X20 | Final X position (20 mm) |
| Y10 | Final Y position (10 mm) |
| F100 | Feed rate (100 mm/min) |
Not all lines contain all letters. Some may contain only one instruction:
Others may simply move the tool:
3. Modal vs. Non-Modal Commands
To understand G-code correctly, you must know the difference between modal and non-modal commands.
✔ Modal Commands (Stay Active Automatically)
When a modal command is used, it stays active until changed.
Example:
Here, G01 remains active for the next lines, so both X10 and Y20 are linear cutting moves.
✔ Non-Modal Commands (Used Once Only)
A non-modal command applies only to the line it appears on.
Example:
G53 moves in machine coordinates only for this line.
Understanding modals is critical—using the wrong modal can cause a crash.
4. Essential G-Codes for Beginners
These are the basic codes you will encounter in nearly every program.
Motion Commands
| Code | Description |
|---|---|
| G00 | Rapid move (non-cutting) |
| G01 | Linear cutting move |
| G02 | Clockwise arc move |
| G03 | Counterclockwise arc move |
Setup Commands
| Code | Description |
|---|---|
| G17 | XY plane |
| G18 | ZX plane |
| G19 | YZ plane |
| G20 | Use inches |
| G21 | Use millimeters |
Positioning Modes
| Code | Description |
|---|---|
| G90 | Absolute positioning |
| G91 | Incremental positioning |
These foundational commands make up around 80% of all CNC programs.
5. Coordinate Systems Explained
CNC machines don’t work with just one coordinate system—they use several.
Machine Coordinates (G53)
These are fixed and define the machine’s physical limits.
Work Offsets (G54–G59)
These define where the part zero is located.
If your part zero is located 100 mm right and 50 mm forward from machine zero, using:
tells the machine:
Work offsets are essential for accuracy and consistent part setup.
6. G00 and G01 — Rapid vs. Cutting Moves
G00 Rapid Movement
Used for fast, non-cutting moves.
The machine moves as fast as possible, usually diagonally, to reach the position quickly.
G01 Linear Cutting
Used when removing material.
The feed rate (F150) determines the cutting speed.
Important safety rule:
Never rapid into the material! Always retract first:
7. Arc Movements — G02 and G03
Arcs can be programmed using:
✔ Method 1: Radius (R)
✔ Method 2: Center Offsets (I/J)
Where:
-
I = X center offset
-
J = Y center offset
Arc Radius Verification Formula
To ensure the arc is valid:
If the numbers don’t match, the controller will give an arc error.
8. Feed Rate, Speed, and Cutting Equations
Choosing correct cutting parameters is essential.
Spindle Speed Formula

Feed Rate Formula

These formulas help prevent:
-
Tool breakage
-
Overheating
-
Poor surface finish
9. Putting It All Together — Complete Beginner G-Code Program
Below is a simple CNC milling program that cuts a 40 × 20 mm rectangle 2 mm deep.
This small example demonstrates the flow of a typical CNC program.
10. Common Beginner G-Code Mistakes (and How to Avoid Them)
Mistake 1: Forgetting to Retract Before Rapids
Always move up before moving sideways.
Mistake 2: Wrong Feed Rate
-
Too fast → tool breaks
-
Too slow → tool overheats
Mistake 3: Mixing G90 and G91
Absolute vs. incremental confusion can cause machine collisions.
Mistake 4: Incorrect arc values
Arcs need correct radius or center offset.
Mistake 5: Forgetting to start the spindle
Moving into the material without spindle rotation = instant tool break.
11. Mini Project for Beginners: Engrave Your Name
Engraving your name is a great beginner exercise.
Steps to follow:
-
Set your work offset using G54
-
Choose the cutting tool
-
Convert letters into coordinates
-
Use G01 for straight lines
-
Use G02/G03 for curves
-
Set proper speeds and feeds
-
Run the program slowly first (simulation recommended)
Example: Engraving the letter “A”
Doing this for each letter builds strong understanding of coordinates and movement.
Conclusion
G-code is the foundation of all CNC machining. Although it may look intimidating at first, it follows a clear and logical structure. Once you understand the basic commands—G00, G01, G02, G03, G90, G91—you can control any CNC machine with confidence.
By learning coordinate systems, feed rate formulas, and common modal commands, you become capable of writing your own programs, troubleshooting machine behavior, and understanding movements generated by CAM software.
This introduction gives you the essential base for more advanced topics such as:
-
Drilling cycles (G81–G89)
-
Tool compensation (G41/G42)
-
Loops and macro variables
-
Probing
-
Automated multi-tool operations
Master G-code, and you unlock the full power of CNC machining.

