The Complete Guide to G-Code for 3D Printing: From Basics to Manual Changes

On this page

Introduction: The Hidden Language

What is g code in 3d printing? Simply put, G-code is the basic language that tells your 3D printer exactly what to do. It's a group of text-based instructions that controls every movement, every temperature change, and every action the machine takes to turn a digital file into a real object.

Think of it as a very detailed recipe for your printer. Your 3D model is the picture of the finished cake, but the G-code is the step-by-step instructions: "heat the oven to 210°C," "move the mixer head to this exact spot," "add 0.05mm of frosting," and so on, repeated thousands of times. Understanding this language gives you a deeper level of control and problem-solving abilities, changing you from a simple user into a true operator of your machine.

In This Guide

In this complete guide, we will cover:

  • The role of G-code in the 3D printing process.
  • How your slicer software creates a G-code file.
  • A breakdown of the most common and useful commands.
  • How to read and understand a G-code file.
  • Real examples of when and how to edit G-code for better prints.

The Bridge Between Worlds

G-code is the important link in the chain that connects your digital design to a physical print. It doesn't appear from nowhere; it's the result of a specific and essential process. Understanding this process helps explain where G-code comes from and why it's so important. The journey from idea to object almost always follows these three main steps.

The Standard Process

  1. The 3D Model (.STL, .3MF, .OBJ): This is your starting point—the digital blueprint of your object. Created in CAD software or downloaded from a website, this file describes the surface shape of your model, basically a collection of connected triangles. However, it contains no information about how to actually print it. It's the "what," not the "how."

  2. The Slicer (The Translator): This is where the magic happens. You import your 3D model into a slicer program. Here, you define all your print settings—options like layer height, print speed, nozzle and bed temperature, and the internal fill structure. The slicer's main job is to take your 3D model, "slice" it into hundreds or thousands of thin horizontal layers, and then calculate the exact toolpaths needed to draw each layer.

  3. The G-Code File (.gcode): The final output of the slicer is a plain text file with a .gcode extension. This file contains the step-by-step list of commands that your printer's firmware can read and execute. Every line of G-code corresponds to a specific action—heating the nozzle, moving to a coordinate, or pushing out filament. You then transfer this file to your printer via an SD card, USB, or a network connection to begin the print.

How G-Code is Born

To truly understand G-code, we need to look inside the "black box" of the slicer. It's not just a simple conversion; it's a complex process of calculation and translation based on your model's shape and your chosen settings. Here's a basic breakdown of how a slicer thinks and generates a G-code file.

Slicing into Layers

The very first thing the slicer does is cut your continuous 3D model into a series of separate 2D layers. The thickness of each slice is determined by the layer height setting you choose. A smaller layer height means more layers and a more detailed, smoother final print, but it will also result in a much longer print time and a larger G-code file. This process is like creating a stack of digital cross-sections of your object from bottom to top.

Creating Toolpaths

For each individual layer, the slicer must then calculate the exact path the print head's nozzle will follow to draw it. This is far more complex than just tracing an outline. The slicer breaks down the toolpath creation into several different parts:

  • Perimeters (or Walls): These are the outlines of your model on that specific layer. The slicer calculates one or more passes to create the solid outer surface of the print.
  • Infill: To save time and material, most prints are not 100% solid. The slicer creates a pattern (like a grid, honeycomb, or gyroid) inside the perimeters based on your chosen infill density. This internal structure provides support without using extra filament.
  • Top and Bottom Layers: To ensure the object has a solid, sealed surface, the slicer creates solid layers for the very top and bottom of the print.
  • Supports: If your model has steep overhangs or bridges where plastic would otherwise be printed in mid-air, the slicer creates temporary support structures. These have their own special toolpaths and are designed to be removed after the print is complete.

Translating into Commands

This is the final and most important step. The slicer takes all the calculated geometric paths—perimeters, infill, supports—and translates them, along with your other settings like temperature, fan speed, and print speed, into a step-by-step list of G-code commands. Every move is converted into a G1 command with X, Y, and Z coordinates. Every instruction to push out plastic becomes an E value. Every temperature setting becomes an M command.

This entire concept of using coded instructions for machine control isn't new. G-code's history traces back to the 1950s for controlling CNC (Computer Numerical Control) mills and lathes. Its principles were later adapted for additive manufacturing (3D printing), with standardization formats historically influenced by organizations like the National Institute of Standards and Technology (NIST) to ensure compatibility between machines.

