G-Code Basics

Introduction

An important piece of 3D Printing technology is the instructions that make them replicate a desired model – those instructions are G-Code!

What is G-Code?

Think about what you need a 3D printer to do fundamentally. You need to move, extrude, set some temperatures, and that’s sort of it (I realize that a ton of smaller things have been omitted). G-Code is a certain specification of commands that executes all of those things and more. We know that a slicer cuts an STL into a list of movements that will result in our print, and it turns out, not that many commands are really needed.

As a note, we are using the Marlin flavor of G-Code, but there are several variants available. I’d familiarize yourself with this excellent reference: https://marlinfw.org/meta/gcode/ I use it all of the time.

Movement

The first commands we’ll look at are the movement commands, G0 and G1. Functionally in Marlin they do the same thing: move the motors, including the extrusion motor. In fact, they can be used interchangeably, but typically G0 is reserved for non-extrusion movement. The syntax for the command looks like:

G1 [E<pos>] [F<rate>] [X<pos>] [Y<pos>] [Z<pos>]

Where:

  • E: An absolute or relative coordinate on the E (extruder) axis (in current units). The E axis describes the position of the filament in terms of input to the extruder feeder.
  • F: The maximum movement rate of the move between the start and end point. The feedrate set here applies to subsequent moves that omit this parameter.
  • X: An absolute or relative coordinate on the X axis (in current units).
  • Y: An absolute or relative coordinate on the Y axis (in current units).
  • Z: An absolute or relative coordinate on the Z axis (in current units).

From what I’ve seen with most systems, the arguments don’t have to be in order, and unneeded arguments can be omitted.

Let’s look at a little example of this:

Relative vs Absolute

A modifier on movement is if you are in Relative movement mode or Absolute movement mode. In most cases, absolute movement is used, where the argument of a G1 (or G0) command indicates a coordinate to move to relative to (0,0,0). Where in relative movement, the G1 command indicates the number of units to be moved relative to the current position of the end effector. The commands are free of any argument, meaning that you can put the printer in absolute mode with:

G90

and you can go into relative movement with

G91

The printer will stay in your specified mode until you select a different mode.

Control Commands

There are a few control commands needed to put the printer in a desirable state:

Temperature Control

One of the biggest commands we care about is our temperature control commands. The extruder and bed have their own commands. The extruder’s controlling command is:

M104 [B<temp>] [F<flag>] [I<index>] [S<temp>] [T<index>]

Where:

  • B: The max auto-temperature.
  • F: Autotemp flag. Omit to disable autotemp.
  • I: Material preset index. Overrides S
  • S: Target temperature.
  • T: Hotend index. If omitted, the currently active hotend will be used.

Normally your command will just use the S argument (IE setting your extruder to 200C would be M104 S200). The other parameters are for more fancy features.

Setting the bed temperature looks like:

M140 [I<index>] [S<temp>]

One thing to note, is that a temperature set command will normally be followed with an M109 command (or M190 for Bed Temp), which tells the controller to wait for the temperature to be reached before allowing any more G-Code commands to be ran.

Digital Control

The M3 command is used to “turn a spindle on” – this is for milling machines, but I’ve used it for all sorts of other purposes. Fundamentally, you can use it to trigger a digital output on your board.

M3 [I<mode>] [O<power>] [S<power>]
  • I: Inline mode ON / OFF.
  • 0: Spindle speed or laser power in PWM 0-255 value range
  • S: Spindle speed or laser power in the configured value range (PWM 0-255 by default)

Honorable Mentions

There are a couple more commands I’d like to mention that I also think are important:

Arc Movement

You’ll notice that the first movement commands that we learned are only for straight lines – so how do we do curves? Well, normally, a curve is just broken up (approximated) as a bunch of smaller line segments connected end-to-end (sort of how OpenSCAD makes circles from lines). However there is a specialized command for Arc Movement:

G2 [E<pos>] [F<rate>] I<offset> J<offset> [P<count>] R<radius> [S<power>] [X<pos>] [Y<pos>] [Z<pos>]

Where:

  • E: The amount to extrude between the start point and end point
  • F: The maximum rate of the move between the start and end point
  • I: An offset from the current X position to use as the arc center
  • J: An offset from the current Y position to use as the arc center
  • P: Specify complete circles
  • R: A radius from the current XY position to use as the arc center
  • S: Set the Laser power for the move.
  • X: A coordinate on the X axis
  • Y: A coordinate on the Y axis
  • Z: A coordinate on the Z axis

Dwell

Sometimes you just want the printer to wait. I’ve done this for custom applications.

G4 [P<time (ms)>] [S<time (sec)>]

Where:

  • P: The amount of time to wait in milliseconds
  • S: the amount of time to wait in seconds

Get Current Position

When you’ve got a custom application controlling a printer or other G-Code controlled device, sometimes it’s handy to get the state of the printer. That information can be gathered with the M114 command.

M114 [D] [E] [R]

Where:

  • D: Detailed information
  • E: Report E stepper position
  • R: Real position information

So for example, typing in M114 will spit out the current position:

X:0.00 Y:127.00 Z:145.00 E:0.00 Count X: 0 Y:10160 Z:116000

This information can then be received, parsed, and utilized by your custom program.

Print Friendly, PDF & Email