guide

3D printer G-code reference: the commands worth knowing

What G-code commands are most useful to know for 3D printing?

The most useful G-code commands to know: G28 homes all axes, G29 runs auto bed leveling, G0/G1 move the head, M104 sets hotend temperature without waiting, M109 sets hotend temperature and waits, M140 sets bed temperature without waiting, M190 sets bed temperature and waits, M500 saves settings to EEPROM, M503 reports current settings, and M92 sets steps per millimeter. You rarely need to write G-code by hand; the slicer handles it.

What G-code is and where it comes from

G-code is the numerical control language that tells a 3D printer (or a CNC machine) where to move, how fast, and what temperature to use. Your slicer generates thousands of G-code lines for every print and writes them to a file on your SD card or sends them over USB or network. You almost never need to write G-code from scratch, but knowing the common commands helps you understand what the slicer is doing, modify start and end scripts, and troubleshoot problems.

G-code on 3D printers is not perfectly standardized: Marlin, Klipper, and RepRapFirmware share most commands but have different extensions and behaviors in edge cases. The commands listed in this guide apply broadly to most Marlin-based printers, which covers the majority of consumer FDM machines.

Motion commands: G0 and G1

G0 is rapid move: the head moves to the specified coordinates at maximum speed with no extrusion. G1 is coordinated move: the head moves to the specified coordinates at a specified feedrate, optionally extruding at the same time. In 3D printing G0 and G1 are often interchangeable since the firmware usually treats both as coordinated moves, but the distinction matters in CNC machining.

Example: G1 X100 Y50 Z0.3 E5.2 F3000 moves the head to X100, Y50, Z0.3 while extruding to position E5.2 at feedrate 3000mm/min. The F feedrate persists until changed. The E coordinate is the extruder position, not a per-move amount in absolute mode, but the exact interpretation depends on whether your firmware is in absolute (M82) or relative (M83) extrusion mode.

Homing: G28

G28 homes the specified axes by moving them toward their endstops until the endstop triggers, then backing off and re-approaching slowly to find the exact trigger point. G28 with no parameters homes all three axes. G28 X homes only X. G28 X Y homes X and Y in sequence. On printers with auto bed leveling, the probe is usually used as the Z endstop, and the firmware applies the probe offset when setting the Z home position.

Homing must happen at the start of every print so the firmware knows where the head is. Most start G-code scripts generated by slicers include G28 as the first motion command. If your printer loses its position mid-print (power loss, connection drop, or a stalled motor), re-homing is required before continuing or aborting.

Bed leveling: G29

G29 triggers the automatic bed leveling or mesh probing sequence. The exact behavior depends on your firmware configuration: it may probe a grid of points, build a compensation mesh, and apply it to subsequent moves. After G29 runs, the firmware compensates all Z moves to follow the measured bed surface.

G29 is often placed in the start G-code just after G28. Some users re-probe every print (safe but slow), others save the mesh with M500 and load it with G29 L1 (or equivalent on their firmware) to avoid re-probing each time. A saved mesh becomes inaccurate if the bed surface changes temperature before probing was done, or if the printer is moved, so probing frequency is a judgment call based on how consistent your first layers are.

Temperature commands

M104 sets the hotend temperature and returns immediately so the printer can do other things while it heats. M109 sets the hotend temperature and waits until it is reached before moving to the next command. For start scripts, M109 is usually the right choice so the printer does not start extruding cold material. M104 is useful when you want to start heating early and do other setup steps in parallel.

M140 and M190 are the equivalents for the heated bed: M140 sets and returns immediately, M190 sets and waits. In start scripts, M140 is often placed before M109 so bed and hotend heat together rather than sequentially, cutting total heat-up time.

EEPROM and settings commands

M500 saves the current settings to EEPROM so they persist across power cycles. After calibrating PID, E-steps, or Z offset, always save with M500. M501 loads settings from EEPROM (restores to last saved state). M502 restores all settings to compiled firmware defaults (useful when EEPROM is corrupt). M503 reports the current in-memory settings, which is the fastest way to see what values are active without navigating menus.

M92 sets steps per millimeter for any axis: M92 E415 sets extruder steps to 415 steps/mm. Follow any M92 command with M500 to save it, otherwise it is lost at power-off. The same applies to M301 (PID settings) and M851 (Z probe offset).

Common questions

Frequently asked questions: g-code reference

What does G28 do on a 3D printer?

G28 homes the printer by moving the specified axes toward their endstops until the endstop sensors trigger. This sets the firmware's internal position tracking to zero for each homed axis, which is required before any print so the printer knows where its head is. G28 with no parameters homes all three axes. G28 X Y homes just X and Y.

What is the difference between M104 and M109?

M104 S200 sets the hotend to 200 degrees and immediately continues to the next command without waiting. M109 S200 sets the hotend to 200 degrees and waits at that command until the temperature is reached before moving to the next line. Use M109 in start G-code when you need to be at temperature before extruding; use M104 when you want to start heating while doing other setup steps in parallel.

How do I save settings to EEPROM so they survive power off?

After making any setting change via G-code command (like M92 for steps, M301 for PID, or M851 for Z offset), send M500 to save all current settings to EEPROM. Settings changed via G-code that are not saved with M500 will be lost when the printer powers off. You can verify what is saved by sending M503, which reports all current in-memory values.

What is the G-code to run auto bed leveling?

G29 runs the auto bed leveling sequence. The exact behavior depends on your firmware and probe configuration: it typically probes a grid of points and stores the resulting mesh. G29 is usually placed in your start G-code right after G28. Some firmwares support saving the mesh with M500 and restoring it with G29 L1 to avoid re-probing every print.

Get new g-code reference guides by email

One email when a new guide goes up. No spam, unsubscribe any time.

3D Printer Softwares publishes free educational guides on 3D printing software. Some links are affiliate links; we earn a commission at no extra cost to you. Software versions, pricing, and compatibility change; verify details with the official developer before purchasing or installing anything.