1. What is G-code?
  2. Syntax
  3. G-code command reference
  4. Common G-code commands
  5. Initialization
  6. The main commands
  7. Wrapping things up
  8. Why knowing G-code is useful

What is G-code?

G-code is the most widely used CNC and 3D printing programming language. It is used to provide commands for controlling the movement of tools as well as controlling the temperature of the 3D printer.

The discussion below will focus mostly on using G-code for 3D printing.

Syntax

Each line of G-code contains a command, often followed by one or more parameters.

An example of a line of G-code looks like this:

G1 X500 Y500 E0.3

G1 is a command to move the hot end.

X specifies how far to move the hot end along the x-axis, in this case 500. Similarly, Y specifies how far to move the hot end along the y-axis. There is also a Z parameter that can be used to move the hot end up or down.

E specifies how much filament should be extruded while the hot end is being moved.

These commands are often modified based on previous commands, which may set whether the given x and y values should be interpreted as absolute or relative values, for example.

Blank spaces and blank lines are ignored.

Comments are set off by a semicolon.

Although G-code is somewhat standardized, there are variations that have been introduced to add additional features for controlling specific machines. Many of the machine-specific commands start with an M instead of a G.

G-code command reference

This is a great website to use as a reference for G-code commands:

Gcode | Marlin Firmware (marlinfw.org)

Common G-code commands

The most commonly used G-code commands include:

CommandMeaning
G1linear move
G21set units to mm
G28home all
G90use absolute coordinates
G92set current position
M83use relative coordinates for extruder
M84disable motors
M104set extruder temperature
M107turn fan off
M140set bed temperature
M201maximum acceleration
M203maximum feedrate
M204set acceleration
M205set jerk limits

Initialization

Typically, G-code instructions will start with a set of instructions to set the initial parameters before beginning printing. This can include turning on the heaters on the hot end and build plate, homing the print head, etc.

I copied this header from G-code produced by PrusaSlicer when I sliced a standard CAD-produced .stl file.

M201 X500 Y500 Z100 E5000 ; sets maximum accelerations, mm/sec^2
M203 X100 Y100 Z10 E60 ; sets maximum feedrates, mm/sec
M204 P500 R1000 T500 ; sets acceleration (P, T) and retract acceleration (R), mm/sec^2
M205 X8.00 Y8.00 Z0.40 E5.00 ; sets the jerk limits, mm/sec
M205 S0 T0 ; sets the minimum extruding and travel feed rate, mm/sec
M107
G90 ; use absolute coordinates
M83 ; extruder relative mode
M104 S210 ; set extruder temp
M140 S40 ; set bed temp
M190 S40 ; wait for bed temp
M109 S210 ; wait for extruder temp
G28 ; home all
G1 Z2 F240
G1 X2 Y10 F3000
G1 Z0.28 F240
G92 E0.0
G1 Y190 E15.0 F1500.0 ; intro line
G1 X2.3 F5000
G1 Y10 E30 F1200.0 ; intro line
G92 E0.0
G21 ; set units to millimeters
G90 ; use absolute coordinates
M83 ; use relative distances for extrusion

The main commands

The body of the code contains instructions to move the print head while extruding filament, occasionally retracting the filament, and moving the print head up to the next layer when a layer is finished.

Wrapping things up

This is an example of the G-code that is placed at the end to wrap things up and shut things down. This includes commands to move the print head out of the way and move the build plate forward for easy access to the print, as well as turning off the heaters, fan and motors.

G1 E-5.000000 F9000 ;retract filament
; Filament-specific end gcode
;END gcode for filament
M104 S0 ; turn off temperature
M140 S0 ; turn off heatbed
M107 ; turn off fan
G1 Z20.04 F600 ; Move print head up //this may need to change for tall prints
G1 X0 Y200 F3000 ; present print
M84 X Y E ; disable motors

Why knowing G-code is useful

Most users of 3D printers never need to exam the G-code that is produced by their slicer. Nevertheless, there are some reasons that an understanding of G-code can come in handy.

One simple example would be to convert G-code for a print using PLA to a print using ABS. If you only have the G-code without the original .stl, you could modify the G-code directly by changing the temperatures of the hot end and the build plate to the appropriate temperatures for ABS.

There are often ways to tell your slicer to add additional G-code at specific locations, or to replace certain G-code commands with others, for instance, when a 3D printer is using a different version of G-code.

More advanced uses of G-code include:

Comments

Leave a comment