Get to Know Your CNC: How to Read G-Code

CNC & Machining Computers & Mobile Digital Fabrication Technology
Get to Know Your CNC: How to Read G-Code
images
This tutorial is excerpted from Make: Getting Started with CNC, which provides a basic overview of how to use affordable, hobbyist-level computer-controlled routers. Available at Maker Shed and fine bookstores.

G-code is the generic name for a plain-text language that CNC machines can understand.

Using a modern-day desktop CNC machine and software, you’ll never have to enter G-code manually, unless you want to. The CAD/CAM software and the machine controller will take care of all of this for you. However, some people (especially makers!) like to know what’s under the hood and how things really work.

A G-code file is plain text; it’s not exactly human readable, but it’s pretty easy to look through the file and figure out what’s going on. G-codes tell the controller what sort of motion is desired. Here are the most common commands and how they work.

G0/G1 (Rapid/Controlled Motion)

gcodea

The G0 command moves the machine at maximum travel speed to whatever coordinates follow G0 (Figure A). The machine will move in a coordinated fashion, and both axes complete their travel at the same time. G0 is not used for cutting. Instead, it’s used to move the machine quickly to begin a job or move to another operation within the same job. Here’s an example of a rapid (G0) command:
G0 X7 Y18

gcodeb

A G1 command (Figure B) is similar but tells the machine to move at a specified rate called the feed rate (F):
G1 X7 Y18 F500

G2 (Clockwise Motion)

gcodec

Setting the mode to G2 and specifying the offset from center (Figures C and D) creates clockwise motion between the starting point and the specified ending points.
G21 G90 G17
G0 X0 Y12
G2 X12 Y0 I0 J-12

gcoded

The G2 starting point is where the machine is located prior to issuing the G2 command.
 It’s easiest if you move your machine to the starting point before trying to issue the G2 command.

G3 (Counterclockwise Motion)

gcodee

Just like G2, the G3 command creates an arc between two points. Whereas G2 specifies clockwise motion, G3 specifies counterclockwise motion between the points (Figure E). A valid set of commands to produce G3 motion is shown here:
G21 G90 G17
G0 X-5 Y25
G3 X-25 Y5 I0 J-20

G17/G18/G19 (Working Planes)

These modes set the plane to be machined. Typically G17 is used and is the default for most hobby machines, but two other planes can be used in a three-axis machine:
G17 = x/y plane
G18 = z/x plane
G19 = y/z plane

G20/21 (Inches or Millimeters)

The G21 and G20 commands determine the G-code units, either inches or millimeters:
G21 = millimeters
G20 = inches
Here’s an example that’s set to millimeters:
G21 G17 G90

G28 (Referencing Home)

A simple G28 command sends the machine to its home position. Adding coordinates will define an intermediate point to go to, before homing (to avoid collisions), like this:
G28 Z0
Some machines require a G28.1 command to define the home position coordinates:
G28.1 X0 Y0 Z0

G90 (Absolute Mode)

G90 causes units to be interpreted as absolute coordinates. This is the most common mode for hobby-grade CNC machines; it’s the “default” mode.
Absolute coordinates will be interpreted as exactly that — absolute. G0 X10 will send the machine to x = 10. It will not send the x-axis to “10 more” units from where it’s currently located.

G91 (Incremental Mode)

The opposite mode of G90. Setting incremental mode means that every command issued will move your machine the specified number of units from its current point.

For example, in incremental mode, G1 X1 will advance the machine 1 unit in the x direction, regardless of its current location.

G-Code Rules

Just like a math equation, G-code has its own rules about the order of operations. Here are the most common, in order of precedence (that is, comments will be interpreted first and the change tool will be interpreted last):

  • Comments
  • Feed rate
  • Spindle speed
  • Select tool
  • Change tool

When you issue a G command, you are putting the machine into that mode. If you issue a G1 command, such as G1 X5 Y13, then the machine moves to X5 Y13.

If you issue another set of coordinates, you do not need to issue another G1 command. Why? Because the machine is in G1 mode until you change it to something else like G0 or G2 or G3.

Feeds, Speeds, and Tools

Simple G-code commands are used for setting the speed, feed, and tool parameters.

“F” Is for “Feed”

The F command sets the feed rate; the machine operates at the set feed rate when G1 is used, and subsequent G1 commands will execute at the set F value.

If the feed rate (F) is not set once before the first G1 call, either an error will occur or the machine will operate at its “default” feed rate. An example of a valid F command:
G1 F1500 X100 Y100

“S” Is for “Spindle Speed”

The S command sets the spindle speed, typically in revolutions per minute (RPM). An example of a valid S command:
S10000

“T” Is for “Tool”

The T command is used in conjunction with M6 (M-codes are machine action codes) to specify the tool number to be used for cutting the current file:
M6 T1
On industrial machines, an M6 T command usually produces a tool change with an automatic tool changer. On hobby machines with no tool changer available, issuing a new M6 T command will generally cause the machine to issue itself a feed-hold command, wait for the operator to change the tool, and then continue the job after the “resume” button is pressed.

Discuss this article with the rest of the community on our Discord server!
Tagged

Edward Ford designed and released Project Shapeoko and helped cofound Carbide 3D, where he leads the Shapeoko product line and develops other great desktop manufacturing software and equipment.

View more articles by Edward Ford

ADVERTISEMENT

Maker Faire Bay Area 2023 - Mare Island, CA

Escape to an island of imagination + innovation as Maker Faire Bay Area returns for its 15th iteration!

Buy Tickets today! SAVE 15% and lock-in your preferred date(s).

FEEDBACK