G Code
G Code
Note: Sections in RED are preliminary, untested, under development, or future features.
All words can be entered as upper or lower case. X9.999 or x9.999, either is acceptable to DeskCNC gcode interpreter. Decimals shown are optional, but are recommended to insure numeric intent.
Axis words:
2D-3D milling and routing
Linear Axes units in inches ( or mm with 9.999)
X9.9999
Y9.9999
Z9.9999 Spindle
4th axis rotary
A9.999 Rotary axis units in degrees
4-axis foam cutting
Linear Axes units in inches (or mm with 9.999)
X9.9999
Y9.9999
U9.9999
V9.9999
Lathe
Linear Axes units in inches (or mm with 9.999)
X9.9999 Radius or diameter
Z9.9999 Longitudinal
Other words:
Line Numbers - N99999 unique names for lines of the program. Line numbers are optional on each line
(integer)
Spindle Speed - S9999 in revolution per minute (integer)
Feed Rate - F99.99 in inches per minute or (mm/min) Modal
Pitch - P99.9 in threads per inch or threads per mm
I Incremental arc center position with respect to start position in X I9.999
J Incremental arc center position with respect to start position in Y J9.999
K Incremental arc center position with respect to start position in Z K9.999
Radius of arcs - R used in G02 and G03 R9.999
Tool Height - H, used with tool length offset (G43) H01 (integer)
Dwell time - P in seconds is used with G04 and canned cycles G81-G88. Also used for index in G10
command settings.
Program Name O%1 Specifies the name of a subprogram in a G65 subroutine call. Integer value in the
range 01-99
Peck depth - Q is used in Drilling cycles Q.25
Tool selection - T to select a tool from the tool table T01 (integer), up to 50 tools may be used.
G - codes
G00 rapid positioning
G00 X9.999 Y9.999 Z9.999 A9.999
Positioning at maximum steps per unit, axes are not synchronized to reach destination at the same time.
G00 or G0 may be used. Modal, the G00 may be left off lines with ensuing G00 format.
G01 linear interpolation
G01 X9.999 Y9.999 Z9.999 A9.999 F10.5
Coordinated linear positioning at programmed feed rate, axes are synchronized to reach destination at the
same time. G01 or G1 may be used. Modal, after first usage, the G01 may be left off immediately
following lines with G01 format. An initial feed rate must be programmed, with an MDI command, or with
a feed rate as shown above.
Example showing modality for F and G01:
G01 X9.999 Y9.999 Z9.999 A9.999 F10.5
X8.999 Y9.999 Z9.999 A360.0
X7.999 Y7.999 Z6.999 A10.0
X6.999 Y3.999 Z3.999 A152.37
Where the axis values define the acceptable tolerance for table position error. The example above will
home all 3 axes simultaneously to the Home Switches. G27 is only available when doing a Hard Home to
physical switches. If an axis is not called out in the G27 command, then that axis is ignored during
execution. For example, G27 X0.001 Z0.005 will only home/verify the XZ axes. The axis value defines
the acceptable tolerance. In the example, the X-axis tolerance is set to 0.001. If the table position is off by
more than 0.001 then the G27 command will put the machine in E-Stop.
G28 Home Return (Soft Home)
G28 is used to move to a known location. The G28 position is set in the Work Coordinate Offset Table
under menu Setup - Work Coordinate Offsets. G28 will simply move to this location.
G30 Home
G30 is used to Home to physical Home Switches. G30 will home to a switch, set the Home position, and
then move to an Offset Location. If an axis is not called out in the G30 command, then that axis is ignored
during execution.
For example: G30 X.5 Z.25 F20
Homes the X and Z axes simultaneously at a feed rate of 20. The position of the XZ coordinates will be set
from values entered in menu Setup - Machine Setup - Axes Setup - Home Position. The axis values set the
Home Offset. After the machine finds the Home Switches, the XZ axis will move to position 0.5 and 0.25
respectively.
Using the Homing Script:
Any valid G-Code Block sequence can be executed when a Home Button is pressed. The Homing
sequence is defined for each axis separately as well as for the 'All' button. Homing Scripts are defined in
menu Setup - Machine Setup - Home. One example of Homing using the All button follows...
M5
G92.2 G54
G30 X.5 F100
G30 X.5 F1
G30 Y.5 F100
G30 Y.5 F1
G30 Z.5 F100
G30 Z.5 F1
Program G38.2 X- Y- Z- A- to perform a straight probe operation. The rotational axis words are allowed,
but it is better to omit them. If rotational axis words are used, the numbers must be the same as the current
position numbers so that the rotational axes do not move. The linear axis words are optional, except that at
least one of them must be used. The tool in the spindle must be a probe.
It is an error if:
the current point is less than 0.254 millimeter or 0.01 inch from the programmed point.
G38.2 is used in inverse time feed rate mode,
any rotational axis is commanded to move,
no X, Y, or Z-axis word is used.
In response to this command, the machine moves the controlled point (which should be at the end of the
probe tip) in a straight line at the current feed rate toward the programmed point. If the probe trips, the
probe is retracted slightly from the trip point at the end of command execution. If the probe does not trip
even after overshooting the programmed point slightly, an error is signaled.
After successful probing, parameters 5061 to 5066 will be set to the coordinates of the location of the
controlled point at the time the probe tripped.
Using the straight probe command, if the probe shank is kept nominally parallel to the Z-axis (i.e., any
rotational axes are at zero) and the tool length offset for the probe is used, so that the controlled point is at
the end of the tip of the probe:
without additional knowledge about the probe, the parallelism of a face of a part to the XY-plane may, for
example, be found.
if the probe tip radius is known approximately, the parallelism of a face of a part to the YZ or XZ-plane
may, for example, be found.
if the shank of the probe is known to be well-aligned with the Z-axis and the probe tip radius is known
approximately, the center of a circular hole, may, for example, be found.
if the shank of the probe is known to be well-aligned with the Z-axis and the probe tip radius is known
precisely, more uses may be made of the straight probe command, such as finding the diameter of a circular
hole.
If the straightness of the probe shank cannot be adjusted to high accuracy, it is desirable to know the
effective radii of the probe tip in at least the +X, -X, +Y, and -Y directions. These quantities can be stored
in parameters either by being included in the parameter file or by being set in an RS274/NGC program.
As a usable example, the code for finding the center and diameter of a circular hole is shown in Table 6.
For this code to yield accurate results, the probe shank must be well-aligned with the Z-axis, the cross
section of the probe tip at its widest point must be very circular, and the probe tip radius (i.e., the radius of
the circular cross section) must be known precisely. If the probe tip radius is known only approximately
(but the other conditions hold), the location of the hole center will still be accurate, but the hole diameter
will not.
In the following example, an entry of the form <description of number> is meant to be replaced by an
actual number that matches the description of number. After this section of code has executed, the X-value
of the center will be in parameter 1041, the Y-value of the center in parameter 1022, and the diameter in
parameter 1034. In addition, the diameter parallel to the X-axis will be in parameter 1024, the diameter
parallel to the Y-axis in parameter 1014, and the difference (an indicator of circularity) in parameter 1035.
The probe tip will be in the hole at the XY center of the hole.
The example does not include a tool change to put a probe in the spindle. Add the tool change code at the
beginning, if needed.
N010 (probe to find center and diameter of circular hole)
N020 (This program will not run as given here. You have to)
G43 H01
To use a tool length offset, program G43 H-, where the H number is the desired index in the tool table. It is
expected that all entries in this table will be positive. The H number should be, but does not have to be, the
same as the slot number of the tool currently in the spindle. It is OK for the H number to be zero; an offset
value of zero will be used.
G65 Subroutine
Formatting for G65 is as follows:
N100 (Subroutine sample)
N110 G65 P1
N120 M02
N130 O%1
N140 (Subroutine Body)
N150 M99
Explanation:
N110 G65 P1 (The next line to be executed is the first line of Subroutine O%1)
-Additional program instructions may be placed between N110 & N120
N120 M02 (End Program)
Subroutine names currently must be Integers only, between 1-99 of the form...
N130 O%1 (Subroutine 1, Use the letter O "OH", not zero for the word address)
-place the subroutine instruction lines between N130 and N150, ie G01 X1.5 Y1.25, etc
N150 M99 (Return to the line following the G65 that called this subroutine.
All canned cycles are performed with respect to the currently selected plane. Any of the three planes (XY,
YZ, ZX) may be selected. Throughout this section, most of the descriptions assume the XY-plane has been
selected. The behavior is always analogous if the YZ or XZ-plane is selected.
Rotational axis words are allowed in canned cycles, but it is better to omit them. If rotational axis words are
used, the numbers must be the same as the current position numbers so that the rotational axes do not move.
All canned cycles use X, Y, R, and Z numbers in the NC code. These numbers are used to determine X, Y,
R, and Z positions. The R (usually meaning retract) position is along the axis perpendicular to the currently
selected plane (Z-axis for XY-plane, X-axis for YZ-plane, Y-axis for XZ-plane). Some canned cycles use
additional arguments.
For canned cycles, we will call a number "sticky" if, when the same cycle is used on several lines of code
in a row, the number must be used the first time, but is optional on the rest of the lines. Sticky numbers
keep their value on the rest of the lines if they are not explicitly programmed to be different. The R number
is always sticky.
In incremental distance mode: when the XY-plane is selected, X, Y, and R numbers are treated as
increments to the current position and Z as an increment from the Z-axis position before the move
involving Z takes place; when the YZ or XZ-plane is selected, treatment of the axis words is analogous. In
absolute distance mode, the X, Y, R, and Z numbers are absolute positions in the current coordinate system.
The L number is optional and represents the number of repeats. L=0 is not allowed. If the repeat feature is
used, it is normally used in incremental distance mode, so that the same sequence of motions is repeated in
several equally spaced places along a straight line. In absolute distance mode, L > 1 means "do the same
cycle in the same place several times," Omitting the L word is equivalent to specifying L=1. The L number
is not sticky.
When L>1 in incremental mode with the XY-plane selected, the X and Y positions are determined by
adding the given X and Y numbers either to the current X and Y positions (on the first go-around) or to the
X and Y positions at the end of the previous go-around (on the repetitions). The R and Z positions do not
change during the repeats.
The height of the retract move at the end of each repeat (called "clear Z" in the descriptions below) is
determined by the setting of the retract mode: either to the original Z position (if that is above the R
position and the retract mode is G98, OLD_Z), or otherwise to the R position.
It is an error if:
X, Y, and Z words are all missing during a canned cycle,
a P number is required and a negative P number is used,
an L number is used that does not evaluate to a positive integer,
rotational axis motion is used during a canned cycle,
inverse time feed rate is active during a canned cycle,
cutter radius compensation is active during a canned cycle.
When the XY plane is active, the Z number is sticky, and it is an error if:
the Z number is missing and the same canned cycle was not already active,
the R number is less than the Z number.
When the XZ plane is active, the Y number is sticky, and it is an error if:
the Y number is missing and the same canned cycle was not already active,
the R number is less than the Y number.
When the YZ plane is active, the X number is sticky, and it is an error if:
the X number is missing and the same canned cycle was not already active,
the R number is less than the X number.
Preliminary and In-Between Motion
At the very beginning of the execution of any of the canned cycles, with the XY-plane selected, if the
current Z position is below the R position, the Z-axis is traversed to the R position. This happens only once,
regardless of the value of L.
In addition, at the beginning of the first cycle and each repeat, the following one or two moves are made:
1. a straight traverse parallel to the XY-plane to the given XY-position,
2. a straight traverse of the Z-axis only to the R position, if it is not already at the R position.
If the XZ or YZ plane is active, the preliminary and in-between motions are analogous.
The G81 cycle is intended for drilling. Program G81 X- Y- Z- A- R- L0. Preliminary motion, as described above.
1. Move the Z-axis only at the current feed rate to the Z position.
2. Retract the Z-axis at traverse rate to clear Z.
G82 chip-breaking drilling canned cycle
The G82 cycle is intended for drilling with dwell. Program G82 X- Y- Z- A- B- C- R- L- P0. Preliminary motion, as described above.
1. Move the Z-axis only at the current feed rate to the Z position.
2. Dwell for the P number of seconds.
3. Retract the Z-axis at traverse rate to clear Z.
G83 chip-breaking drilling canned cycle
The G83 cycle (often called peck drilling) is intended for deep drilling or milling with chip breaking. The
retractions in this cycle clear the hole of chips and cut off any long stringers (which are common when
drilling in aluminum). This cycle takes a Q number, which represents a "delta" increment along the Z-axis.
Program G83 X- Y- Z- A- R- L- Q0. Preliminary motion, as described above.
1. Move the Z-axis only at the current feed rate downward by delta or to the Z position, whichever is less
deep.
2. Rapid back out to the clear_z.
3. Rapid back down to the current hole bottom, backed off a bit.
4. Repeat steps 1, 2, and 3 until the Z position is reached at step 1.
5. Retract the Z-axis at traverse rate to clear Z.
It is an error if:
the Q number is negative or zero.
G84 right hand tapping cycle
The G84 cycle is intended for right-hand tapping with a tap tool.
Program G84 X- Y- Z- A- R- L0. Preliminary motion, as described above.
1. Start speed-feed synchronization.
2. Move the Z-axis only at the current feed rate to the Z position.
3. Stop the spindle.
4. Start the spindle counterclockwise.
5. Retract the Z-axis at the current feed rate to clear Z.
6. If speed-feed synch was not on before the cycle started, stop it.
7. Stop the spindle.
8. Start the spindle clockwise.
The spindle must be turning clockwise before this cycle is used. It is an error if:
the spindle is not turning clockwise before this cycle is executed.
With this cycle, the programmer must be sure to program the speed and feed in the correct proportion to
match the pitch of threads being made. The relationship is that the spindle speed equals the feed rate times
the pitch (in threads per length unit). For example, if the pitch is 2 threads per millimeter, the active length
units are millimeters, and the feed rate has been set with the command F150, then the speed should be set
with the command S300, since 150 x 2 = 300.
If the feed and speed override switches are enabled and not set at 100%, the one set at the lower setting will
take effect. The speed and feed rates will still be synchronized.
9. Move at traverse rate parallel to the XY-plane to the point indicated by I and J.
10. Move the Z-axis only at traverse rate to the clear Z.
11. Move at traverse rate parallel to the XY-plane to the specified X,Y location.
12. Restart the spindle in the direction it was going before.
When programming this cycle, the I and J numbers must be chosen so that when the tool is stopped in an
oriented position, it will fit through the hole. Because different cutters are made differently, it may take
some analysis and/or experimentation to determine appropriate values for I and J.
G88 boring, spindle stop, manual out canned cycle
The G88 cycle is intended for boring. This cycle uses a P word, where P specifies the number of seconds to
dwell. Program G88 X- Y- Z- A- R- L- P0. Preliminary motion, as described above.
1. Move the Z-axis only at the current feed rate to the Z position.
2. Dwell for the P number of seconds.
3. Stop the spindle turning.
4. Stop the program so the operator can retract the spindle manually.
5. Restart the spindle in the direction it was going.
G89 boring, feed-in, dwell, feed-out cycle
The G89 cycle is intended for boring. This cycle uses a P number, where P specifies the number of seconds
to dwell. program G89 X- Y- Z- A- B- C- R- L- P0. Preliminary motion, as described above.
1. Move the Z-axis only at the current feed rate to the Z position.
2. Dwell for the P number of seconds.
3. Retract the Z-axis at the current feed rate to clear Z.
G90 absolute distance mode
Modal. All following code is programmed in machine coordinates. Each command is relative to the
absolute position of the machine coordinates.
G91 incremental distance mode
Modal. All following code is programmed in relative motion. Each command is relative to the last end
point.
G92 offset coordinate systems (add XYZA values)
Offset the current coordinate system using G92 or G92.3. This offset will then apply to all program
coordinate systems. This offset may be cancelled with G92.1 or G92.2.
To make the current point have the coordinates you want (without motion), program G92 X- Y- Z- A- ,
where the axis words contain the axis numbers you want. All axis words are optional, except that at least
one must be used. If an axis word is not used for a given axis, the coordinate on that axis of the current
point is not changed. It is an error if: all axis words are omitted.
When G92 is executed, the origin of the currently active coordinate system moves. To do this, origin
offsets are calculated so that the coordinates of the current point with respect to the moved origin are as
specified on the line containing the G92. In addition, parameters 5211 to 5216 are set to the X, Y, Z, and Aaxis offsets. The offset for an axis is the amount the origin must be moved so that the coordinate of the
controlled point on the axis has the specified value.
Here is an example. Suppose the current point is at X=4 in the currently specified coordinate system and
the current X-axis offset is zero, then G92 x7 sets the X-axis offset to -3, sets parameter 5211 to -3, and
causes the X-coordinate of the current point to be 7.
The axis offsets are always used when motion is specified in absolute distance mode using any of the nine
coordinate systems (those designated by G54 - G59.3). Thus all nine coordinate systems are affected by
G92.
Being in incremental distance mode has no effect on the action of G92.
Non-zero offsets may be already be in effect when the G92 is called. If this is the case, the new value of
each offset is A+B, where A is what the offset would be if the old offset were zero, and B is the old offset.
For example, after the previous example, the X-value of the current point is 7. If G92 x9 is then
programmed, the new X-axis offset is -5, which is calculated by [[7-9] + -3].
To reset axis offsets to zero, program G92.1 or G92.2. G92.1 sets parameters 5211 to 5216 to zero, whereas
G92.2 leaves their current values alone.
To set the axis-offset values to the values given in parameters 5211 to 5216, program G92.3.
You can set axis offsets in one program and use the same offsets in another program. Program G92 in the
first program. This will set parameters 5211 to 5216. Do not use G92.1 in the remainder of the first
program. The parameter values will be saved when the first program exits and restored when the second
one starts up. Use G92.3 near the beginning of the second program. That will restore the offsets saved in
the first program. If other programs are to run between the the program that sets the offsets and the one that
restores them, make a copy of the parameter file written by the first program and use it as the parameter file
for the second program.
Miscellaneous M codes
/ Block Skip ( no option button presently available )
Line of code to the right of this character is ignored.
M00 program stop
Program execution stops and machine waits for operator to press continue or start button. Program is not
rewound.
M01 optional program stop ( no option button presently available )
Same as M00, except that the stop only occurs if option stop is turned on.
M02 program end
Stops and ends program execution. Program is reqound to beginning.
M03 Spindle on Clockwise
Turns on the Spindle relay corresponding to clockwise rotation (CW).
M04 Spindle on Counter Clockwise
Turns on the Spindle relay corresponding to counter-clockwise rotation (CCW).
M05 Spindle Off
M92 Aux2 0N
M93 Aux2 OFF
M94 Aux3 0N
M95 Aux3 OFF
M96 Dynamic Tool Length offset, with preposition in X-Y
M96 moves to the Tool Sensor Location defined in menu Setup - Machine Setup - Digitizing Probe. Then
'down' to the tool sensor. M96 is used when the tool sensor is semi-permanently mounted to a fixed
location on the machines table. See discussion of tool setting under M97.
M97 Dynamic Tool Length Offset, Without preposition in X-Y
M97 does not move to location but rather moves directly 'down' to the tool sensor. M97 is used when the
tool sensor is manually moved/placed under the tool for measurement.
TLO's are measured using the M96 or M97 codes. M96 moves to the Tool Sensor Location defined in
menu Setup - Machine Setup - Digitizing Probe. M97 does not move to location but rather moves directly
'down' to the tool sensor. M97 is used when the tool sensor is manually moved/placed under the tool for
measurement. M96 is used when the tool sensor is semi-permanently mounted to a fixed location on the
machines table.
Measuring TLO's:
1. If using M96, set the tool sensor location in menu Setup - Machine Setup - Tool Sensor - X/Y Location
along with the Tool Sensor Height. The Sensor Height is relative for this method of calculating TLO's so it
does not have to be entered exactly. Make certain that the Default Lim Polarity is set to Normally Closed
when using the Digitizing Probe or Tool Sensor.
2. Tool Changes are executed using a Tx M6 combination. A tool needs to be loaded for the M96/M97
commands to function. Enter T1M6 in the MDI box and press Enter. The Tool 1 'Tool Change Script' will
be executed (Tool Change Scripting). Tool 1 should now be in the Spindle.
3. Place the Tool Sensor under the tool and Enter M97 in the MDI box. TLO's are measured using the
current feedrate. Enter a new feedrate (F) if warranted.
4. The Spindle will lower the Tool to the Sensor and record the TLO. The TLO will be active. The TLO
for Tool 1 will NOT be saved in the Tool Library until you save the Tool Library from menu Setup - Tool
Library - Save. You can measure all tools and then save the entire table.
5. Once a TLO has been measured, it will need to be active when machining. This is done with the G43
Hx command where x is the tool number. The G43 Hx may be placed in the Tool Change Script.
6. When the Spindle is Zeroed, the Active TLO is used.
Using TLO's when machining:
1. Place the first tool used in your GCode file in the Spindle. Do this be entering T1M6 (assumes Tool 1)
in the MDI box. The tool change script for Tool 1 will be executed. The Tool Change Script should
include the G43 H1 command to make the TLO for tool 1 active. The TLO readout in DeskCNC will
display the current TLO.
2. Zero the tool to the part material. The Z Coordinate will reflect the active TLO.
3. Run the GCode file. All subsequent tool calls will place the tip of each tool at the proper Z Height
according to their TLO.
M99 Return from subroutine.
This command is the last line of a subroutine segment. Returns program execution to the next sequential
line after the calling line in the main program that called this subroutine.