Introduction to G-Code and Ten Essential Commands for 3D Printing
2023-07-31 | By Maker.io Staff
GCode is one of the oldest programming languages, yet it still plays a crucial role in numerous industrial and desktop manufacturing machines around the world. We’ll cover GCode and delve into ten essential commands you should know to step up your 3D-printing game.
Image Source: https://pixabay.com/photos/printer-3d-print-3d-printing-white-2416269/
What is GCode, and Why do 3D Printers use it?
GCode, also known as Gerber or Geometric Code, is a standardized set of commands introduced in the 1950s to control CNC equipment such as drilling, milling, and cutting machines. GCode is very versatile, and aside from its use in instructing industrial CNC devices, it’s also suitable for controlling other machines such as 3D printers, 2D plotters, or even regular inkjet printers you might find in a home office.
This versatility is possible because GCode contains only a set of lines where each line represents a single command, typically composed of an alphanumeric code and parameters. These instructions tell the CNC machine what to perform and are sent to a controller board responsible for interpreting the commands and performing the desired actions.
This image shows an example GCode program created using Cura.
As the example shows, each line must contain exactly one command. However, there may be an additional comment explaining how a line of code functions. Such comments start with a semi-colon character, and the interpreting controller ignores everything following a semi-colon.
G-code remains widely used today because of its simplicity, broad compatibility, and extensive support from various manufacturers. The standardized set of commands also ensures that controllers can depend on a consistent framework. At the same time, slicer software — specifically for 3D printing — can confidently generate and send code files to 3D printers, knowing they will be highly compatible.
Essential Movement-Related GCode Commands for Makers
The first command you should know is G28. This particular command instructs the 3D printer to home its axes, returning the extruder and print bed to their reference positions. Doing so is necessary because the motors can’t remember where they are between prints or when powered off, and issuing a G28 command ensures a consistent starting point for subsequent operations. You can use the command on its own to home all axes or together with the axis to home (e.g., G28 X0 to home only the X-axis).
Using G1, you can instruct the printer to move along one or multiple axes in a linear motion. Parameters control the axis and distance. For example, G1 X100 Y100 instructs the printer to move its X and Y axes 100 units. In addition to axes, G1 can also control the printer’s extruder and feed rate. When the printer is in absolute positioning mode, G1 moves the axes to the specified position, which also applies to the extruder.
Issuing G1 commands to the extruder in absolute positioning mode may not be desirable, as it complicates calculations. Often, it’s easier to have the extruder push out 1 mm of the filament rather than telling it to advance the filament to a certain length, as would be the case in absolute mode. Therefore, you can instruct the 3D printer to switch to relative extrusion mode using M83 and revert to absolute filament positioning using M82.
Many CNC machines, such as engravers, 3D printers, mills, and laser cutters, support standard and sometimes modified GCode commands. Image Source: https://pixabay.com/photos/engraving-on-metal-milling-details-4047890/
G92 allows you to set the current position of the 3D printer’s axes, and it’s commonly used to define custom origin points or adjust the coordinates during the print process. G92 is, for example, also helpful in resetting the extruder to enforce the belief that it’s at the origin in absolute positioning mode.
When discussing movements, you need to remember that any moving machine will eventually go out of alignment, regardless of how perfectly it was calibrated at one point. The M92 command allows you to set the steps per unit for any 3D printer axis, which helps calibrate the movement control by adjusting the number of steps required for the motors to move a specific distance. You can adjust this number by extruding a specified length of the filament and then measuring the extruded distance using calipers, for example. Then, change the number until the extruded length matches the expected value.
Heating-Related GCode Commands for 3D Printing
M302 is an instruction that lets you disable or enable extrusion when the printer’s nozzle is cold. Typically, the 3D printer’s controller doesn’t allow cold extrusion to prevent damaging the extruder mechanism. However, for testing and calibration purposes, you may want to circumvent this safety mechanism by using the M302 command.
GCode programs for 3D printing typically contain either M104 or M109 in the first few lines, as these commands set the temperature of the printer’s nozzle. M104 sets the temperature but instructs the printer to immediately continue with the next instruction, while M109 makes the printer wait until its nozzle reaches the target temperature.
Controlling a Printer’s Fan Using GCode Commands
Lastly, some printers have fans that help them cool parts during critical points in the print process, such as when creating bridges. Blowing on the freshly placed material while bridging a gap may help create a better surface finish and prevent drooping. M106 allows you to control a printer’s fan speed, and M107 lets you turn off the fan.
Summary
GCode, introduced in the 1950s, is a universal programming language used to control CNC equipment and other machines such as 3D printers. It consists of lines with alphanumeric codes and parameters the device’s controller board interprets.
The commands discussed include G28 for homing axes, G1 for linear movement control, M83 for relative extrusion mode, G92 for setting positions, M92 for calibrating steps per unit, M302 for enabling cold extrusion, and M104, M109, M106, and M107 for temperature and fan control.
Still have questions about GCode? Visit the 3D Printing area of our TechForum to find more info and ask our experts!
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum