G-code makes your 3D printer go ’round. Literally. It is the control language of choice for most computer numerical control (CNC) machinery, such as a lathe, mill, drill, or 3D printer.
The job of G-code-generating software is to turn a file created by a CAD (computer-aided design) program into G-code, which manages the motion of the device that is doing the making or modifying. In the world of 3D printing, the slicer takes the object and turns it into a G-code file that will build up your print layer by layer.
But what if you want to do something different that the slicing software didn’t do? Like change the filament color in the middle of a print, change the temperature of the hot end, change the amount of filament extruded, or park the hot end at the end of a print (or at any other time such as a power outage)?
If you want these features, you must add them either by placing the control commands in the appropriate places in the slicing software (more on this later) or by adding them yourself in the correct place (hint: those last 4 words mean everything)! In this article will show you how to do both.
Foreword
Before you get started on one of these G-code examples, you may want to familiarize yourself with all the different 3D printer G-code commands. We’ll give you a quick overview here, but this would help you with understanding all the G-code examples you see here.
Regardless, in this article, we’ll show you some G-code examples to put a smile on your face, springs on your feet, and prints in your display case.
Warning! G-code and M-code flavors vary from printer to printer. It’s like speaking a dialect of a language — some words mean something completely different in the dialect and you can get in a lot of trouble if you use the wrong one. Same goes here. These follow the RepRap dialect, but make no assumptions and tread lightly. Try out these code snippets judiciously a bit at a time.
The easier way to modify your G-code is to do so through the slicer. There are some obvious places where you can “tell” your slicer to insert additional G-code all by itself:
Let’s look at examples of actual functioning G-code for each of these situations.
Note: In the following, the emboldened print is the actual G-code. What follows the semicolon “;” is just a comment explaining what the step does and is not sent to the printer.
This is an example of code that could be placed where you want a filament change on a single hot end. This code moves the hot end up and out of the way, ejects the current filament, waits for you to signal it to continue, returns to where it started, and then continues with the print.
Several manufacturers make adapters to allow one hot end to feed more than one filament. A good example of this is the DisTech Automation Prometheus, a dual Bowden extruder system. In this case, one filament is withdrawn past the Y in the adapter, which clears the path for the second filament. This only works for Bowden extruders, but it is an elegant method to change filaments automatically without worrying about multiple hot ends. An example of code to handle this is shown below:
The other option for changing the filament is if you have two hot ends for each filament. There are a couple mechanisms that do this. Here we give you an example for using dual hot ends on a single carriage. The G-code will shut down or back the filament out of one hot end and fire up the other hot end. To keep the unused hot end from dripping filament onto the print, it’s a good idea to just turn down the heat too (to about 80 °C for PLA, for example). To fire up the unused hot end, simply raise its temperature, prime it, and start printing. No purging is necessary. Here is an example of a script that does that:
When changing hot ends, it may also be useful to move the hot end someplace where filament ooze will not impinge on your print.
To clean off the hot end between layers, you can add code to do that. The best place to do this is at a layer change, since the print will momentarily be stalled anyways. You can find a layer change easily in G-code, because it’s where the Z-axis moves up the height of a layer. You can clean the hot end several ways, here we will present two options.
Option 1: Purge the hot end between layers.
“xx” represents the distance away from the print that you want to move. You could also move in the X-axis as well, but remember to put the hot end back where it started after the purge.
Option 2: If you have a brush or Scotch Brite pad at a known location on your printer, you can go to that spot and move the hot end back and forth over the cleaning device several times (assume the brush is located at X150 Y150 and the best cleaning height is 5 mm).
Note that there is a Z-axis change and all movements are relative once you have moved to the location of the cleaner.
If you print a plate of objects one at a time, you might want to clean the off hot end or change the filament between the objects. During this transition is a great place to put code.
Current versions of Marlin have a single G-code command to initiate a hot-end cleaning process – G12. It is followed by four possible parameters:
For example:
More information about this command is available on the Marlin website.
Different 3D printers require different starting sequences to initialize themselves and prepare for the print. Here is an annotated example for a delta-style 3D printer produced by Slic3r:
If you have auto-leveling capability or a bed probe, you can insert the G29 code at the beginning to perform the bed leveling process. This code should be placed after the M104, M109 sequence and the bed should be at temperature first. Heating the hot end and the bed changes their dimensions slightly and can make the difference between a successful first layer and either a scratched bed or spaghetti when the print starts. There are several flavors of G29 depending on the firmware of your printer and, if you decide to use this code, it’s important to know how G29 is implemented in your firmware. Make sure you home the printer using G28 before leveling the bed.
When the print is finished, it’s useful to turn off the bed and hot end as a precaution. That code is:
Use the M190 and M109 commands with caution as they block all communication with your printer while the printer is waiting for the temperature to reach the set point.
The G-code examples in this article just scratch the surface of what is possible with G-code modifications that you make yourself. Literally anything you can do with your printer is a function of the G-code that the printer is sent. A slicer routine can determine what to send, but so can you. As with all complicated things, start slowly. We hope this article is the start of a long and happy relationship with G-code.
Want more? Check out these related articles:
Feature image source: Repetier
License: The text of "6 Easy G-code Examples to Begin With" by All3DP is licensed under a Creative Commons Attribution 4.0 International License.