3D printers communicate via a language called G-code, like all Computer Numerical Controlled (CNC) machines. This versatile language provides a set of human-readable commands for controlling each action that a 3D printer performs. Slicing software may be thought of as an interpreter for translating 3D models into a series of G-code commands for producing a solid part.
We tend to spend a lot of time focusing on key parameters affecting the final print quality (layer height, infill, perimeters, etc.), but slicing software adds two additional sections or scripts of G-code to the sliced files: start.gcode and end.gcode. These two highly customizable scripts have a dramatic effect on your final print.
Let’s examine two typical G-code lines to better understand how a command is constructed. Commands beginning with G control movements and offset definitions, while commands beginning with M control miscellaneous actions.
The purpose of the start.gcode script is to prepare the 3D printer for producing the desired object. At a minimum, the extruder and heated bed (if applicable) need to be set to proper temperatures and the tool head needs to be homed. To increase your probability of a successful print, you should also perform additional actions like leveling (or tramming) the bed, priming the hotend, and even updating the LCD to inform the user that the print is underway. The script below walks through a compilation of typical start.gcode lines that many current slicers employ:
G21 ; Set all units to millimeters M107 ; Turn off the part cooling fan M104 S215 ; Set extruder to 215°C [and immediately move on] M140 S60 ; Set bed to 60°C [and immediately move on] M190 S60 ; Set bed to 60°C [and wait for 60°C] M109 S215 ; Set extruder to 215°C [and wait for 215°C] G28 ; Move toolhead to origin (or home X, Y, Z) ; Prusa uses G28 W to perform homing command G29 ; Auto-Level the printer bed using a measurement probe ; Prusa uses G80 to accomplish a mesh bed leveling G92 E0.0 ; Reset the extruder position to 0mm G1 Z0.2 ; Move hotend nozzle to Z position of 0.2mm G1 X100.0 E20.0 ; Prime the hotend (Move to X=100mm & Extruder=20mm) G92 E0.0 ; Reset the extruder position to 0mm G90 ; Set to absolute positioning as opposed to relative M83 ; Set the extruder to relative positioning M300 S300 P1000 ; Play a 300Hz beep sound for 1000 milliseconds M117 Printing...; Update the LCD screen with “Printing...” END.GCODE
The purpose of the end.gcode script is to ensure that all printing functions have been halted, and perform any final cleanup tasks. From a safety standpoint, the most important job is to turn off power to the heating elements and motors. The script below walks through a compilation of typical end.gcode lines:
M107 ; Turn off the part cooling fan G28 X0 ; Home X axis and remove hotend from object M104 S0 ; Turn off the extruder [and immediately move on] M140 S0 ; Turn off the bed [and immediately move on] M84 ; Turn off the stepper motors M300 S300 P1000 ; Play a 300Hz beep sound for 1000 millisecond M117 Done! ; Update the LCD screen with “Done!”
With great power comes great responsibility — especially when you add custom movements into your G-code! Be sure to test your modifications from within your slicing software to ensure that you are not creating movements outside of your printer’s boundaries. You can learn more about G-code for RepRap firmware and 3D printing at reprap.org/wiki/G-code.
COMMON START.GCODE AND END.GCODE COMMANDS
G1 – Perform a synchronized movement
G21 – Set all units to millimeters since 3D printers use the metric system
G28 – Home the 3D printer or move the toolhead to the origin
G29 – Use a probe to measure the flatness of the bed then compensate by “leveling” or “tramming” the bed via a live z offset
G90 – Set all future commands to use absolute coordinates (as opposed to a relative position from the last location)
G92 – Define the current physical position to user-specified values
M83 – Set all future commands for the extruder to use relative coordinates from the last physical position (as opposed to absolute coordinates)
M84 – Stop holding the current position of the motor
M104 – Set the extruder temperature to a user-specified target (in Celsius) and immediately return control to the controller
M109 – Set the extruder temperature to a user-specified target (in Celsius) and wait for the user-specified target to be achieved
M117 – Display a user-specified message to appear on the LCD screen of the 3D printer
M140 – Set the heated bed temperature to a user-specified target (in Celsius) and immediately return control to the controller
M190 – Set the heated bed temperature to a user-specified target (in Celsius) and wait for the user-specified target to be achieved
M300 – Play a beep sound based on a user-specified frequency and duration