Bird
Raised Fist0
3D Printingknowledge~15 mins

Common G-code commands (G0, G1, G28, M104, M106) in 3D Printing - Deep Dive

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Overview - Common G-code commands (G0, G1, G28, M104, M106)
What is it?
G-code is a language that tells 3D printers and CNC machines how to move and operate. Common G-code commands like G0, G1, G28, M104, and M106 control movements, positioning, and machine functions such as heating and fan control. These commands are simple instructions that guide the printer step-by-step to create objects. Understanding these commands helps you know how your 3D printer works and how to fix or customize prints.
Why it matters
Without these commands, 3D printers wouldn't know where to move or how to prepare for printing. They solve the problem of translating digital designs into physical movements and actions. If these commands didn't exist, printing would be random or impossible, leading to failed prints and wasted materials. Knowing them lets you troubleshoot, optimize prints, and even create custom printing behaviors.
Where it fits
Before learning these commands, you should understand basic 3D printing concepts like layers, axes (X, Y, Z), and printer parts (nozzle, bed, fans). After mastering these commands, you can explore advanced G-code programming, custom macros, and firmware settings to fully control your printer.
Mental Model
Core Idea
Common G-code commands are simple, step-by-step instructions that tell a 3D printer how to move, heat, and cool to build an object layer by layer.
Think of it like...
It's like giving a robot precise directions: 'Go straight to this spot,' 'Move slowly while drawing,' 'Heat up before starting,' or 'Turn on the fan now.' Each command is a clear order the robot follows exactly.
┌───────────────┐
│  G-code Line  │
│  (Command)    │
└──────┬────────┘
       │
       ▼
┌───────────────┐
│  Movement     │
│  or Action    │
└──────┬────────┘
       │
       ▼
