Table of Contents
- What Is G-Code?
- G-Code File Structure
- Essential G-Code Commands for 3D Printing
- Start and End G-Code Scripts
- Reading and Understanding Slicer Output
- Modifying G-Code: Practical Examples
- G-Code Viewers and Simulators
- Advanced G-Code Commands
- G-Code Across Different Firmware
- FAQ
Every 3D print you make goes through a transformation: your 3D model (STL or OBJ) is sliced into thousands of thin layers, and each layer is converted into a sequence of instructions that tell the printer exactly where to move, how fast to move, at what temperature to print, and how much filament to push. These instructions are written in G-Code — a simple, human-readable programming language that has been the backbone of CNC machining and additive manufacturing for decades.
Understanding G-Code is not just for engineers. Even a hobbyist maker who knows the basics can troubleshoot print failures more effectively, customize slicer start/end scripts, fine-tune filament behavior, and even run prints without a slicer altogether. This guide gives you everything you need to go from G-Code beginner to confident reader and modifier.
What Is G-Code?
G-Code (RS-274) is a standardized programming language used to control CNC (Computer Numerical Control) machines — including 3D printers, laser cutters, CNC mills, and lathes. The “G” stands for “geometric” — it originally described geometric movements. The companion “M-Code” (miscellaneous code) handles machine-specific functions like turning fans on, setting temperatures, or enabling motors.
In 3D printing, a slicer like PrusaSlicer, Cura, or Bambu Studio takes your 3D model and generates a G-Code file (usually with a .gcode extension) that the printer’s firmware (Marlin, Klipper, RepRapFirmware) reads and executes line by line.
A simple G-Code file for a 3D print typically contains:
- 2,000 to 2,000,000+ lines for complex parts
- Startup commands (homing, bed leveling, temperature pre-heat)
- Layer-by-layer movement and extrusion commands
- Fan speed changes, temperature adjustments mid-print
- Shutdown commands (motors off, cooling fan on, park head)
G-Code File Structure
Let us look at the anatomy of a typical 3D printing G-Code file.
Comments
Lines starting with a semicolon ; are comments — ignored by the printer but useful for humans reading the file. Slicers insert comments to mark layer numbers, feature types, print settings, and more.
; Layer 1 of 120
; Feature: Perimeter
; Width: 0.45mm, Height: 0.20mm
Parameter Structure
Each G-Code line consists of a command letter + number, followed by optional parameters:
G1 X120.5 Y85.3 Z0.2 E2.345 F3000
G1— Linear move commandX120.5 Y85.3— Move to X=120.5mm, Y=85.3mmZ0.2— Set Z height to 0.2mm (layer 1)E2.345— Extruder position (filament pushed)F3000— Feedrate (speed) in mm/min = 50mm/s
Essential G-Code Commands for 3D Printing
G0 and G1 — Move Commands
G0 is a rapid (non-print) move — the print head travels from one point to another without extruding filament. Used for travel moves between print regions.
G1 is a controlled linear move — used for printing. Includes X, Y, Z coordinates and the E (extruder) value.
G0 X50 Y50 F6000 ; Travel move to X50, Y50 at 100mm/s
G1 X100 Y50 E5.2 F3000 ; Print move, extruding 5.2mm of filament
G28 — Home All Axes
Moves the print head to the home position (endstops) on all axes. Always included at the start of every print.
G28 ; Home all axes (X, Y, Z)
G28 X Y ; Home only X and Y axes
G29 — Automatic Bed Leveling
Triggers the printer’s auto-leveling probe (BLTouch, CR Touch, etc.) to measure bed flatness across a grid.
G29 ; Run bed leveling mesh
G29 L1 ; Load saved mesh (Marlin UBL)
G90 and G91 — Absolute vs Relative Positioning
G90 ; Absolute positioning — coordinates from machine origin
G91 ; Relative positioning — coordinates from current position
G92 — Set Position
Tells the printer to treat the current position as a specific coordinate — commonly used to reset the extruder position to zero.
G92 E0 ; Set extruder position to 0 (reset before print)
M104 and M109 — Set Hotend Temperature
M104 S200 ; Set hotend to 200°C (don't wait)
M109 S200 ; Set hotend to 200°C and WAIT until reached
M104 sends the temperature command and moves on immediately. M109 pauses execution until the target temperature is reached — important before starting actual printing.
M140 and M190 — Set Bed Temperature
M140 S60 ; Set bed to 60°C (don't wait)
M190 S60 ; Set bed to 60°C and WAIT
M106 and M107 — Fan Control
M106 S255 ; Part cooling fan at 100% (255 = max)
M106 S128 ; Part cooling fan at 50%
M107 ; Fan off
M82 and M83 — Extruder Mode
M82 ; Absolute extrusion mode
M83 ; Relative extrusion mode
M84 — Disable Stepper Motors
M84 ; Disable all steppers (motors free to move by hand)
Bambu Lab PLA Filament Grey – 1.75mm
Reliable Bambu PLA for calibration prints and G-Code testing. Consistent diameter ensures predictable extrusion when tuning your start/end scripts.
Start and End G-Code Scripts
The start and end G-Code scripts in your slicer run before and after the print respectively. They are the most commonly customized part of the G-Code pipeline.
Typical Start G-Code (Ender 3 / Marlin)
G28 ; Home all axes
G29 ; Auto bed leveling
M190 S{first_layer_bed_temperature[0]} ; Wait for bed temp
M109 S{first_layer_temperature[0]} ; Wait for hotend temp
G92 E0 ; Reset extruder
G1 Z2.0 F3000 ; Raise Z 2mm
G1 X0.1 Y20 Z0.3 F5000 ; Move to purge line start
G1 X0.1 Y200 Z0.3 F1500 E15 ; Draw purge line
G1 X0.4 Y200 Z0.3 F5000 ; Move slightly right
G1 X0.4 Y20 Z0.3 F1500 E30 ; Second purge line
G92 E0 ; Reset extruder
G1 Z2.0 F3000 ; Lift Z
; Print starts here
The purge line primes the nozzle with fresh, consistent filament before your print begins — preventing the weak first extrusion artifact (the “blob” or “gap” at the print start).
Typical End G-Code
G91 ; Relative positioning
G1 E-2 F2700 ; Retract 2mm
G1 E-2 Z0.2 F2400 ; Retract more + raise Z
G1 X5 Y5 F3000 ; Wipe nozzle
G1 Z10 ; Raise Z 10mm
G90 ; Absolute positioning
G1 X0 Y{machine_depth} ; Present print at front
M106 S0 ; Fan off
M104 S0 ; Hotend off
M140 S0 ; Bed off
M84 X Y E ; Disable motors (keep Z)
; End of print
Reading and Understanding Slicer Output
Open any .gcode file in a text editor. Slicers add extensive comments that help you navigate. Here is what you will typically see:
Layer Markers
;LAYER_CHANGE
;Z:0.2
;HEIGHT:0.2
These mark the start of each new layer. The Z coordinate tells you the layer height, and the HEIGHT value is the layer thickness.
Feature Type Markers (PrusaSlicer)
;TYPE:Perimeter
;TYPE:External perimeter
;TYPE:Infill
;TYPE:Bridge infill
;TYPE:Support material
These help you identify which part of the print each section of G-Code corresponds to.
Print Time and Filament Usage (Header Comments)
; estimated printing time (normal mode) = 2h 34m 20s
; filament used [mm] = 4235.6
; filament used [cm3] = 12.8
; filament used [g] = 15.5
; filament cost = 0.10
These are calculated by the slicer and appear at the top of the file. They help you plan material purchases and print scheduling.
Modifying G-Code: Practical Examples
Example 1: Add a Temperature Change Mid-Print
Suppose you want to print the first 20 layers at 205°C (for adhesion) and the rest at 195°C (for surface quality). Find the layer marker for layer 21 and insert:
M104 S195 ; Drop nozzle temp after layer 20
Example 2: Add a Filament Change Pause
Find the G-Code line for the layer where you want to change filament color and insert:
M600 ; Filament change (Marlin)
; Printer parks, beeps, waits for you to swap filament and resume
Example 3: Slow Down for Bridges
If your slicer is not handling bridge detection well, locate the bridge sections (look for ;TYPE:Bridge infill) and reduce the feedrate:
;TYPE:Bridge infill
M220 S40 ; Set print speed to 40% of base speed for bridges
Bambu Lab ABS Filament Black – 1.75mm
Professional-grade black ABS filament for functional parts. Practice G-Code temperature control and mid-print adjustments with this reliable, warp-resistant formulation.
G-Code Viewers and Simulators
You do not have to read raw G-Code text to understand what your slicer generated. G-Code viewers visualize the toolpath layer by layer.
- PrusaSlicer Preview: Built-in layer-by-layer viewer that color-codes feature types (perimeters, infill, support, etc.). Essential for slicing quality checks.
- Cura Preview: Similar built-in layer viewer with feature type coloring and travel move visualization.
- Repetier-Host: Open-source host software with an excellent G-Code visualizer. Can simulate prints in real-time.
- GCode Viewer (web app at gcode.ws): Browser-based, no installation needed. Upload your .gcode file and navigate layers.
- Octoprint with Spaghetti Detective / Gadget: If you run Octoprint on a Raspberry Pi (popular among Indian makers for remote printing), it includes a G-Code viewer accessible via browser.
Always preview your G-Code in a viewer before printing, especially for new or complex models. A 30-second layer preview can save hours of failed prints.
Advanced G-Code Commands
M220 — Set Print Speed Override
M220 S80 ; Set print speed to 80% of slicer value
M220 S100 ; Reset to 100%
M221 — Set Flow Rate Override
M221 S95 ; Set extrusion to 95% (reduce flow rate slightly)
G10 and G11 — Firmware Retraction
If your firmware handles retraction (firmware retraction), these commands trigger retract and recover instead of explicit E movements:
G10 ; Retract
G11 ; Recover (un-retract)
M301 — Set PID Parameters
M301 P22.20 I1.08 D114.00 ; Set hotend PID values
After running PID tuning (M303), save the values in the start G-Code to ensure consistent temperature stability.
M500, M501, M502 — EEPROM Management
M500 ; Save current settings to EEPROM
M501 ; Load settings from EEPROM
M502 ; Reset settings to firmware defaults
G-Code Across Different Firmware
Most G-Code commands are standardized, but firmware implementations differ in specifics:
| Feature | Marlin | Klipper | RepRapFirmware |
|---|---|---|---|
| Bed leveling | G29 | BED_MESH_CALIBRATE | G32 |
| Filament change | M600 | PAUSE_PRINT + custom | M226 |
| Input shaping | Limited (newer versions) | SET_INPUT_SHAPER | M593 |
| Pressure advance | M900 K value | SET_PRESSURE_ADVANCE | M572 |
Klipper, which is increasingly popular in the Indian maker community (especially on modified Ender 3 printers via Raspberry Pi), uses a macro-based system where many functions are defined in config files rather than G-Code commands. If you move from Marlin to Klipper, your start/end G-Code will need to be updated significantly.
Frequently Asked Questions
Can I edit G-Code after slicing?
Yes. G-Code is plain text and can be edited in any text editor (Notepad, VS Code, Notepad++). This is commonly done to add temperature changes, pauses, or custom macros at specific layer heights. For large files, use a text editor with line search functionality.
Why is my slicer’s G-Code different from what I find online?
G-Code varies between printer models (different build volumes, bed configurations), firmware versions (Marlin 1.x vs 2.x vs Klipper), and slicer defaults. Always use a printer profile that matches your specific printer, and verify start/end G-Code against your printer’s documentation.
What does the E value in G1 commands mean?
The E value represents the extruder motor position. In absolute extrusion mode (M82, the default in most slicers), it is the cumulative total filament extruded since the last G92 E0 reset. In relative mode (M83), it is the amount to extrude for this specific move.
How do I find the G-Code for a specific layer in a large file?
Use your text editor’s search function and look for ;LAYER_CHANGE or ;Layer: (format depends on the slicer). Alternatively, use a G-Code viewer and navigate to the layer number directly.
Does Bambu Lab use standard G-Code?
Bambu Lab printers use a modified G-Code dialect and the Bambu Studio slicer generates files in .3mf format (a 3D printing container format that includes G-Code, model data, and settings). The underlying G-Code is still standard-ish, but many Bambu-specific features use proprietary extensions. For Bambu users, direct G-Code modification is less common.
Is G-Code the same for CNC routers and 3D printers?
The command set is largely the same (G0, G1, G28, etc.) but the parameters differ. CNC G-Code involves cutting operations (spindle speed via S parameter, coolant control) and does not have extruder (E) commands. A 3D printing G-Code file sent to a CNC router would not cause damage but would produce no meaningful output.
Get the Filaments That Make G-Code Testing Worth It
Whether you are fine-tuning temperature towers, calibrating flow rates, or writing custom start scripts, start with premium filament that delivers consistent results. Shop Zbotic’s full filament range — PLA, ABS, PETG and more — with fast delivery across India.
Add comment