Introduction to G-Code — The Fundamental Language of CNC Machines

Introduction to G-Code

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:

G01 X20 Y10 F100

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:

N10 G01 X20 Y10 F100

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:

M03 S1200 (Spindle ON at 1200 RPM)

Others may simply move the tool:

G00 X0 Y0

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:

G01
X10
Y20

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 X0 Y0

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:

G54

tells the machine:

Part Position = Machine Position + Work Offset

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.

G00 X0 Y0 Z10

The machine moves as fast as possible, usually diagonally, to reach the position quickly.

G01 Linear Cutting

Used when removing material.

G01 X50 Y30 F150

The feed rate (F150) determines the cutting speed.

Important safety rule:
Never rapid into the material! Always retract first:

G00 Z5

7. Arc Movements — G02 and G03

Arcs can be programmed using:

Method 1: Radius (R)

G02 X30 Y20 R10

Method 2: Center Offsets (I/J)

G03 X30 Y20 I5 J0

Where:

  • I = X center offset

  • J = Y center offset

Arc Radius Verification Formula

To ensure the arc is valid:

R² = (ΔX)² + (ΔY)²

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

Spindle Speed Formula G-Code
Spindle Speed Formula

Feed Rate Formula

Feed Rate Formula G-Code
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.

%
(RECTANGLE CUT)
G21 (Units: mm)
G90 (Absolute position)
G17 (XY plane)
G00 Z5 (Lift tool)
G00 X0 Y0 (Move to start)
M03 S1200 (Spindle on)
G01 Z-2 F80 (Plunge)
G01 X40 F120 (Cut right)
G01 Y20 (Cut up)
G01 X0 (Cut left)
G01 Y0 (Back to start)
G00 Z5 (Retract)
M05 (Spindle off)
G00 X0 Y0 (Home)
M30 (End)
%

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:

  1. Set your work offset using G54

  2. Choose the cutting tool

  3. Convert letters into coordinates

  4. Use G01 for straight lines

  5. Use G02/G03 for curves

  6. Set proper speeds and feeds

  7. Run the program slowly first (simulation recommended)

Example: Engraving the letter “A”

G01 X0 Y0
G01 X10 Y20
G01 X20 Y0
G01 X5 Y10
G01 X15 Y10

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.

Leave a Reply

Your email address will not be published. Required fields are marked *