G-Code

G-code is something of a dark art amongst the 3D printing community. You could print for years without touching it, yet use it with every print. While knowledge of g-code is not required for most tasks, it can be used to solve very specific problems. In short, g-code is the set of commands that instruct your printer to move and extrude filament. When you slice a file, it is translated into a series of g-code commands that tell the printer exactly where to move, how much filament to extrude or retract, or whether to turn fans and heaters on or off. While our 3D printer is high-tech, g-code is a decades-old rudimentary control language. The printer has no real intelligence or decision-making capabilities. It’s a profoundly dumb device, lacking any sort of decision making or programming logic. Nearly everything the printer does is based on explicit g-code commands.

After comparing a variety of “magic” slicer profiles, I realized a lot of the magic happens in the g-code. In many cases, a bit of embedded g-code sets printer values that provide most of the benefit with little or no explanation. Knowing just a bit of g-code can allow you to really optimize the hardware and print settings of your printer.

Note

These notes are based on my experiences with the Prusa i3 Mk3 printer. If you are using a different printer, please verify the hardware details are same. These pages may be a bit rough as I revise them and add new material. Please check back regularly for updates.

Until recently, tuning accleration, jerk and other hardware settings required tweaking and inserting gcode commands by hand. More recent versions of slicers – notably PrusaSlicer and Ultimaker Cura – have begun exposing these settings in printer, print and filament profiles, making them much more accessible and easy to adjust between prints. While this is a big improvement, there are still several refinements to the print sequence that can optimize your prints. Custom g-code is the right way to insert these functions. Most slicers support inserting g-code at several points during a print:

  1. At the start of a print, startup gcode can insert printer initialization commands, warmup routines and nozzle priming.

  2. At the end of a print, end g-code is responsible for turning off heaters, motors and fans. It can also be modified to move the extruder and bed after a print, make noises or otherwise tidy things up.

  3. Between layer changes, gcode is useful for adjusting hardware settings like nozzle temperatures and other settings based on the current layer number or height.

Caution

Please be sure that you’ve calibrated your printer, and particularly your Live-Z settings before using any of the following examples. These routines move the nozzle around close to the bed and can cause damage if your printer is not adjusted properly.

Contact and feedback

You can find me on the Prusa support forums or Reddit where I lurk in many of the 3D printing-related subreddits.

Last updated on 20190828