Understanding the Language

At first glance, a G-code file can look like a scary wall of text and numbers. However, it's built on a consistent and logical structure. The commands can be broadly grouped into two main types, and knowing just a handful of the most common ones will allow you to understand most of any G-code file.

G-Commands and M-Commands

  • G-Commands (Geometrical Codes): These commands are the "action words" of the language. They mainly control the motion and actions of the tool head. They tell the printer where to move, how to move (e.g., in a straight line or an arc), and what action to perform, like pushing out material. The command G1 for linear movement is the most common command in any 3D printing G-code file.

  • M-Commands (Miscellaneous Codes): These commands are the "setup instructions" or "modifiers." They control the printer's extra hardware and settings that aren't directly related to movement. This includes setting temperatures, controlling fan speeds, waiting for conditions to be met, or displaying messages on the screen.

Common 3D Printing Commands

The best way to learn these commands is with a quick-reference table. Here are some of the most essential G-code and M-code commands you'll encounter in 3D printing as of 2025.

Command Name What It Does Example Usage
G0/G1 Linear Move Moves the print head to a specified X, Y, Z coordinate. G0 is a rapid, non-extruding move, while G1 is a controlled move that can also extrude. In modern 3D printing firmware, they are often treated identically. G1 X10 Y20 E5 F1500 (Move to X10, Y20, extrude 5mm of filament, at a feedrate of 1500 mm/min)
G28 Auto Home Moves all axes (X, Y, and Z) to their physical end-stops to establish the machine's zero position, or origin. G28 (Home all axes) or G28 X Y (Home only X and Y axes)
G90 Absolute Positioning Instructs the printer that all following coordinates are absolute, meaning they are relative to the origin (0,0,0) established by G28. This is the standard mode for 3D printing. G90
G91 Relative Positioning Instructs the printer that all following coordinates are relative to its last known position. Useful for specific routines but less common for the main print job. G91
G92 Set Position Allows you to manually define the current position of one or more axes without actually moving them. Most commonly used to reset the extruder's position counter. G92 E0 (Resets the extruder's extruded distance to zero)
M104 Set Hotend Temp Sets the target temperature for the hotend (nozzle) but does not pause the program. The printer continues to the next command immediately. M104 S210 (Set hotend target to 210°C)
M109 Set Hotend Temp (Wait) Sets the target temperature for the hotend and, importantly, waits for the nozzle to reach that temperature before executing the next command. M109 S210 (Set hotend to 210°C and wait)
M140 Set Bed Temp Sets the target temperature for the heated print bed but does not wait. M140 S60 (Set bed target to 60°C)
M190 Set Bed Temp (Wait) Sets the target temperature for the heated bed and pauses the program until the bed reaches that temperature. M190 S60 (Set bed to 60°C and wait)
M106 Set Fan Speed Sets the speed of the part cooling fan. The speed is typically defined by a value from 0 (off) to 255 (full speed). M106 S255 (Turn fan on to full speed)
M107 Fan Off A simple command to turn the part cooling fan completely off. It is equivalent to M106 S0. M107

Reading a G-Code File

Knowing the commands is one thing; seeing them in context is another. Let's bridge the gap between theory and practice by analyzing a short, typical snippet from the beginning of a G-code file. This is the "start-up script" that prepares the printer for the main job.

A Sample Start-Up Script

You can open any .gcode file in a basic text editor like Notepad or TextEdit to see this for yourself.

; G-code generated by a generic slicer for a standard FDM printer in 2025
M190 S60 ; Wait for bed to reach 60°C
M109 S205 ; Wait for hotend to reach 205°C
G28 ; Home all axes
G92 E0 ; Reset extruder position
G1 Z2.0 F3000 ; Move Z up a little
G1 X5.1 Y20 Z0.3 F5000.0 ; Move to start position
G1 X5.1 Y200.0 Z0.3 F1500.0 E15 ; Draw the first line (skirt/purge line)
G92 E0 ; Reset extruder again after purge

What It All Means

