Code Bits, Projects, & Tech Thoughts

A Quick Introduction to G-Code

A Quick Introduction to G-Code


I am not a G-Code expert. You'll find tonnes of other, likely more instructive, tutorials for G-Code online. Google is your friend here, I kid you not there are thousands of hours a few clicks away. Right then, with that out of the way let's launch right in.


G-Code is a Numerical Control (NC -- that's the NC in CNC) standard, used to send instructions to machines. It's a well defined set of commands that you can send to the machine to elicit certain behaviors. By this means, many CNC machines are instructed to move, cut, burn, extrude, draw, and otherwise interact with the world. Because computer machinery does not understand English1 G-Code provides a common language that programmers can use to write toolpath generators, with fair confidence that different CNC machines will interpret their instructions in a predictable way.

The G-Code Instruction

A G-Code is an instruction is one 'Letter Code' command to the CNC machine. A command is often coupled with parameters, describing certain aspects of the command. Let's use one of the more basic commands to illustrate this. The G01 command instructs a machine to move linearly to a point. The command takes parameters such as the X, Y, Z positions to which the machine should move, as well as the feedrate (basically the linear speed of an axis) at which the machine should move.

For Example, to move the machine to (X:1,Y:1,Z:0) at a speed of 1500mm/minute2 you would issue the command G01 X1.0 Y1.0 Z0.0 F1500. If you only want to move some of the axes, you may omit axes without movement. If you want to move at the feedrate currently set on the machine you may omit the F parameter. To add a comment, surround the comment with parentheses. Alternatively, we can "force a line break" in the command (basically, tell the interpreter that the command is over) by inserting a semicolon ; character.

Common G-Code

I've listed some commonly used codes below. There are tonnes more, and their behavior differs from machine to machine (across G-Code interpreter 'flavours,' so to speak). The RepRap G-Code standard is a good place to start, this is used across many 3D Printer firmwares.

Command Name
Rapid Move [X Y Z E(xtruder) F(eedrate)]
G1 Linear Move [X Y Z E(xtruder) F(eedrate)]
G4 Dwell [P(miliseconds) | S(econds)]
G90 Set to Absolute Positioning None
G91 Set to Relative Positioning None
G92 Set Position [X Y Z E(xtruder)]
M01 Sleep or Conditional stop None
M17 Enable/Power all steppers None
M18 Disable all stepper motors None
M114 Get Current Position None

'Hello World'

Let's apply this basic overview and write our first G-Code program. This will be a simple program, and all it will do is draw a 1cm x 1cm square with our tool, in this case a pencil.

Assume the pen starts at (0,0,1) which we'll call our home position. That's X0, Y0 and the pencil is raised off the paper 1mm. We'll want to move to where we want one corner of the pencil to be, say (1,1). Then we'll want to lower the pencil. We'll want to start moving the pencil, but since we're programmers we're lazy, and can't be bothered to do math. So we'll tell the machine to use relative positioning instead of absolute positioning. This means that to move from (1,1) up by one centimeter we only have to send "add 1 centimeter to Y" instead of calculating that the new position will be 10mm (new) + 1mm (current) = 11mm.

To make our square we'll have to move up one centimeter, then right one centimeter, then down one centimeter, then left one centimeter. Since we're now in relative positioning this is easy. Finally, we'll raise the pencil, and move the pencil back to home. Again we're lazy. Instead of calculating where we've gone and keeping track of where we are, we can tell the machine that we want to move to "absolute 0,0' by setting the machine back to absolute positioning.

All together this might look something like the following,

G90 ;Set Absolute  
G92 X0.0 Y0.0 Z1.0 ;Tell us we're at home  
M17 ;Enable Motors  
G01 X1.0 Y1.0 ;Move to start of square  
M01 Z0.0 ;Lower pencil  
G91 ;Set Relative  
G01 Y10.0 ;Move up 1cm  
G01 X10.0 ;Move right 1cm  
G01 Y-10.0 ;Move down 1cm  
G01 X-10.0 ;Move left 1 cm  
G90 ;Set Absolute  
G01 Z1.0 ;Raise pencil  
G01 X0.0 Y0.0 ;Move to Home  
M18 ;Disable Motors  

But what does it look like?

G-Code Render Preview A quick simulation of this G-Code being drawn with a pencil 'tool'.

What now?

Now you're loosely prepped to examine wild G-Code. Unless you have a hankering to write a G-Code interpreter this is more useful as a debugging tool. 3D Print not working quite the way you'd expect? Open the G-Code file and take a look at the instructions! Want to quickly move your laser engraver from the command interface? Now you know an easy way to do it.


  1. Or any other language, for that matter. Or shouting. Yes I have tried shouting at them, at best you get mild indifference.

  2. Yes, often G-Code is in metric because engineers are decent humans. The G20 command can be used to set units to inches, but I've yet to see this used