┌───────────────┐
│  Printer      │
│  Executes     │
└───────────────┘
Build-Up - 7 Steps
1
FoundationUnderstanding G-code Basics
🤔
Concept: G-code commands are text instructions that control printer movements and actions.
G-code lines start with a letter (G or M) followed by numbers and parameters. G commands mainly control movements, while M commands control machine functions like heating or fans. For example, G0 and G1 move the print head, but G1 moves while extruding filament.
Result
You can read and recognize basic G-code commands and know their general purpose.
Understanding the difference between G and M commands helps you know which control movement and which control machine functions.
2
FoundationAxes and Positioning in G-code
🤔
Concept: G-code commands use X, Y, and Z coordinates to tell the printer where to move.
The printer's nozzle moves in three directions: X (left-right), Y (front-back), and Z (up-down). Commands like G0 and G1 include coordinates like X10 Y20 Z0.3 to specify exact positions. The printer moves the nozzle to these points to build layers.
Result
You can understand how G-code controls the printer's position in 3D space.
Knowing how coordinates map to physical printer movements is key to understanding how prints are formed layer by layer.
3
IntermediateG0 and G1: Rapid and Controlled Moves
🤔Before reading on: do you think G0 and G1 commands move the printer head at the same speed or different speeds? Commit to your answer.
Concept: G0 commands move the print head quickly without extruding, while G1 moves at controlled speeds and extrudes filament.
G0 is used for fast, non-printing moves to reposition the nozzle quickly. G1 moves the nozzle while pushing filament out to create the print. Both commands include coordinates and optional speed settings. For example, G1 X50 Y50 E10 moves to X50 Y50 while extruding 10 units of filament.
Result
You can distinguish between fast moves and printing moves in G-code.
Understanding the difference prevents mistakes like extruding filament during rapid moves, which can cause blobs or stringing.
4
IntermediateG28: Homing the Printer
🤔Before reading on: does G28 move the printer to a random position or a fixed reference point? Commit to your answer.
Concept: G28 tells the printer to move all axes to their home positions, usually where limit switches are triggered.
Homing sets the printer's coordinate system by moving the nozzle to known reference points, often at the edges of the print bed. This ensures all future moves are accurate. For example, G28 moves X, Y, and Z axes to their home positions. You can also home individual axes like G28 X or G28 Z.
Result
You understand how the printer knows its starting point for accurate printing.
Knowing homing is essential for print accuracy and avoiding crashes or misaligned layers.
5
IntermediateM104: Setting Nozzle Temperature
🤔Before reading on: does M104 wait for the nozzle to reach temperature before continuing or not? Commit to your answer.
Concept: M104 sets the nozzle temperature but does not wait for it to reach the target before moving on.
M104 S200 sets the nozzle temperature to 200°C. The printer starts heating but continues executing the next commands immediately. To wait until the temperature is reached, M109 is used instead. This allows heating and other preparations to happen in parallel.
Result
You can control nozzle heating timing and avoid print errors from cold extrusion.
Knowing the difference between M104 and M109 helps optimize print start times and prevents nozzle jams.
6
AdvancedM106: Controlling the Cooling Fan
🤔Before reading on: does M106 turn the fan on at full speed only, or can it set variable speeds? Commit to your answer.
Concept: M106 controls the cooling fan speed with values from 0 (off) to 255 (full speed).
M106 S128 sets the fan to about half speed. Cooling fans help solidify the printed filament quickly, improving print quality. You can turn the fan off with M107. Adjusting fan speed during printing affects layer adhesion and surface finish.
Result
You can manage cooling to improve print quality and avoid warping or stringing.
Understanding fan control lets you fine-tune prints for different materials and geometries.
7
ExpertCombining Commands for Efficient Printing
🤔Before reading on: do you think combining movement and temperature commands in sequence affects print speed or quality? Commit to your answer.
Concept: Expert G-code sequences combine movement, heating, and fan commands to optimize print speed and quality.
For example, starting a print might include G28 to home, M104 to start heating, G0 to move to start position, and M106 to turn on the fan. Knowing when to wait for temperature (M109) or move immediately (M104) affects print time. Also, combining G1 moves with extrusion and fan speed changes can smooth transitions and improve layer bonding.
Result
You can write or modify G-code to make prints faster and better without manual intervention.
Knowing how to sequence commands strategically unlocks advanced control over printing processes and troubleshooting.
Under the Hood
G-code commands are interpreted line-by-line by the printer's firmware, which translates them into electrical signals that move stepper motors, control heaters, and operate fans. The firmware maintains a coordinate system and tracks temperatures, ensuring movements and actions happen precisely as commanded. Commands like G0 and G1 adjust motor steps to move the nozzle, while M commands control hardware components. The printer processes commands in order, sometimes waiting for conditions like temperature before continuing.
Why designed this way?
G-code was designed as a simple, text-based language to be easy to generate, read, and modify. Separating movement (G commands) from machine functions (M commands) keeps instructions clear and modular. The design allows printers from different manufacturers to use a common language, promoting compatibility. Waiting commands like M109 were added to handle hardware constraints safely, while non-waiting commands like M104 improve efficiency by allowing parallel operations.
┌───────────────┐
│  G-code Line  │
└──────┬────────┘
       │
       ▼
┌───────────────┐
│  Firmware     │
│  Interpreter  │
└──────┬────────┘
       │
       ▼
