What is G-code?
3D printers are machines. They do not understand human speech or English text. They understand what is known as machine language. In order for us to instruct the 3D printer on what to do in a particular job, a suitable language of communication is necessary. This language must be understood by both the 3D printer and the humans using the printer. The name of this language is G-code.
The commands in the G-code tell the printer which parts to move, in what direction to move, at what speed to move, and what temperatures to set. The G-code is comprised of G-commands and M-commands. Each of these commands is associated with a particular movement and action.
Why learn about G-codes?
It is absolutely critical for any user of a 3D printer to have at least a basic understanding of G-codes. Learning about the G-code will give you a deeper insight into how a 3D printer actually works. It will also allow you to manage the entire 3D printing process. You will be able to correctly verify the slicer files, perform maintenance, and even debug small errors in the code if something isn’t working right.
G-codes can be created by a slicing software. This software basically converts a 3D model or design into individual layers (slices them) that the 3D printer prints one-by-one. Once the individual slices are created, the file then gets converted into G-code to instruct the 3D printer what to do.
How to access the G-code?
You can view the G-code by saving the output file from the slicer software onto your computer. Once you save the file, go to the folder where you saved it and open the file using a text editor like Notepad. Normally, the extension of the file will be .gcode. Other file formats are also sometimes given out by the slicer software. However, some of those formats might be full of 0’s and 1’s as the file would be a binary file. Hence, use a slicing software that gives out a .gcode file format.
Printing a 3D model involves thousands of actions or movements. Hence, a typical G-code can be pretty long, often running into hundreds of pages. Writing so many lines of code will be a time-taking and tedious task. Hence, using a slicer software to convert the design into G-code is a practical solution. Automating this process saves you a lot of time indeed.
What does a typical G-code format look like?
If you actually try to read a G-code file using a text editor, you will see a few common things. At the start of every new line of code, you will see a line number to the very left. Next, you will see the letter G or the letter M. These letters simply signify whether the command you are reading is a G-command or an M-command.
Next, you will notice the letter F followed by a number. This portion indicates the speed with which the printer head is supposed to move. Then you will notice the letter X followed by some number and a letter Y followed by another number. These are the X and Y coordinates that guide the printer head on where to move.
Lastly, to the extreme right, you will see the letter E followed by a number. This letter indicates the printer’s feeder movement (also known as the extruder from where the filament is deposited). The number after E is the length of the filament to be pushed through the extruder nozzle.
Sometimes, there is a semicolon at the end of a line and that is then followed by some comments. These comments are ignored by the 3D printer and are only for the reference of the user or the person reading the code. A sample line of G-code is shown as follows:
12 G2 F800 X183.500 Y35.400 E24.62500
Note: Unless you are an expert on G-code, try not to write your own G-code because it may cause damage to your 3D printer.
Commonly used G-code commands
Let us now look at some of the most commonly used G-code commands for 3D printing:
Returning to home – G28
The command G28 is used to instruct the printer head to move towards the far end of the printer until the head touches the end stops. This is like the origin or the starting point of any printing job because it is a known and fixed location.
This command can be used to return the printer head to the end point of a particular axis. Normally, simply writing G28 would mean that the printer head moves to the end stop or the farther end of the X, Y, and Z axis. However, if “G28 X” is the code, then the printer head only moves to the end point along the X-axis. Similarly “G28 X Y” would mean moving to the end point of the X and Y axes while leaving the Z-axis unaltered.
So, the next time you see any line of G-code with G28, you will know what that means.
Straight line or linear movement – G1
Perhaps the most commonly used command in any G-code file is G1. It instructs the printer head to move in a straight line along a specified axis/axes to an exact user-determined location. This command may be used along any one axis or along two/three axes.
The X, Y, and Z co-ordinates mentioned would normally be absolute points along the printing bed. However, using other commands like G91 (as explained in the next point below) can instruct the printer head to move at relative distances with respect to the existing position.
Along with the G1 command and the location co-ordinates, other commands like F and E are also used to specify the speed of movement and the amount of filament to be pushed through the nozzle of the extruder.
So, for example the code “G1 X20 Y30 E10 F1200” would mean that the printer head is to move to x=20mm and y=30mm on the printer bed. While doing so, the printer has to push 10mm of filament through the extruder and move at the speed of 1200 mm/min. The unit of speed can be mm/min or mm/sec depending on the settings you create in the slicing software.
Resetting the filament position – G92
At the start of every new layer, the position of the filament is often reset. Normally, after printing a layer of a 3D model, a series of E commands are used to extrude the filament. However, once that layer is finished, then resetting the filament back to 0 will mean that all future commands for the next layer are relative to that reset zero position using the G92 command.
For example, writing the code “G92 E0” means that the existing filament position is now the new “zero” position and a new phase or layer of printing can begin.
Relative or absolute positioning – G90/G91
The 3D printer head can instructed to move to an absolute coordinate or by a relative distance from its existing position. The G-commands G90 and G91 are used for such positioning. For example, let us assume that a code line reads as follows:
G1 X20 F3200”
This means that the printer head is to move to X=20mm position on the bed. So, regardless of where the printer head currently is, it has to move to that particular coordinate.
Now contrast that movement with a code line which reads as follows:
G1 X20 F3200”
This basically means that the printer head has to move 20mm to the right of wherever it currently rests. This command is particularly helpful when you are not sure what the current or existing location of the printer head is and simply want it to move along a particular axis by a fixed distance.
Normally, the G-code file created by the slicing software uses absolute positioning or G90 commands to instruct the printer head on where to move. The slicing software determines the X, Y, and Z coordinates based on the 3D model that is fed to it by the user. It comes up with the distances and the axes automatically saving the user a lot of time and effort.
Extruder temperature – M104/M109
When you begin any 3D print job, the first thing you have to do is heat up the extruder. Once the extruder reaches a specific temperature, then it is ready to push out the filament and move along the design path of the 3D model being printed.
You can heat up the extruder in two distinct ways. The first one is by instructing the extruder to begin heating and work towards reaching a specific temperature. While that happens, the 3D printer goes about performing its other functions. The second way is by instructing the extruder to heat and reach a certain temperature before anything else can happen. The command M104 is used to let other commands run while the extruder heats, while the command M109 is used to complete the extruder heating process before any other command can run.
Normally, an S command is also written alongside the M104/M109 command. The S command sets the temperature at which is extruder is to be heated. S150 would mean the extruder has to reach 150 degrees Celsius. Sometimes, a T command is also used if the 3D printer has two extruders. T0 is normally used to signify the right-sided extruder while T1 is used to signify the left-sided extruder.
Print bed temperature – M140/M190
A small juxtaposition of the numbers from the previous commands leads us to two new commands. M140 is similar to M104 in that it allows the printer bed to heat up to a certain temperature. While the bed heats up, the printer runs the other commands.
M190 is similar to M109 and it directs the printer bed to heat up to a specific temperature. While the heating happens, no other command is allowed to run. Since the printer bed takes much longer to fully heat up to a specific temperature than an extruder, your 3D printer may go into pause mode. So, you may want to use M140 instead of M190 and work simultaneously on other components of the printer while the heating concludes.
An S command is also used in conjunction with the M140 and M190 commands to specify the temperature to which the printing bed is to be heated. Getting the printing bed temperature is very important because it affects the adhesion quality of the very first printed layer to the print bed.
External Cooling Fan Speed – M106
If your 3D printer has an external cooling fan, then you can set its speed using the M106 command. The external fan is different from another fan that cools the extruder. Most 3D printers tend to have such external cooling fans but there are a few models that do not have such fans. So, always check your printer before you use this command.
The M106 command is pretty easy to use. You simply have to write M106 and then write another S command next to it with a number notifying the speed. The numbers can range between 0 and 255 with 0 meaning fan off and 255 meaning fan at full speed. So for example, if you want to run the fan at full speed, you simply write “M106 S255”.
With all of this knowledge, you should now be able to understand the basics of G-codes. You will have enough information to start your 3D printer, send some G-code to it, and begin playing around with it. If you are unsure about any piece of G-code, then you can copy one line from that piece of code and send it to the printer. You can then observe how the printer reacts and whether it does what you intended it to do. Happy printing!