Common G-code commands (G0, G1, G28, M104, M106) in 3D Printing - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using common G-code commands in 3D printing, it's helpful to understand how the time to execute these commands changes as the number of commands grows.
We want to know: how does the printer's work time increase when we add more commands?
Analyze the time complexity of this sequence of G-code commands.
G28 ; Home all axes
M104 S200 ; Set extruder temperature
G0 X10 Y10 Z0.3 ; Move quickly to position
M106 S255 ; Turn on fan at full speed
G1 X50 Y50 E10 ; Move while extruding
This snippet shows common commands for moving, heating, and controlling the printer.
Look at what repeats when many commands run.
- Primary operation: Each command executes one after another.
- How many times: The number of commands determines how many steps the printer takes.
As you add more commands, the printer does more work, roughly one step per command.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 commands executed |
| 100 | 100 commands executed |
| 1000 | 1000 commands executed |
Pattern observation: The work grows directly with the number of commands; doubling commands doubles work.
Time Complexity: O(n)
This means the time to run all commands grows in a straight line with how many commands you have.
[X] Wrong: "Adding more commands won't affect total time much because some commands are fast."
[OK] Correct: Even if some commands are quick, each command adds to total time, so more commands always mean more time overall.
Understanding how the number of commands affects printing time helps you think clearly about efficiency and scaling, a useful skill in many technical roles.
"What if we combined multiple moves into one command? How would the time complexity change?"
Practice
G28 do in 3D printing?Solution
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.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.Final Answer:
Homes all axes to their origin positions -> Option BQuick Check:
G28 = Home axes [OK]
- 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
Solution
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.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.Final Answer:
M104 S200 -> Option DQuick Check:
M104 sets extruder temp = M104 S200 [OK]
- 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
G0 X50 Y50
G1 X100 Y100 E10
Solution
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.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.Final Answer:
The print head moves quickly to (50,50) without extruding, then moves to (100,100) while extruding filament -> Option CQuick Check:
G0 = fast move no extrusion, G1 = move with extrusion [OK]
- Mixing up G0 and G1 commands
- Assuming extrusion happens during G0
- Confusing E parameter as fan speed or temperature
M106 S128
But the fan does not turn on. What is the likely error?
Solution
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.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.Final Answer:
The printer firmware might require a different command or fan pin setup -> Option AQuick Check:
M106 S128 valid but hardware/firmware may block fan [OK]
- Thinking S128 is out of range (it's valid)
- Using M104 instead of M106 for fan control
- Assuming G28 is needed before fan commands
Solution
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.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.Final Answer:
G28
M104 S210
M109 S210
G0 X10 Y10 -> Option AQuick Check:
Home, set temp, wait, then fast move = G28
M104 S210
M109 S210
G0 X10 Y10 [OK]
- Using M106 instead of temperature commands
- Moving with G1 and extruding when not needed
- Setting temperature after moving the head
