Skip to content
GEEKnodes

3D printing

G-code Explained: Structure and Basic 3D Printing Commands

What G-code is, how a file is structured and what G0/G1, G28, G29, G90/G91, M104/M109, M140/M190, M106, M84 and M500 do, with sample start and end code.

G-code is the language your 3D printer understands: a list of text instructions that tell it where to move, how much to extrude and what temperature to heat to. In this guide you’ll see how a file is organized, what the most common commands do and how to read your slicer’s start and end G-code.

The examples follow the syntax of Marlin, the most widely used firmware. Klipper and RepRapFirmware accept most of these commands.

In short

  • G commands are movements; M commands are machine functions (temperature, fan, motors).
  • The letters that go with a command are parameters: X, Y, Z, E (extruder), F (speed) and S (value).
  • The start G-code heats up, homes and purges; the end G-code turns off the heaters, moves the print head out of the way and disables the motors.

What G-code is

The slicer (Cura, PrusaSlicer, OrcaSlicer…) turns a 3D model into thousands of lines of G-code. The printer runs them from top to bottom, one by one.

Each line looks like this:

G1 X50 Y25.3 E0.42 F1500 ; optional comment
  • G1: the command (linear move).
  • X50 Y25.3: destination in millimeters.
  • E0.42: filament to extrude during the move.
  • F1500: speed in mm/min (1,500 mm/min is 25 mm/s).
  • ;: everything after it is a comment and the printer ignores it.

Structure of a G-code file

A typical file has four blocks:

  1. Header: slicer comments with the profile, estimated time and filament.
  2. Start G-code: heat up, home, level and purge the nozzle.
  3. Body: layer by layer, with comments like ;LAYER:0 or ;TYPE:WALL-OUTER.
  4. End G-code: turn everything off and leave the part accessible.
Four blocks of a G-code file: header, start, layer-by-layer body and end.
The file runs from top to bottom: it starts with the header and ends when the print wraps up.

You define the start and end G-code yourself in the slicer’s printer settings. The body is generated by the slicer.

Movement commands

CommandWhat it doesExample
G0Rapid move (no extrusion)G0 X10 Y10 F6000
G1Linear move, with or without extrusionG1 X60 E2.5 F1200
G28Home: sends the axes to their endstopsG28 or G28 Z
G29Automatic bed leveling (mesh probing)G29
G90Absolute coordinatesG90
G91Relative coordinatesG91
G92Sets the current position without movingG92 E0

G0 and G1

In Marlin, G0 and G1 behave practically the same. Slicers usually use G0 for travel moves and G1 for extruding lines, which makes the file easier to read.

G90 and G91: absolute vs. relative

With G90, G1 Z10 means “go to Z = 10 mm”. With G91, the same command means “move up 10 mm from wherever you are”.

Comparison of an absolute Z move to 10 mm and a relative 10 mm move from Z=5.
G90 targets a coordinate; G91 adds the distance to the current position.

It’s very handy in the end G-code to lift the nozzle without knowing at what height the part finished:

G91        ; relative mode
G1 Z10 F600 ; move up 10 mm from the current position
G90        ; back to absolute mode

The extruder has its own mode: M82 (absolute) and M83 (relative). Your slicer must use the same one that the start G-code declares.

G92 E0

Resets the extruder counter to zero. You’ll see it before every purge and, in many slicers, at the start of every layer.

Temperature commands

CommandWhat it doesWaits?
M104 S200Sets the hotend to 200 °CNo
M109 S200Sets the hotend to 200 °CYes, until it gets there
M140 S60Sets the bed to 60 °CNo
M190 S60Sets the bed to 60 °CYes, until it gets there

According to the Marlin documentation, M109 with S only waits while heating. With R it also waits if the temperature has to drop, which is useful when switching materials.

A common trick is to heat both at once and wait afterward:

M140 S60  ; bed to 60 without waiting
M104 S200 ; hotend to 200 without waiting
M190 S60  ; now wait for the bed
M109 S200 ; and then for the hotend

Fan, motors and memory

CommandWhat it does
M106 S255Part cooling fan at 100% (the value ranges from 0 to 255)
M106 S128Fan at roughly 50%
M107Turns off the part cooling fan
M84Disables the stepper motors
M500Saves the current settings to EEPROM
M501Loads the saved settings
M503Shows the current settings
M420 S1Enables the saved leveling mesh

M500 matters: changes such as steps per millimeter (M92) or the Z offset (M851) are lost when you power off if you don’t save them. In Klipper, the equivalent is SAVE_CONFIG.

Sample start G-code

A typical start sequence for an Ender 3-style printer with a leveling sensor:

G90                 ; absolute coordinates
M82                 ; extruder in absolute mode
M140 S60            ; bed to 60 °C
M104 S150           ; hotend to 150 °C so it doesn't ooze while probing
M190 S60            ; wait for the bed
G28                 ; home all axes
G29                 ; probe the mesh (or M420 S1 if it's already saved)
M109 S210           ; hotend to printing temperature
G92 E0              ; reset extruder counter
G1 Z2.0 F3000       ; lift the nozzle a little
G1 X0.1 Y20 Z0.3 F5000   ; go to the start of the purge line
G1 X0.1 Y200 Z0.3 F1500 E15 ; first purge line
G1 X0.4 Y200 Z0.3 F5000  ; move to the side
G1 X0.4 Y20 Z0.3 F1500 E30  ; second purge line
G92 E0              ; reset counter again
G1 Z2.0 F3000       ; lift before starting the part
Print start sequence: heat up, home, probe the bed and purge the nozzle.
Order matters: homing comes before probing, and the purge primes the nozzle for printing.

Why each step:

  • Hotend at 150 °C during probing: that way it doesn’t ooze plastic onto the bed or the sensor.
  • G28 before G29: probing needs to know where the origin is.
  • Purge line: fills the nozzle with fresh plastic before the first layer.

Keep in mind that G28 disables leveling in Marlin by default. If you don’t run G29 on every print, you need M420 S1 after homing.

Sample end G-code

G91                 ; relative mode
G1 E-2 F2700        ; retract 2 mm of filament
G1 Z10 F600         ; move up 10 mm
G90                 ; absolute mode
G1 X0 Y220 F3000    ; bring the bed forward to remove the part
M106 S0             ; turn off the part cooling fan
M104 S0             ; turn off the hotend
M140 S0             ; turn off the bed
M84 X Y E           ; disable motors (keep Z locked)

Keeping the Z motor enabled prevents the axis from dropping under its own weight on some printers.

How to send G-code to the printer

  • Printer screen: many let you run single commands from a menu.
  • Pronterface or the OctoPrint terminal: connect over USB and type commands one at a time. See how to control the Ender 3 with OctoPrint.
  • Mainsail or Fluidd on printers running Klipper.

When you send M503, the printer replies with its settings, which is handy for knowing what values you have saved.

Common mistakes

  • Forgetting that F is in mm/min. F50 moves the print head at less than 1 mm/s.
  • Mixing absolute and relative extruder modes. The printer extrudes absurd amounts on the first layer.
  • Not saving with M500. Your settings disappear when you power off.
  • Moving Z with G91 and not going back to G90. The rest of the print is interpreted incorrectly.
  • Waiting for the hotend before leveling. It oozes onto the bed during probing.

Accessibility preferences

Quick profiles

Text and reading

Text size
Default
Line height
Default
Letter spacing
Default
Word spacing
Default

Colour

Motion and orientation

Cursor