What is 3d Printing G-Code?


G-Code is the control code that you use to print 3d models on your 3d printer. You will need to convert your design file (most likely in the form of an STL file) and turn it into a series of G-codes, the language that your printer compiles and uses. So how do you do this?

G-Code is the control language generated by your slicer software when it converts an STL design file into a G-Code control file for the printer. The slicer will combine movement, extrusion, heating and sensing commands together in a sequence that, when interpreted by your 3d printer, will produce a 3d model. This process is known as ‘slicing’ the STL file, and is an intermediate step. It is this .GCODE file that you will load into your 3d printer.

For the casual user, you will never need to know a single line of G-Code. Your slicer will take care of any conversions you need to do. But for some, knowing some G-Code can be useful in making helpful utilities, or editing them once you’ve made them. Let’s have a closer look.

The examples I use are written for the Ender 3 printer that I have. It is unlikely that all the commands are universal enough to be run on all 3d printers. You’ll want to consult the manufacturer’s website and documentation to determine if the code will work on that machine.

What Is G-Code? What Does It Look Like?

G-Code is so named because the commands start with the letter G. Take, for example, the command G01. It says move from the current position to a specific point in a straight line. So, you could use the command ‘G01 X0 Y0 Z0 F2400’. It says to go from where it currently is to the position X=0, Y=0 and Z=0 at a speed of 2400mm per hour (or 40mm per second). Essentially, this is the command that you’d use to order the print head to move to the home position. 

X=0, Y=0, Z=0 is the position that describes the left front corner of the build plate. Any value of X above 0 moves the print head to the right. Any value of Y above 0 moves the print head back, and any value of Z above 0 moves the print head up.

G-Code is just text. You could open and edit it in a text editor or Notepad, if you like. Most coders like to put each command on its own line, so the first thing in the line is the G-Code and what follows it is any arguments (an argument is a further set of details for the command, like our X, Y, Z positions and F speed). The file extension that the 3d printer will look for is .GCODE. If it finds a file with that name, it will try to interpret it and run it when instructed to do so.

What Is M-Code? Is There A .MCODE File, Too?

M-Code is the same as G-Code. It is a command that the printer will interpret, and it is found in the same G-Code file. So no, there is no separate M-Code file. Essentially, G-Codes are universally understood by printer that use G-Code, and M-Codes are those codes that are specific to individual printer lines. Just to make things a little more standard, many M-Codes are now standard across a lot of 3d printers.

M-Codes start with the letter M. An example would be M104. This code heats up the extruder, so having the command M104 S200 T0 will instruct the printer to heat extruder T0 (if there’s only one extruder, it’s called T0, but if you have a dual extruder printer, you would have T0 and T1). The S value is the degrees in Celsius. So we’re asking the printer to heat the extruder up to 200 degrees.

What Do You Use To Write G-Code and M-Code?

Normally, you wouldn’t write G-Code and M-Code at all. That’s done automatically by your slicer program when it converts a design STL file into a .GCODE file for use in the printer. The slicer, like its name suggests, is slicing up the model into slices in the XY plane. Each slice will represent a layer that the printer will print. The G-Codes and M-Codes generated tell the printer how to extrude the plastic from the extruder and where to deposit it on the build plate or previously printed parts of the model.

If you want to write your own G-Code, you can use any text editor that will output a .TXT file. Once you’ve saved your file after writing it, you can simply rename it to have a .GCODE file suffix. Some coding editors (like those that Java developers and C++ developers use) will also be good for writing G-Code. I never did anything more than a few lines of G-Code, so Notepad was good enough for what I needed.

What Are Some Common G-Code and M-Code Commands?

G28 – Home

You can use G28 to move the print head to the X=0, Y=0, Z=0 position. If you only want to home the print head to the X and Y home, but leave the Z height unchanged, you can use ‘G28 X Y’, for example, or to only home the Z axis but leave the X and Y unchanged, use ‘G28 Z”.

G01 – Move (X, Y, Z, F, E)