┌───────────────┬───────────────┬───────────────┐
│Stepper Motors │Heaters       │Fans           │
│(X,Y,Z,E axes)│(Nozzle, Bed) │(Cooling Fan)  │
└───────────────┴───────────────┴───────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does G0 always move faster than G1? Commit to yes or no before reading on.
Common Belief:G0 always moves the print head faster than G1.
Tap to reveal reality
Reality:G0 is intended for rapid moves without extrusion, but actual speed depends on firmware settings and can be similar to G1 speeds.
Why it matters:Assuming G0 is always faster can lead to incorrect expectations about print time and cause confusion when speeds seem inconsistent.
Quick: Does M104 wait for the nozzle to reach temperature before continuing? Commit to yes or no before reading on.
Common Belief:M104 waits for the nozzle to heat before moving to the next command.
Tap to reveal reality
Reality:M104 sets the temperature but does not wait; the printer continues immediately. M109 is the command that waits.
Why it matters:Misunderstanding this can cause printing to start before the nozzle is hot, leading to poor extrusion or clogs.
Quick: Does G28 move the nozzle to a random position? Commit to yes or no before reading on.
Common Belief:G28 moves the nozzle to a random or user-defined position.
Tap to reveal reality
Reality:G28 always moves the nozzle to the home position defined by the printer's limit switches.
Why it matters:Thinking G28 moves to arbitrary points can cause confusion and improper printer setup, risking crashes or misalignment.
Quick: Can M106 only turn the fan fully on or off? Commit to yes or no before reading on.
Common Belief:M106 can only turn the fan fully on or off.
Tap to reveal reality
Reality:M106 can set the fan speed anywhere from 0 (off) to 255 (full speed), allowing fine control.
Why it matters:Ignoring variable fan speeds limits print quality control and material-specific cooling strategies.
Expert Zone
1
Some printers and firmware allow combining multiple commands on one line, but this can cause timing issues if not handled carefully.
2
The difference between absolute and relative positioning modes (G90 vs G91) changes how coordinates in G0/G1 are interpreted, which can cause unexpected moves if misunderstood.
3
Fan control with M106 can be linked to temperature sensors or print progress, enabling dynamic cooling strategies that improve print quality.
When NOT to use
Using manual G-code commands is not ideal for beginners or complex prints; slicer software automates command generation better. For advanced motion control, newer protocols or firmware-specific commands may be preferred. Also, relying solely on G-code for temperature control can be risky; firmware safety features and sensors should be used.
Production Patterns
In professional 3D printing, G-code is often customized with start and end scripts to prepare the printer and finish prints cleanly. Conditional commands and macros automate repetitive tasks. Cooling fan speeds are adjusted dynamically based on layer time or material. Homing (G28) is always done before printing to ensure accuracy. Temperature commands are sequenced to minimize wait times while ensuring proper extrusion.
Connections
CNC Machining G-code
Common language and command structure
Understanding 3D printer G-code helps grasp CNC machining commands since both use similar instructions for precise machine control.
Robotics Motion Control
Shared principles of coordinate-based movement
G-code commands resemble robotic arm instructions, showing how coordinate systems and stepwise commands control physical movement in automation.
Cooking Recipes
Step-by-step instructions to achieve a result
Like G-code guides a printer, recipes guide cooking; both require precise steps in order and timing to produce a successful outcome.
Common Pitfalls
#1Starting a print before the nozzle is heated.
Wrong approach:M104 S200 G28 G1 X50 Y50 E10
Correct approach:M104 S200 G28 M109 S200 G1 X50 Y50 E10
Root cause:Using M104 without waiting (M109) causes extrusion before the nozzle reaches proper temperature.
#2Assuming G0 extrudes filament during moves.
Wrong approach:G0 X100 Y100 E5
Correct approach:G1 X100 Y100 E5
Root cause:G0 is for rapid moves without extrusion; adding extrusion parameters to G0 is ignored or causes errors.
#3Not homing the printer before printing.
Wrong approach:G1 X0 Y0 Z0
Correct approach:G28 G1 X0 Y0 Z0
Root cause:Skipping G28 means the printer doesn't know its true position, leading to inaccurate or damaging moves.
Key Takeaways
Common G-code commands like G0, G1, G28, M104, and M106 are essential instructions that control printer movement, positioning, heating, and cooling.
G0 moves the print head quickly without extruding, while G1 moves with extrusion to build the object layer by layer.
Homing with G28 sets the printer's reference point, ensuring accurate and safe printing.
M104 sets nozzle temperature without waiting, while M109 waits for the target temperature before continuing.
M106 controls the cooling fan speed, allowing fine adjustments to improve print quality.

Practice

(1/5)
1. What does the G-code command G28 do in 3D printing?
easy
A. Sets the extruder temperature
B. Homes all axes to their origin positions
C. Moves the print head quickly without extruding
D. Turns on the cooling fan

Solution

  1. Step 1: Understand the purpose of G28

    The G28 command is used to move the printer's axes to their home or origin positions, ensuring the printer knows where the starting point is.
  2. Step 2: Compare with other commands

    Other commands like G0 move quickly without printing, M104 sets temperature, and M106 controls the fan, so they do not home the axes.
  3. Final Answer:

    Homes all axes to their origin positions -> Option B
  4. Quick Check:

    G28 = Home axes [OK]
Hint: G28 always means 'go home' for all axes [OK]
Common Mistakes:
  • Confusing G28 with G0 or G1 which move the head
  • Thinking M104 or M106 control movement instead of temperature or fan
  • Assuming G28 sets temperature or fan speed
2. Which of the following is the correct syntax to set the extruder temperature to 200°C using G-code?
easy
A. G28 S200
B. M106 S200
C. G1 S200
D. M104 S200