Let's break this down line by line to see what the printer is being told to do.

  1. ; G-code generated...: Any line beginning with a semicolon (;) is a comment. The printer completely ignores these lines. They are inserted by the slicer for human readability, helping you navigate the file and understand what's happening at different stages.
  2. M190 S60: This command tells the printer to heat the build plate to 60°C and, importantly, to wait until it gets there before proceeding.
  3. M109 S205: Similarly, this command instructs the hotend to heat to 205°C and wait for it to stabilize at that temperature. These two "wait" commands ensure the printer is at the correct thermal state before starting any movements.
  4. G28: This executes the auto-home sequence. The print head moves until it triggers the end-stop switches on the X, Y, and Z axes, establishing its (0,0,0) origin point.
  5. G92 E0: This sets the extruder's internal counter to zero. It's like zeroing a digital scale before you weigh something. It ensures that all future extrusion amounts are measured from a known starting point.
  6. G1 Z2.0 F3000: This is a linear move (G1). It lifts the Z-axis to a height of 2.0mm at a feedrate (F) of 3000 mm/minute, ensuring the nozzle is safely clear of the bed.
  7. G1 X5.1 Y20 Z0.3 F5000.0: Another linear move. The nozzle moves rapidly to the X/Y coordinates where the first line will begin, while also lowering to the first layer height (Z0.3).
  8. G1 X5.1 Y200.0 Z0.3 F1500.0 E15: This is the first printing move. The nozzle moves in a straight line along the Y-axis (from Y20 to Y200) while simultaneously extruding (E) 15mm of filament. This is often a purge line or skirt printed at the edge of the bed to prime the nozzle.
  9. G92 E0: The extruder is reset to zero again after the purge line to ensure the print itself starts with a precise extrusion count.

When and How to Edit

While you will almost never write G-code from scratch, knowing how to make small, targeted edits can be a powerful problem-solving tool. It can save you from having to re-slice an entire multi-hour print just to make a tiny adjustment. Here are a couple of practical, real-world scenarios where manual editing is a lifesaver.

Scenario 1: The Z-Offset Fix

  • Problem: You start a print, and you notice the first layer isn't sticking because the nozzle is just a fraction of a millimeter too high. Or, conversely, the nozzle is grinding into the bed because it's too low. Your machine's Z-offset setting is slightly off for this specific print.
  • Solution with G-Code: You can quickly adjust the starting Z-height for the first layer without re-slicing.
    1. Open your .gcode file in a simple text editor.
    2. Use the find function (Ctrl+F or Cmd+F) to search for the very first G1 Z move that happens after the G28 homing command. This will be a small value, like G1 Z0.2 or G1 Z0.3, representing your initial layer height.
    3. To lower the nozzle for better adhesion, decrease this value slightly. For example, change G1 Z0.2 to G1 Z0.18.
    4. To raise the nozzle to prevent squishing, increase the value. For example, change G1 Z0.2 to G1 Z0.22.
    5. Save the file and start the print. This tiny edit can be the difference between a failed print and a perfect first layer.

Scenario 2: Mid-Print Temp Change

  • Problem: You're printing a model like a figurine with a thin sword or a building with a narrow spire at the top. As the printer gets to these small, fast layers, they don't have enough time to cool before the next layer is applied. This results in a melty, blobby, and undefined finish.
  • Solution with G-Code: You can manually insert a command to lower the temperature once the print reaches that problematic section.
    1. First, open the G-code file in your slicer's preview or viewer mode. Scrub through the layers to identify the exact layer number where the narrow section begins (e.g., Layer 254).
    2. Now, open the .gcode file in your text editor.
    3. Search for the comment that marks the beginning of that layer. It will look like ;LAYER:253 (the layer count often starts at 0, so search for the layer before your target).
    4. Directly below that comment line, insert a new line with a temperature change command: M104 S195 ; Lower temp for spire.
    5. Save the file. Now, when the printer finishes layer 253 and starts layer 254, it will receive the new command to lower its temperature to 195°C (or whatever you choose), giving those small layers a better chance to cool and solidify properly, dramatically improving quality.

Your Printer's Language

You've now journeyed from the basic definition of what is g code in 3d printing to the details of its creation and the practicality of its application. We've seen how it forms the essential bridge in the printing workflow, decoded its core commands, and learned how to make targeted edits to solve real-world problems.

You don't need to be a programmer to master 3D printing, and you'll rarely, if ever, need to write a G-code file from scratch. But understanding what it is, how to read it, and where to make a small change is a superpower. It elevates your skills, deepens your understanding of the process, and gives you the confidence to take full control of your machine. You now speak your printer's language.

Back to blog