This moves the print head. If you want the print head to go to a position of X=20mm, Y=30mm and Z=40mm (so 20mm to the right of the front, left corner of the print bed, back 30mm and up 40mm in the air), issue the command ‘G1 X20 Y30 Z40. You can use the F argument to set the speed that it will travel there. The speed is set in millimeters per minute (a strange measure, but there it is!) To convert to millimeters per second, divide by 60. So, the same command above but at a speed of 50mm/s would be ‘G1 X20 Y30 Z40 F3000’.

You can extrude filament using the same command. So, you could use the E argument to extrude an amount of filament. The same command might look like this, with the filament extrusion: ‘G1 X20 Y30 Z40 F3000 E1’. This is where it gets tricky. The E command will extrude a number of millimeters of filament before being extruded. Not after. The extruder squeezes the filament in the process of extruding (for example, the stock Ender 3 uses 1.75mm filament, but it’s extruded out a 0.4mm nozzle). It takes some math to figure out how many millimeters to extrude for the distance that you’re traveling. That’s beyond my pay grade. For extruding filament, I leave it to the slicer to calculate (and it generally does this perfectly!)

G90 – Set Absolute Positioning

This tells the printer that when you give G01 move commands, you’re going to send an exact position. So, if you sent ‘G01 X10’, you would send the print head to the X=10mm from the left of the print bed position. You can either have the printer in Absolute Positioning or in Relative Positioning. Absolute means that the command says what point to put the print head.

If you’re in Relative Positioning mode, issuing the G90 command will change it to Absolute Positioning mode. If you’re already in that mode, issuing the command does nothing.

G91 – Set Relative Positioning

This tells the printer that when you give G01 move commands, you’re sending offsets from the current position. So ‘G01 X10’ sends the print head to the right (along the X-axis) 10mm from where it was. So if you were at an X position of 48mm, the print head would end up at an X position of 58mm. This is useful for drawing on an angle, as it’s easier to say ‘move to the left by 10mm and move back 10mm’ than calculating the current position and then calculating the end position and sending the G01 command to go to that absolute address. It’s quite handy.

If you’re in Absolute Positioning mode, issuing the G91 command will change it to Relative Positioning mode. If you’re already in that mode, issuing the command does nothing.

M104 – Heat Extruder

This one’s simple. You’re telling the extruder to heat up to a certain temperature. Once you issue the command, however, the printer can do the next command and doesn’t need to wait to heat up. You can set the extruder number (the default is T0, but if you have more than one extruder, they’re numbered up, so the next one is T1 and so on). The S argument sets the temperature in degrees Celsius. So, issue ‘M104 S190 T0’ to heat your extruder to 190 degrees Celsius.

M109 – Heat Extruder but don’t do anything else until it achieves temperature

This is also simple. It does the same thing as M104, except the printer will pause until the temperature is reached. It won’t do anything else. Once that temperature is achieved, the printer will process the next command. Issue ‘M109 S190 T0’ to have the extruder heat to 190 degrees Celsius and wait until that temperature is achieved.

M140 – Heat Print Bed

This is just like the M104 command, in that you can go on to do the next command without waiting for the bed to heat. So ‘M104 S50’ will heat the bed to 50 degrees Celsius but won’t pause. There’s no need for a T argument, as there’s only one print bed.

M190 – Heat Print Bed but don’t do anything else until it achieves temperature

Just like the M109 command, this one will pause until the heat has been reached. Again, there’s no T argument. So ‘M109 S50’ will heat the bed to 50 degrees Celsius, but it will pause until the temperature is reached. It will continue on to the next command once temperature is reached.

M0 – Pause For User Input

Just like it sounds, pause the execution of the code until the user pushes the button on the front of the printer. Then, go on to the next command. You can display a message on the screen by simply typing it as an argument after the command. So issue ‘M0 Press to Continue’ to pause the code, display “Press to Continue” and then do nothing until the button is pressed.

M117 – Show A Message On the Screen

The M117 command is similar to the M0 command in that the message after the command is displayed on the screen of the printer. It won’t pause or anything. It’s just informational. Issue ‘M117 Starting’ for the screen to show “Starting” on it. It’s as simple as that!

