2.1. An overview of G-Code for 3D printing
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. G-code (or more correctly, G-code) is a programming language originally developed for CNC machines. Most such machines lacked the computing power of even rudimentary handheld systems today. Your 3D printer can follow explicit g-code commands… and that’s about it. While our 3D printer is high-tech, it 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. Any interpretation, calculation, or conditional tests are done in the slicer and not the printer. Any “programming” must be done during slicing.
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.
Note
These notes are based on my experiences with the Prusa i3 Mk3 and Artillery/Evnovo Sidewinder X1 printers. If you are using a different printer, please verify the hardware details are similar.
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.
Until recently, tuning acceleration, 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:
At the start of a print, startup gcode can insert printer initialization commands, warmup routines and nozzle priming.
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.
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 or 1st layer 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.
I’ve put together more detailed notes on using gcode for different slicers:
See also
The Comprehensive RepRap G-code reference page
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. I occasionally drop into the Official Prusa 3D discord server where I can be reached as bobstro (bobstro#9830). You can email me directly at projects@ttlexceeded.com.
Last modified Apr 3, 2021. Last build on Oct 22, 2021.