G-Code Tutorial for Beginners: Improve Your 3D Printing Skills
By Anatol Locker
G-code is the language used by computers to communicate with 3D printers. Using G-code, a computer tells a printer when, where, how to move and how much to extrude throughout the entire print process.
If you have never dealt with G-code so far, that’s normal. Slicers like Cura and Simplify3D generate G-code “automagically” from CAD models, so most users never see or program a single line of code. However, if you want to develop a deeper understanding of 3D printing, it is essential to know about this programming language.
A knowledge of G-code will give you 3D printing superpowers. People who know G-code are able to troubleshoot their printers better, control every aspect of the print process and identify and prevent print failures much before they happen.
If that sounds interesting, this post is for you. Our aim is to get you started with G-code basics. After reading this post, you will be able to:
Read and understand G-code
Write it yourself and test it online
Use the G-code Preview functionality of Slicers to troubleshoot complicated prints
Let’s get started!
What is G-code?
G-code stands for “Geometric Code”. Its main function is to instruct a machine head how to move geometrically in 3 dimensions. However, it can also instruct a machine to do non-geometric things. For example, G-code can tell a 3D printer to extrude material at a specified extrusion rate or change its bed temperature.
In formal terms, G-code is a numerical control programming language. For those who know how to program, G-code is an easy programming language. It is rudimentary and does not have advanced constructs like variables, conditionals, and loops.
For those who don’t know about programming languages, you can think of G-code as sequential lines of instructions. Each line tells the printer to do a specific task. The printer executes the line one by one until it reaches the end.
How to read G-code
So, how does a line of G-code look like? Here is a typical example:
G1 X-9.2 Y-5.42 Z0.5 F3000.0 E0.0377
This particular line tells the printer to move in a straight line towards the destination coordinates X=-9.2, Y=-5.42, and Z=0.5 at a feed rate of 3000.0. It also instructs the printer to extrude material at a rate of 0.0377 while it is moving.
How did we read and interpret that? It’s quite easy. Every line of G-code starts with a command. In this case, the command is G1.
G1 X-9.2 Y-5.42 Z0.5 F3000.0 E0.0377
It means “move in a straight line in a controlled fashion”. You can look up the meaning of every G-code command in a table that we have provided at the end of the article. We will also go through the most important G-code commands in a later section.
The code snippets that appears after the command are called arguments.
G1 X-9.2 Y-5.42 Z0.5 F3000.0 E0.0377
Each argument tells the printer about how to execute the command. The arguments start with an English letter and then specify a value. For example, X-9.2 means a destination X coordinate of -9.2. F3000.0 means a Feed rate(F) of 3000.0. E0.0377 means an Extrusion rate(E) of 0.0377.
Try reading the following line of G-code now.
G1 X5 Y5 Z0 F3000.0 E0.02
If you interpreted it to mean “move towards X=5, Y=5, and Z=0 in a straight line at a feed rate of 3000.0 while extruding material at the rate 0.02”, then you have already learned how to read G-code!
Commands which start with the letter G are geometric commands. They tell the printer head how to move, but this is clearly not enough to control all aspects of a 3D printer. What if you needed to tell the printer to turn the motor off or raise the bed temperature? For these non-geometric tasks, G-code implementations also define another set of commands which start with the letter M. They are aptly called M Codes. For example, the command M140 sets the bed temperature, and the command M190 tells the printer to wait for the temperature to reach the target.
Each English letter that you encounter in G-code will have a specific meaning. For example, we learned that G means a geometric command, M means a non-geometric command, X means the X coordinate, Y means the Y coordinate, F means Feed rate and so on. For your reference, here’s a table with the meaning of every G-code letter.
Standard GCode command, such as move to a point
RepRap-defined command, such as turn on a cooling fan
Select tool nnn. In RepRap, a tool is typically associated with a nozzle, which may be fed by one or more extruders.
Command parameter, such as time in seconds; temperatures; voltage to send to a motor
Command parameter, such as time in milliseconds; proportional (Kp) in PID Tuning
A X coordinate, usually to move to. This can be an Integer or Fractional number.
A Y coordinate, usually to move to. This can be an Integer or Fractional number.
A Z coordinate, usually to move to. This can be an Integer or Fractional number.
Additional axis coordinates (RepRapFirmware)
Parameter – X-offset in arc move; integral (Ki) in PID Tuning
Parameter – Y-offset in arc move
Parameter – used for diameter; derivative (Kd) in PID Tuning
Parameter – used for heater number in PID Tuning
Feedrate in mm per minute. (Speed of print head movement)
Parameter – used for temperatures
Parameter – not currently used
Length of extrudate. This is exactly like X, Y and Z, but for the length of filament to consume.
Line number. Used to request repeat transmission in the case of communications errors.
Checksum. Used to check for communications errors.
A simple example of G-code in action
Now that you know how to read a line of G-code, let’s look at a simple example in action. The following video shows G-code at work in a cutting machine (not a 3D printer). The cutting machine will cut a circular edge in a rectangular slab. The G-code instructs the cutter on how to move to achieve the desired result.
Do not worry that the video is about a cutting machine. The geometric aspects of G-code works similarly for all machines that have a machine head. In the case of the 3D printer, the nozzle is the head. For the cutting machine, the head is the cutter. That’s the only difference. All other geometric aspects of the code remain the same.
If you understand the cutter’s movements, you will also know how to move a print head.
The most important G-code commands
In the last section, we discussed the G1 command, which means “move the nozzle in a controlled fashion in a straight line”. This is just one of the many G-code commands. In this section, we will discuss other important G-code commands that are used frequently.
G0 or “rapid motion”
The G0 command tells the print head to move at maximum travel speed from the current position to the coordinates specified by the command. The head will move in a coordinated fashion such that both axes complete the travel simultaneously. The nozzle will not extrude any material while executing this command. This command is usually used to bring the nozzle rapidly to some desired coordinates at the start of the print or during the print.
Example: G0 X7 Y18
G1 or “controlled motion”
The G1 command tells the print head to move at specified speed from the current position to the coordinated specified by the command. The speed is specified by the Feed rate parameter F. The head will move in a coordinated fashion such that both axes complete the travel simultaneously. The printer can extrude material while executing this command at an extrusion rate specified by the Extrusion rate parameter E. Most of the 3D printing happens while executing this command. If you open the G-code file for an actual 3D printing process, you will see a lot of G1 commands.
Example: G1 X7 Y18 F500 E0.02
G17/G18/G19 or “set planes”
These G-code commands set the plane in which the nozzle should move. Typically, G17 is the default for most machines and it denotes the X-Y plane. G18 denotes the Z-X plane and G19 denotes the Y-Z plane.
G20/G21 or “set units”
These commands set the G-code units. G20 denotes inches while G21 denotes millimeters. This makes a big difference because
G0 X7 Y18
means “move rapidly to X=7 inches and Y=18 inches” while
G0 X7 Y18
means “move rapidly to X=7 mm and Y=18 mm”.
G28 or “homing”
A G28 command tells the machine to go to its home position. A home position can be defined by the G28.1 command as follows.
G28.1 X0 Y0 Z0
G90 or “absolute mode”
Absolute mode tells the machine to interpret coordinates as absolute coordinates. This means a command
will send the machine head to the coordinate X=10.
G91 or “relative mode”
The relative mode is the opposite of the absolute mode. G91 tells the machine to interpret coordinates as relative coordinates. If the machine is currently at X=10, then the following commands
tell the machine to move 10 units in the X direction from its current position. At the end of the operation, the machine head will be located at X=20.
G2 or “clockwise motion”
G2 tells the machine to move clockwise starting from its current location. The endpoint is specified by the coordinates X and Y. The center of rotation is specified by the parameter I, which denotes the X offset of the current position from the center of rotation. J denotes the Y offset of the current position from the center of rotation.
G21 G90 G17
G0 X6 Y18
G2 X18 Y6 I0 J-12
G3 or “counterclockwise motion”
Just like the G2 command, the G3 command creates a circular motion but in the counterclockwise direction.
G21 G90 G17
G0 X-5 Y25
G3 X-25 Y5 I0 J-20
If you look at any real world G-code file, you will find that in addition to G-code commands and arguments, it also contains things written in plain English. Here’s an example:
G0 X-25 Y5 ; rapid movement to X=-25 and Y=5
The English text will always be preceded by a semicolon, as you can see in the above line.
Programmers often need to write down explanations in plain English so that other programmers can understand the motivation behind a certain line or section of code. In fact, forget about other programmers! If you are looking at your own code afta an year, chances are that you will have forgotten why you coded things in a certain way and would have a hard time figuring things out again.
To solve this problem, G-code can include code comments. Comments are written after adding a semicolon punctuation mark.You can write anything after adding a semicolon, but most often it is used to explain the rationale behind the code in a human friendly way. Anything that appears after a semicolon character in a line is ignored by the printer while executing the G-code and is only meant for human eyes.
Here is another example of a line that has a code comment.
G1 X-25 Y5 ; I am a code comment!
The structure of a full-fledged G-code program
We are now in a good position to look at actual G-code that is used for printing a 3D model.
Most G-code programs contain three important sections. The first section initializes the printer for the printing process. The second section instructs the printer to print the model. The third section resets the printer to its default configuration after the print finishes. Let’s take a look at these sections one by one.
1. Initialization phase
Certain tasks need to be performed before a print can begin. For example, we need to heat the print bed, heat the extruder, purge the nozzle, bring the nozzle to the start position etc. These tasks form the first section of any program.
Here are the first five lines of initialization G-code from an actual 3D printing task. You should be in a position to read and understand them at this point, with help from the reference table at the end.
The first line sets the coordinates to absolute positioning. The second line tells the extruder to interpret the extrusion rate as absolute values. The third line turns the fan on, but sets the speed to 0, which essentially means that the fan is off. The fourth line sets the bed temperature to 100 degrees. The fifth line tells the printer to wait till the bed temperature reaches the desired value, in this case, 100.
During the initialization phase, the printer will not extrude any material except when it is purging the nozzle. This is an easy to way to figure out when the initialization phase stops and the actual printing begins. During the actual printing, the printer will be extruding material at almost every step.
2. Printing phase
A 3D printer prints a model layer by layer. Slicers like Simplify3D or Cura typically slices a 3D model into many horizontal layers that stack on top of each other to create the final print.
Therefore, the print phase consists of many movements in the X-Y plane (printing a single layer), then one movement in the Z direction (move to next layer) followed by many movements in the X -Y plane again (print the next layer).
Here is how it looks like.
G1 X108.587 Y111.559 F525 ; controlled motion in X-Y plane
G1 X108.553 Y111.504 F525 ; controlled motion in X-Y plane
G1 Z0.345 F500 ; change layer
G1 X108.551 Y111.489 F525 ; controlled motion in X-Y plane
G1 X108.532 Y111.472 F525 ; controlled motion in X-Y plane
3. Reset the printer
Finally, when the printing is over, some final lines of G-code bring the printer to a reasonable default state. For example, the nozzle is brought back to the origin, the heating is turned off (both for the bed and the extruder) and the motors are disabled.
G28 ; bring the nozzle to home
M104 S0 ; turn off heaters
M140 S0 ; turn off bed
M84 ; disable motors
Input and Output
Till now, we have only talked about the computer sending G-code to the printer, so it seems like the communication is one way. But 3D printing actually involves a two-way communication between the computer and the printer. Here’s how it works.
When you hit the print button on your computer, the 3D printing software starts sending the G-code to the printer, one line at a time. The printer executes the line and responds back to the computer. If the response indicates no error, the computer then sends the next line of code to be executed.
The printer’s response usually follows the following format:
<response> [<line number to resend>] [<current printer parameters>] [<Some debugging or other information>]
<response> can be ok, rs or !!.
Ok means that no error has been detected. This prompts the computer to send the next line of G-code to the printer.
Rs means “resend the instruction”. This is usually followed by the line number to resend.
Two exclamation marks(!!) implies hardware error. The machine shuts down immediately in this case and the print job is aborted.
In addition to these 3 responses, the printer might also report printer parameters like temperature, coordinates of the nozzle etc. to the computer.
Temperature is reported in response to a M105 command. The format of the response is
where T indicates the extruder temperature and B indicates the bed temperature. If the machine does not have a temperature sensor, then -273 is returned as a value.
The coordinatesare reportedd in response to a M114 and M117 commands. The format of response is
C: X:9.2 Y:125.4 Z:3.7 E:1902.5.
Here, C stands for “coordinates follow”. This is followed by current X, Y, Z coordinates and other information.
G-code Visualization Tools
Now that you know how to write G-code, it’s your turn to write some code and test your understanding. You can use an online G-code visualization tool, where you can write some G-code and see the machine head move according to your instructions. It’s a lot of fun! We recommend that you try out this online visualization tool to test your skills.
Slicing software like Simplify3D or Cura also come with a G-code viewer. In the G-code viewer, you will be able to visualize the path of the extruder for actual 3D printing tasks. Check out this must-see video for an excellent demonstration of the G-code viewer in Simplify3D.
Preventing print failures with G-code preview
The G-code viewer can be the difference between a successful and failed print for tricky 3D models. In general, whenever you want to print a complicated 3D model, we advise that you run the G-code viewer and go through the print simulation step by step.
We need to do this because the automatically generated G-code is often not ideal. You will often find that there are problematic areas that do not have enough support, leading to a failed print. In this case, you need to modify the G-code to ensure successful printing. Most of the time, this can be done by adding additional support structures using the graphical interface. Here is a video that shows how to do this for a complicated model of a 3D puppy.
In conclusion, we learned about how a 3D printer prints a CAD model by following an instruction set written in G-code. We learned how to read G-code, and saw some realistic examples of G-code in action. We discussed the most common G-code commands and some ways of visualizing and testing them. Finally, we introduced G-code viewer, a common feature of Slicers, which can be used to prevent failed prints.
We hope that an understanding of G-code helps you become a more knowledgeable and powerful user of your 3D printer. If you found this article useful, share it with other 3D printing enthusiasts and spread the word. Do you have some questions or remarks about G-code? Let us know in the comments below!
Appendix 1: Compatibility notes
Each 3D printer comes with a firmware. There are many firmwares, and developers of these firmwares tend to implement different flavours of G-code. This leads to major compatibility issues. The G-code that works for one machine might not work for another.
This problem is usually solved by connecting the Slicer, which generates the G-code, to a machine specific post processing driver. The post processor detects the incoming G-code flavour and converts the G-code to the specific flavour that the firmware understands.
Therefore, the G-code that you see on the Slicer might not necessarily be the code being executed on the machine because of this subtle implementation detail.
Appendix 2: G-codes
List of all G-codes from Wikipedia
Appendix 3: M Codes
Insert table of M-codes from Wikipedia
The post G-Code Tutorial for Beginners: Improve Your 3D Printing Skills appeared first on All3DP.
April 28, 2017 at 11:01PM
via All3DPAll3DP http://ift.tt/2pHgRwP