The SemiColon (;) For Comments

You can add a semicolon to the end of your line and write anything you like. The printer will not try to execute anything past the semicolon. This is useful to make notes telling you what the command is doing.

This is more than I ever needed for the simple G-Code utility that I use. I have a short program that goes to just in from the four corners of the print bed, so that I can test the height of the print bed, to level the printer. Here’s what it looks like:

  • M117 Starting ;show ‘Starting’ on the screen
  • G28 ;home
  • G90 ;set absolute positioning
  • G1 Z2 ;raise up the print head to 2mm
  • M190 S50 ;heat up the print bed to 50 degrees Celsius and pause until we get there
  • G1 X20 Y20 ;move to a position 20mm right and 20mm back from the front left corner.
  • G1 Z0.02 ;drop the print head to 0.02mm above the print bed so you can test with a piece of paper
  • M0 Press to Continue ;pause the program until the button is pushed, display ‘Press to Continue’ on screen
  • G1 Z2 ;raise up the print head to 2mm
  • G1 X200 Y20 ;move to a position 200mm right and 20mm back from the front left corner.
  • G1 Z0.02 ;drop the print head to 0.02mm above the print bed so you can test with a piece of paper
  • M0 Press to Continue ;pause the program until the button is pushed, display ‘Press to Continue’ on screen
  • G1 Z2 ;raise up the print head to 2mm
  • G1 X200 Y200 ;move to a position 200mm right and 200mm back from the front left corner.
  • G1 Z0.02 ;drop the print head to 0.02mm above the print bed so you can test with a piece of paper
  • M0 Press to Continue ;pause the program until the button is pushed, display ‘Press to Continue’ on screen
  • G1 Z2 ;raise up the print head to 2mm
  • G1 X20 Y200 ;move to a position 20mm right and 200mm back from the front left corner.
  • G1 Z0.02 ;drop the print head to 0.02mm above the print bed so you can test with a piece of paper
  • M0 Press to Continue ;pause the program until the button is pushed, display ‘Press to Continue’ on screen
  • G1 Z2 ;raise up the print head to 2mm
  • G28 ;home
  • M140 S0 ;cool down the bed to ambient temperature
  • M117 Complete ;show ‘Complete’ on the screen

That’s it! You can use this to test the bed level at the four corners of your print bed. My printer has a print bed of 220mm by 220mm, so this will test 20mm in from the corners. Once you’re done, you can run it again and retune the height of the bed until you’re satisfied.

Simulating Your Code Without A 3d Printer

Cura Slicer from Ultimaker and Slic3r Prusa Edition come with G-Code simulators. You can load your G-Code and see what it will do, without loading it into an actual 3d printer. Have a look at their documentation on how to use this feature for your software.

It’s a good way to test your code to see if the sequencing is correct. It gives you a way to test out your code before trying it out on your printer. I recommend commenting your code, so that you can see what it’s supposed to do. You may remember today, but in six months when you’re troubleshooting it or wanting to edit it, those comments will come in handy!

G-Code: Not Just For 3d Printers!

Learning G-Code can be useful for more things than just 3d printers. Laser engravers, laser cutters and CNC machines all use a form of G-Code to get their jobs done. They likely have different M codes for specific things like firing a laser or spinning a cutting head, but it’s very similar stuff. G23 is still home, if they’re using the same G-Code as the Ender 3 uses!

Related Questions

Do all 3d printers use G-Code?

Most 3d printers use some form of G-Code. However, there are some printers that use different command formats. Alternatives are rare, but some complain that G-Code takes up too much space (since it is human readable) and want more efficient coding systems. Still, G-Code remains dominant for most 3d printers.

What is M-Code?

M-Codes are miscellaneous codes that, in many cases, are machine specific. The M in M-Code refers to ‘Machine’. Most of these commands are outside of the movement and extrusion of the filament. Most manufacturers will provide online documentation for the M-Codes that are specific to their printer on their website. You write .GCODE files with both G and M commands in it, and they will execute in order.

Recent Content