What is G-Code? The Complete Beginner's Guide
If you've ever watched a CNC machine carve a perfect pocket into aluminum, or seen a 3D printer lay down a flawless first layer, you've witnessed G-Code in action. G-Code is the universal language that bridges the gap between a digital design and a physical machine. It is arguably the most important programming language that most engineers have never formally studied โ yet it quietly runs millions of machines around the world every single day.
This guide is written for complete beginners. By the end, you'll understand what G-Code is, how it works, how to read a real file, and how tools like GCodex let you visualize and verify your code before running it on a real machine.
A Brief History of G-Code
G-Code (formally known as RS-274) was developed in the 1950s and 1960s at MIT under a U.S. Air Force contract. The goal was to automate complex metal-cutting operations on numerically controlled (NC) machines. Early NC machines used punched paper tape to feed instructions โ and the G-Code format was designed to be simple enough to encode on that tape, yet expressive enough to describe any machining operation.
Over the following decades, the language was standardized by organizations like the Electronic Industries Alliance (EIA) and later ISO. Today, while many dialects and extensions exist โ Fanuc, Haas, Siemens, Marlin, Klipper โ the core vocabulary of G-Code has remained remarkably stable for over 60 years.
Anatomy of a G-Code Command
Every G-Code instruction follows a consistent, readable structure. Each command lives on its own line and consists of one or more words, where each word is a letter (called the address) followed by a number (called the parameter).
G1 X50.0 Y30.0 Z-2.5 F800
Breaking this down:
- G1 โ The motion command: "move in a straight line at the programmed feed rate."
- X50.0 โ Move the X axis to position 50.0 mm.
- Y30.0 โ Move the Y axis to position 30.0 mm.
- Z-2.5 โ Move the Z axis to -2.5 mm (below the datum โ i.e., cutting depth).
- F800 โ Feed rate: 800 mm/min.
Multiple parameters on a single line are processed simultaneously โ meaning the machine moves all three axes at once, arriving at the target position together. That's what creates smooth diagonal cuts rather than stair-stepped motion.
Tip: G-Code is not case-sensitive for addresses (G1 = g1), but by convention all addresses are written in uppercase. Numbers after decimal points are optional for whole values โ X50 and X50.0 are identical.
Coordinate Systems
G-Code operates in a three-dimensional Cartesian coordinate space โ the same X, Y, Z system you learned in high school geometry. The origin (0, 0, 0) can be defined by the programmer using work offsets (like G54 through G59 on CNC mills, or G92 on 3D printers).
Absolute vs Incremental Positioning
This is one of the most important concepts in G-Code:
- G90 โ Absolute mode: All coordinates are measured from the current work origin. If you command
X50, the machine moves to X = 50 mm from zero, regardless of where it currently is. - G91 โ Incremental mode: Coordinates are measured from the current position.
X10means "move 10 mm in the positive X direction from wherever you are now."
Most programs default to G90 (absolute) and switch to G91 only for specific operations like drilling cycles or relative homing moves. Confusing the two is a very common source of crashes โ more on that in our Troubleshooting Guide.
Key G-Codes You Must Know
- G0 โ Rapid positioning. Moves the machine to a position at maximum speed. No cutting โ used for travel moves between operations.
- G1 โ Linear interpolation. Moves in a straight line at a controlled feed rate. Used for all cutting moves.
- G2 โ Clockwise circular arc. Draws an arc from the current position in the clockwise direction.
- G3 โ Counter-clockwise arc. Same as G2 but in the opposite direction.
- G28 โ Return to home position. Tells the machine to move to its homing switches or defined reference point.
- G90 โ Absolute positioning mode (see above).
- G91 โ Incremental positioning mode (see above).
- G92 โ Set position. Redefines the current position as the specified coordinate without moving the machine.
- G17/G18/G19 โ Selects the active arc plane (XY, XZ, or YZ) for circular interpolation.
Key M-Codes: Machine Control
While G-codes govern motion, M-codes control auxiliary machine functions โ spindle, coolant, program flow, temperature (for 3D printers), and more.
- M3 โ Spindle ON (clockwise). Used on CNC mills to start the cutting tool rotating.
- M5 โ Spindle OFF.
- M8 โ Coolant ON. Activates flood or mist coolant to cool the cutting tool.
- M9 โ Coolant OFF.
- M30 โ Program end and rewind. Signals the end of a program and returns to the beginning.
- M104 โ Set hotend temperature (3D printers). Example:
M104 S200sets the hotend to 200ยฐC without waiting. - M109 โ Set hotend temperature and wait. The printer pauses until the target temperature is reached.
- M140 โ Set bed temperature (3D printers).
- M190 โ Set bed temperature and wait.
G-Code in 3D Printing
3D printers are essentially CNC machines that extrude molten plastic (or other materials) instead of cutting. Their G-Code is generated by slicing software โ Cura, PrusaSlicer, Bambu Studio โ which converts a 3D model (STL or 3MF file) into thousands of G-Code lines describing every movement, every temperature change, and every extrusion amount.
A 3D printing G-Code file typically starts with a header block that sets temperatures, homes the axes, levels the bed, and primes the nozzle. Then the main body describes the print layer by layer โ each layer is a series of travel moves (G0) and extrusion moves (G1 with an E parameter for extruder position). A typical FDM print of a moderate-sized object might contain 100,000 to several million lines of G-Code.
M109 S210 ; Wait for hotend to reach 210ยฐC
M190 S60 ; Wait for bed to reach 60ยฐC
G28 ; Home all axes
G29 ; Auto bed leveling
G1 Z0.2 F300 ; Move to first layer height
G1 X10 Y10 E0 F1500 ; Start position
G1 X50 Y10 E5.2 F2000 ; Extrude first line
G-Code in CNC Milling
In CNC milling, G-Code is generated by CAM (Computer-Aided Manufacturing) software such as Fusion 360, Mastercam, or Solidworks CAM. The programmer defines toolpaths โ roughing passes, finishing passes, drilling cycles, contours โ and the CAM software posts out G-Code optimized for the specific machine controller (Fanuc, Haas, Siemens, etc.).
CNC G-Code tends to be more complex than 3D printing G-Code because it must account for tool diameter offsets, cutter compensation, work coordinate systems, and multi-axis simultaneous motion. A 5-axis machining program for an aerospace part might include complex rotational moves using A, B, and C axis commands alongside the standard X, Y, Z.
How to Read a G-Code File
G-Code files are plain text โ you can open any .gcode, .nc, .tap, or .cnc file in a basic text editor. Each line is one instruction. Lines beginning with a semicolon (;) are comments and are ignored by the machine. Parentheses (like this) are also used for comments on some controllers.
When reading a file manually, start at the top and follow the flow: initialization, warm-up (for printers), home, then the main cutting/printing loop. Look for coordinate changes to understand where the machine is travelling, and watch the Z axis to understand depth progression.
Visualizing G-Code with GCodex
Reading G-Code as text is one thing โ but seeing the toolpath is infinitely more useful. GCodex renders your G-Code file as an interactive 3D/2D visualization, right in your browser. You can rotate, zoom, inspect individual layers, and check for over-travels or unexpected moves โ all without sending a single command to a real machine.
This is especially valuable when you receive a G-Code file from a CAM system you're unfamiliar with, or when debugging a sliced file that's printing incorrectly. Drop your file into GCodex, scan the toolpath visually, and catch problems before they damage your machine or waste material.
GCodex