Solution

  1. Step 1: Identify the command for setting extruder temperature

    M104 is the G-code command used to set the extruder temperature to a specified value.
  2. Step 2: Check the syntax

    The correct syntax is M104 followed by S and the temperature value, so M104 S200 sets the extruder to 200°C.
  3. Final Answer:

    M104 S200 -> Option D
  4. Quick Check:

    M104 sets extruder temp = M104 S200 [OK]
Hint: M104 sets extruder temp; M106 controls fan [OK]
Common Mistakes:
  • Using M106 which controls the fan instead of temperature
  • Using G1 or G28 which are for movement, not temperature
  • Omitting the S parameter for temperature value
3. What will happen when the following G-code commands are executed?
G0 X50 Y50
G1 X100 Y100 E10
medium
A. The print head homes all axes, then sets extruder temperature to 10°C
B. The print head moves slowly to (50,50) while extruding, then quickly to (100,100) without extruding
C. The print head moves quickly to (50,50) without extruding, then moves to (100,100) while extruding filament
D. The print head turns on the fan and moves to (100,100)

Solution

  1. Step 1: Understand G0 and G1 commands

    G0 moves the print head quickly without extruding filament. G1 moves the print head while extruding filament as specified by the E parameter.
  2. Step 2: Analyze the commands given

    First, G0 moves the head to X50 Y50 quickly without extrusion. Then, G1 moves the head to X100 Y100 while extruding 10 units of filament.
  3. Final Answer:

    The print head moves quickly to (50,50) without extruding, then moves to (100,100) while extruding filament -> Option C
  4. Quick Check:

    G0 = fast move no extrusion, G1 = move with extrusion [OK]
Hint: G0 = fast move no extrusion; G1 = move with extrusion [OK]
Common Mistakes:
  • Mixing up G0 and G1 commands
  • Assuming extrusion happens during G0
  • Confusing E parameter as fan speed or temperature
4. A user writes the following G-code to turn on the cooling fan at half speed:
M106 S128

But the fan does not turn on. What is the likely error?
medium
A. The printer firmware might require a different command or fan pin setup
B. The S value should be between 0 and 255, 128 is invalid
C. The command should be M104 to control the fan
D. The G28 command is missing before M106

Solution

  1. Step 1: Understand M106 command and S parameter

    M106 controls the cooling fan speed with S value from 0 (off) to 255 (full speed). S128 is a valid half speed.
  2. Step 2: Consider why fan might not turn on

    If the fan does not turn on, it is likely due to printer firmware settings or hardware configuration, such as fan pin assignment or disabled fan control.
  3. Final Answer:

    The printer firmware might require a different command or fan pin setup -> Option A
  4. Quick Check:

    M106 S128 valid but hardware/firmware may block fan [OK]
Hint: Check firmware and hardware if M106 S128 doesn't work [OK]
Common Mistakes:
  • Thinking S128 is out of range (it's valid)
  • Using M104 instead of M106 for fan control
  • Assuming G28 is needed before fan commands
5. You want to start a print by homing all axes, setting the extruder temperature to 210°C, waiting for it to reach temperature, then moving the print head to X10 Y10 quickly without extruding. Which sequence of G-code commands is correct?
hard
A. G28
M104 S210
M109 S210
G0 X10 Y10
B. M104 S210
G28
M109 S210
G1 X10 Y10 E0
C. G28
M109 S210
M104 S210
G0 X10 Y10
D. M106 S210
G28
M109 S210
G0 X10 Y10

Solution

  1. Step 1: Identify commands for each action

    G28 homes all axes. M104 sets extruder temperature without waiting. M109 sets temperature and waits until reached. G0 moves quickly without extruding.
  2. Step 2: Check command order for correct sequence

    First home with G28, then set temp with M104, wait with M109, finally move with G0 to X10 Y10 without extrusion.
  3. Final Answer:

    G28
    M104 S210
    M109 S210
    G0 X10 Y10
    -> Option A
  4. Quick Check:

    Home, set temp, wait, then fast move = G28
    M104 S210
    M109 S210
    G0 X10 Y10 [OK]
Hint: Use M109 to wait for temp after M104, then move with G0 [OK]
Common Mistakes:
  • Using M106 instead of temperature commands
  • Moving with G1 and extruding when not needed
  • Setting temperature after moving the head