Skip to main content

G-Code Overview

What is G-Code​

G-Code is the programming language of 2D plotters and 3D printers. It is a lower level programming language and therefore is typically difficult to understand. Below is a snippet of code to plot a rectangle. This library aims to abstract away these instructions so that more user friendly code, such as Python, can be used instead to generate G-Code. For more information on G

G21
F10000
M3 S0
G1 X0.000 Y-50.000 F10000
M3 S1000
G04 P0.25
G1 X0.000 Y0.000 F10000
G1 X50.000 Y0.000 F10000
G1 X50.000 Y-50.000 F10000
M3 S0
G04 P0.25
M2

Library Instructions​

Below are a collection of all the instructions used in this library. For a more thorough explanation, RepRap and Marlin are great (and dense) resources.

The square braces indicate a variable that can be passed to the specific G-Code instructions. For example the instruction G1 X[x] Y[y] takes in variables x and y, which could result in a G-Code instruction looking like G1 X10.0 Y5.0.

Comment​

Add a comment with a line starting with ;. This instruction is not read by the plotting device.

;The following instruction lifts the pen up.
M3 S0

F​

Set the movement speed f of the plotting device with F[f]. Can also be attached to individual G1 commands.

F10000

G1​

Move the plotting device to the specified x and y and z coordinates at a speed of f with G1 X[x] Y[y] Z[z] F[f]. Not all of the coordinates are required.

G1 X0.000 Y0.000 F10000
G1 X50.000 F10000
G1 Z10 F10000
G1 X50.000 Y-50.000 F10000

G4​

Pause for a period of time p with G4 P[p]. This command is useful in combination with a move command. A pause after moving will give the plotting device a chance to stop any vibrating that might impact the plotting quality.

; Lower plotter head
M3 S1000
; Pause for 0.25 seconds
G4 P0.25
;Point: 59.84807753012208, -48.2635182233307
G1 X59.848 Y-48.264 F10000
G20

G21​

Sets the units of measurements to mm.

G21

G28​

Home the plotting device. Will return the plotter to X = 0, Y = 0 and if supported, Z = 0. Not all plotting devices support this command.

G28

M2​

Marks the end of the program execution.

M2

M3​

For 2D plotters, used to raise and lower the plotter head. M3 S0 will raise the print and M3 S1000 will lower the plotter head.

; Raise plotter head
M3 S0
; Move to first coordinate
G1 X0.000 Y0.000 F10000
; Lower the plotter head
M3 S1000
; Draw two lines
G1 X50.000 Y0.000 F10000
G1 X50.000 Y-50.000 F10000
; Raise the plotter head on a 2D plotter, A `G1` command with `Z` height would be used here for a 3D printer.
M3 S0