0% found this document useful (0 votes)
10 views17 pages

G Codes for CNC Machines Explained

This document describes the most common G codes used in CNC machines. The codes G00, G01, G02, and G03 control the movements of the tool such as rapid movements, straight lines, and circles. Other codes like G17, G18, and G19 select the circular movement plane, while G20 and G21 specify whether metric or inch units will be used. Finally, codes like G40, G41, and G42 activate tool compensation.

Translated by

ScribdTranslations
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views17 pages

G Codes for CNC Machines Explained

This document describes the most common G codes used in CNC machines. The codes G00, G01, G02, and G03 control the movements of the tool such as rapid movements, straight lines, and circles. Other codes like G17, G18, and G19 select the circular movement plane, while G20 and G21 specify whether metric or inch units will be used. Finally, codes like G40, G41, and G42 activate tool compensation.

Translated by

ScribdTranslations
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

6.1.

G codes
These are the default standard codes used in our virtual machines.
normal. Please note that customized machines may have different codes and
formats.

The following tables will provide a brief explanation of the various G, M, and other codes.
recognized by the CNCSimulator interpreter. In the Format column, you will see the expected format
for the program. If a code is written in brackets like this [X #], it means that the code
it is not mandatory and can be omitted if not necessary. The # sign means that the CNCSimulator
wait for a number and the $ sign means that it is expecting a string of text in quotes.

Example:
G12[X#][Y#]Z#R#Q#S#SA#AS#RA#H#

This means that G12 code accepts the X and Y coordinates, but they are not mandatory and are
They can be omitted. On the other hand, you must specify the codes Z, R, Q, S, SA, AS, RA, and H to avoid
an alarm at runtime.

G-code Example Type of


Explanation Format
machine

G00 Go quickly (with the speed


maximum displacement) to the G00 [X#] All the
position X / Y / Z. This code is [Y#][Z#] G00 Z100 machines
use for the position and not for the
real machining.

G01 Travel in a straight line using the G01 [X#]


scheduled feed rate (F). [Y#][Z#] G01 X2.5 All the
[F#] Y4.1 F200 machines
This code is used for the
machining.
G02
XY-machines Circular / helicoidal interpolation in
clockwise. Causes a
circular movement in the direction of
the clock hands at the speed of
scheduled advance (F). The G02[X#]
movement can be two-dimensional [Y#][Z#] G02 X10
[I#][J#] Y10 I10 J0 All the
(plane) or three-dimensional (helical). The
[K#][R#] F200 machines
default plan of movement
Lathe circular is the XY plane (G17), but F#
other plans can also be used
(see G17-G19). The center of the arc or
circle is programmed using the letters
I, J and K (R can also be used).

G03 Exactly like G02, but the G03[X#] G03 X10 All the
XY machines circular motion goes in direction [Y#][Z#] Y10 I10 J0 machines
counterclockwise. [I#][J#] F200
[K#][R#]
Lathe [F#]

G04 P2000
G04 Live in milliseconds. This
(Two All the
will keep the axes motionless for G04 P#
seconds machines
the specified time period of delay
the number P.
It will force the machine to do a
full stop before continuing
G09 All the
G09 with the following movement. This is
a non-modal variant of G61 and, therefore,
machines
therefore, it does not have to be canceled.

Fixed cycle for circular drilling. Use it.


para taladrar agujeros alrededor del
G12 contour of a circle. R is the plane
Z is the total depth of G12[X#] G12 X0 Y0
drilling for each hole. Q is the [Y#]Z# Z-20 R2
incremental depth (peck). SA is R#Q#S#SA Q5 SA0 Alone
the starting angle of the circle (also # AS36 Milling machines
the angle of the first hole) and AS is the AS#RA#H# RA30 H10
angle between the holes. RA is the
circle radius and H specifies it
number of holes to be drilled.

G17
Select the XY plane for
G17 Alone
circular movements (see G02 and G17
Milling machines
G03).

Alone
Select the XZ plane for Milling machines
circular movements (see G02 and G18 G18
G03)

Select the YZ plane for G19 G19 Alone


circular movements (see G02 and Milling machines
G03)
G20 All the
G20 G20
Require the use of inches. machines

Require the use of units of G21 All the


G21 G21
millimeters. machines

Return home from the command. This


the command will first go to the position
scheduled X / Y / Z and then to Xmin
Milling and
Ymax Zmax of the axes of the G28[X#] turning
G28 machine. It can be a form [Y#][Z#] G28 Z10 machines
convenient to end a program
only
that places the machine's table in
a position to change the piece of
work.

G40 All the


machines
G40
Cancel the cutter compensation G40
More info
except
previously activated by G41 or G42. printer
3D

All the
G41
Activate the cutter compensation machines
G41[D#] G41
on the left side (or the compensation) except
[P#] More info
of the tip radius on a lathe. printer
3D

G42
All the
Activates right side cutter G42[D#] G42 machines
compensation (or nose radius [P#] More info except the
compensation in a lathe.
3D Printer

Activates tool length compensation. G43[H#] G43 H2 Z2 Alone


G43 (Optional, if not used, automatic tool) [P#][Z#] More info Milling machines
length compensation will be used).

Cancel tool length compensation G49 Alone


G49 G49
(activated by G43). Milling machines

Move in absolute non-compensated G53 [X#] G53 X0 Y0 All the


G53 [Y#][Z#] Z100
coordinates. machines

G54-G59 Fixture (work) offsets. A typical use of G54 G00 G54 All the
these G-codes are to establish a local X0 Y0 Z3 machines
coordinate system for each workpiece
when using multiple ones. You need to
setup the offsets in the Zero Points
Data table in the Inventory Browser
(F2).
G54 corresponds to offset registry
index 0, G55 to index number 1 etc...

Fixture (work) offsets. A typical use of


these G-codes is to establish a local
coordinate system for each workpiece
when using multiple ones. You need to G54.1 P# G54.1 P10
All the
G54.1 setup the offsets in the Zero Points (0-99) (Use work
machines
Data table in the Inventory Browser offset 10)
(F2).
G54.1 uses letter P to specify the
offset registry index.

G61 All the


G61 Exact stop mode. G61
machines

G64 Todas las


G64 Normal stop mode (cancels G61) G64
machines

G65 P#
Direct call of a macro. P is the macroA...
G65 P1005All the
G65 number to be called. For more B...
A180 machines
information, seeMacro programming. C...
etc.

Initiates modal calling of macro


G66 P#
defined by P. The macro will not be
A...
called in the G66 block but rather after All the
G66 B... G66 P7000
each tool move following the block. machines
C...
Cancel with G67. For more
etc.
information, seeMacro programming.

Cancels any model macro call initiated


All the
G67 by G66. For more information, G67
machines
seeMacro programming.

G68 Activates rotation of the coordinate G68 Milling


system. X(center machines
in X)
Two syntaxes are allowed. Y(center
in Y)
Use G69 to cancel the rotation. R(angle)

or

G68
A(center
in X)
B(center
in Y)
R(angle)

Cancel the rotation of the coordinate G69 Milling


G69
system. machines

Finishing Cycle.

After roughing, finishing can be


performed with this cycle. P is first
block of finishing contour and Q is the G70 [P#] G70 P100 Turning
G70 last block. [Q#] machines
Q250
only
For more information see:G71 Rough
Turning Cycle and G70 Finishing
Cycle.

Rough Turning Cycle

Two block format Roughing cycle. Turning


G71
G71 G71 machines
For more information see:G71 Rough only
Turning Cycle and G70 Finishing
Cycle.

G73
Peck drilling canned cycle. The cycle
is intended for deep drilling or chip
breaking milling operations. The cycle
retracts the tool to break chips. Code G73 [X#]
letter Q is used for peck size. R is [Y#][Z#] G73 Z-20 Milling
starting plane and Z is total depth. [R#][Q#] R1 Q1 machines
[P#] P100 only
Parameter P is used for dwell at each
peck. Please note that at the end of
the cycle, the return position in Z is
controlled by G98 and G99.

Generic drilling/boring/tapping canned


cycle. These are used in a generic
way to create compatibility with many
common CNC controllers on the G74 [X#] Milling and
market. They will bring the tool to the [Y#][Z#] G74 Z-20 turning
G74-G75
programmed Z depth. If R is [R#] R1 machines
programmed it will be used as the start only
plane, if not the current Z position will
be used as the start plane. All other
parameters will be ignored.

G76 [X#]
Generic drilling/boring/tapping canned [Y#][Z#] Alone
G76
cycle. See G74-G75 above. [R#] Milling machines

G76 Threading Cycle G76 G76 Turning


machines
For more information see:G76 Lathe
only
Threading Cycle

Milling and
Cancels any canned cycle. Please
G80 turning
G80 note that G00 – G03 also cancels G80
machines
canned cycles.
only

G81

Basic drilling canned cycle. R is


Milling and
starting plane and Z is total depth. G81 [X#]
Please note that at the end of the [Y#][Z#] G81 Z-6 turning
[R#] R2 machines
cycle, the return position in Z is
only
controlled by G98 and G99.

G82 [X#] Milling and


Generic drilling/boring/tapping canned [Y#][Z#] G82 Z-20 turning
G82-G89
cycle. Same as G74-G76 above. [R#] R1 machines
only

Absolute programming mode.


G90 G90 G00 All the
G90 Distances given will move the tool
X10 Y10 machines
relative to an absolute zero.

Incremental programming mode.


Distances given will move the tool G91 G91 G00 All the
G91
relative to the current position of the Z5 machines
tool.

Use to reposition the origin point (zero G92[X#] G92 X20 All the
G92 [Y#][Z#] Y20 Z10
point). machines

Milling and
G94 Set feed in millimeter or inch per G94 turning
G94
minute. machines
only

Milling and
G95 turning
G95 Set feed per revolution mode. G95
machines
only

G96[S#] Turning
G96 Constant surface speed control. G96 S300
machines
Cancel constant surface speed G97 Turning
G97 G97
control. machines

G98
Milling and
G98 G81 G98 turning
Initial level return at the end of a
Z-7 R2 machines
canned cycle.
only

G99
Milling and
R level return at the end of a canned G99 G81 G99 turning
cycle. Z-7 R2 machines
only
6.2. M codes
These are the default standard codes that are used in our normal virtual machines. Please note
that customized machines may have other codes and formats.

M-code
(codes valid from Explanation Format Example Machine type
V1.0.6.5)
Optional stop (pause). Will pause the
M00 execution of the CNC program if the
"[Link]" switch on the virtual CNC All the
Controller is activated. Click start or play to M00 M00
machines
continue.

M01 Unconditional stop (pause). The execution


of the CNC program will pause. Click start All the
or play to continue. M01 M01
machines

M02
M02 All the
Program end. Simulation will stop. M02
machines

M03
Milling and
Start of spindle clockwise. The rotation M03 M03
turning
speed is controlled by the S code letter. S2500
machines only

M04
Start of spindle counterclockwise. The Milling and
M04 M04
rotation speed is controlled by the S code turning
S2500
letter. machines only

M05
Milling and
Spindle stop. M05 M05 turning
machines only

M06
Execute a tool change. The number of the Milling and
tool has to be preselected with the T code M06 T17 M06 turning
letter. machines only

M08
Milling and
Turn on coolant. M08 M08 turning
machines only

M09
Milling and
Turn off coolant. M09 M09 turning
machines only

M17 Return from subroutine or subprogram. M17 M17 All the


More info machines

M20
All the
Open machine doors. M20 M20 machines with
doors

M21
All the
Close machine doors. M21 M21 machines with
doors

M30
M30 All the
Program end. Simulation will stop. M30
machines

M97 and M98


M98
Call sub program. Use P for sub program M98 P# All the
P1000 L1
number and L for the number of repetitions. L# machines
More info

M99
Return from a macro, subroutine or M99 All the
M99
subprogram. More info machines
6.3. $-commands and regions
These are the default standard codes that are used in our normal virtual machines. Please note
that customized machines may have other codes and formats.

The $-commands are special simulation commands for controlling the various aspects of the
simulation. They have nothing to do with CNC codes and should not be transferred to a real CNC
controller. $-commands can be put within parenthesis and will still be read by the simulator. Blocks
of $-commands can be hidden from the CNC code by encapsulating them in a region making the
block collapsible.

Do you notice the little - (minus) sign before the region command at the first line? If you click it, the
region will be collapsed hiding the block of $-commands.

A collapsed region will show three dots and a + (plus) sign. If you click the plus sign, the region will expand.
expand.

You can see what a collapsed region hides without expanding it by hovering the mouse pointer over it.

When you open a CNC program from the disk, it always loads with its regions collapsed.

In the following table we list and give a brief explanation of all built-in $-commands. Please note that
many of them are not for common use but rather made for special use in examples or when
creating portable programs. We have colored the commands you will likely use in red. If you are
not an advanced user that wants to learn all aspects of the CNCSimulator, you can skip over the
commands colored in black. Not even all red command is a must to know. As a minimum, you have
to know the $AddRegPart command as you will need it to place workpieces on the table (or in the
chuck). If you are a 3D Printer only user, you can ignore the $-commands.

-command Explanation Format


(codes valid from V1.0.6.5)

Puts a workpiece from the user


defined workpiece registry in the
Inventory Browser (F2) on the
machine table (or in the chuck).
The first number is the registry
$AddRegPart index for the workpiece you $AddRegPart # [#][#][#]
want to place. The three
following numbers are the X,Y
and Z coordinate for where you
want to place the workpiece. If
omitted, it will be placed at the
machine zero.
Puts a workpiece from the fixed
$AddEmbeddedRegPart embedded workpiece registry on $AddEmbeddedRegPart # [#][#][#]
the table. See $AddRegPart
above for further explanation.
Put a predefined workpiece on
$AddDefinedPart the table. Use $AddDefinedPart $AddDefinedPart # [#][#][#]
to define workpieces in the
program.
Will block on-screen spindle
rotation. This can be useful in
$BlockSpindleRot the unlimited lathe if visual
the rotation of the workpiece is not
desired.
Inches Enforce the use of Inch units.
(To set millimeters or inches Inches
units, open the program settings
dialog).
Millimeters Enforce the use of Millimeter
units. (To set millimeters or Millimeters
inches units, open the program
settings dialog).
SetView is a special command
to set the 3D view during
simulation. It should not be
programmed manually as it
contains too many parameters
but rather be created using the $SetView
$SetView
menu actionTools - Insert ################
Command - $SetView.

$Message Displays a message on the 3D $Message $#


view during simulation. First
parameter is a text string of the
message to display. Next
parameter is the number of
seconds to show the message.

Set the solid workpiece cutting


values at run-time in percent.
$Cutting Parameters are FromLeft, $Cutting #####
["From Right","From Front"]
FromBack and FromTop.
Set workpiece cutting.
To show the whole part, left
should be 0 and Right should be
Cutting 100.
The last parameter is 0 to show
the workpiece closed and 1 to
show the inside.
$Pause Will pause the simulation for a $Pause #
given number of milliseconds.
Starts the internal stopwatch to
measure the time for an
$StartStopWatch operation. Please note that the $StartStopWatch
time measured is simulation
time and not machining time.
$ShowStopWatch
Stops the internal stopwatch
and shows the time that has
elapsed since the $ShowStopWatch
$StartStopWatch command was
executed.

Enforces the use of a lathe


machine. You will see an alarm
Lathe if you run a program with this Lathe
command on a non-lathe
machine.
Enforces the use of a milling
machine. You will see an alarm
$Mill if you run a program with this $Mill
command on a non-milling
machine.
This command is used to
directly set up and use a milling
tool without using the user or
embedded registries.
$SetupTool Parameters are: TipAngle, $SetupTool ######
ShaftDiameter, ShaftLength,
Tool Diameter, Tool Length and
ToolTipType (0=Flat, 1=Ball,
2=Pointed)
$OverrideBufferQualitySe This special command is used $OverrideBufferQualitySetting
tting to override the resolution setting #
for the solid buffer. The value
given is the number of 3D pixels
(voxels) per millimeter. A value
of 1 will make each 3D pixel 1
cubic millimeter big. A value of
10 will make each 3D pixel 0.1
mm3 big. A value of 1 will create
ugly results but will run fast. On
the other hand, a value of 10 will
create high resolution
workpieces but will run slow. We
do not recommend using values
higher than 10 as you could
easily run out of memory due to
huge material buffer allocation.
Important: this command must
come before the $AddRegPart
command.
This command is used to
directly set up and use a milling
workpiece without using the
user or embedded registries.
$AddMillPart Parameters are: SizeX, SizeY, $AddMillPart #############
SizeZ, Xpos, Ypos, Zpos,
PartColR, PartColG, PartColB,
TopColR,TopColG,TopColB,
MaterialStyle
Define a lathe workpiece that
can be fetched by using the
command $AddDefinedPart.
Define Lathe Workpiece Parameters are Define Lathe Workpiece
NameAndMaterial, Index,
Length, Diameter, Inner
diameter.
Define a mill workpiece that can
be fetched by using the
$DefineMillWorkpiece command $AddDefinedPart. $DefineMillWorkpiece [$]####
Parameters are:
NameAndMaterial, Index, SizeX,
SizeY,SizeZ
Defines a material to be used by
defined workpieces. Parameters
$DefineMaterial are MatrlName, Index, $DefineMaterial [$]########
PartColR, PartColG, PartColG,
TopColR,TopColG,TopColB,
Material type
Defines a milling tool that can be
fetched by the DT code letters.
Parameters are:ToolName,
$DefineMillTool Index, TipAngle, ShaftDiam, $DefineMillTool [$]#######
ShaftLength,ToolDiam,
ToolLength,ToolTipType
(0=Flat, 1=Ball, 2=Pointed)

Turn on or off the display of the


$Toolpaths tool paths during simulation. $Toolpaths #
(1=on, 0=off).
$SetCuttingWidth Used in cutting machines to $SetCuttingWidth
define the cutting width.
Tells the simulator to override
the material for the workpiece
and use the specified one
$UseMaterial instead. The material must exist $UseMaterial
in the materials registry in the
Inventory Browser (F2) to avoid
an alarm.
Tells the simulator to override
the material for the workpiece
$UseEmbeddedMaterial and use the specified embedded $UseEmbeddedMaterial
one instead. The material must
exist in the embedded materials
registry to avoid an alarm.
$InvertLathePart

Flips the lathe workpiece in the


chuck to continue machining the $InvertLathePart
other side of it.

Circle and arc centers are


normally entered as incremental
$AbsoluteCenters values. This command tells the $AbsoluteCenters
simulator to expect absolute
circle and arc center values.

$SetG28pos Set custom position for G28. $SetG28pos{#}{#}{#}

$SetToolChangePos Set custom tool change position. $SetToolChangePos{#}{#}{#}

$DefineDoffsetDiameter Define a D offset in the program. $DefineDoffsetDiameter{#}{#}

When your program has defined


tools (for example in programs
created by SimCam), you call
these tools with 'DT'. Using this
$ReadTasDefinedTool command you can skip the D $ReadTasDefinedTool
and only use T followed by the
tool index number. For example
"T25". The simulator will treat all
T values as defined tools.
This command rotates the
$RotateWorkpiece workpiece around its Z axis. Rotate Workpiece

$Color This command sets the surface $Color R G B


color. Any new surface that gets
exposed by a milling operation
will have this color. The
command expects three
parameters: Amount of Red,
amount of Green and amount of
Blue. Values are giving in the
range 0 to 255 where 255 is the
brightest. $Color 0 0 0 will make
the surface black and $Color
255 255 255 will make it white.
For mixing of other colors,
Google 'RGB Color Picker'.
Is used to define temporary zero
points.
Example: $DefineZeropoint
N:Name 0 100 100 20
This will define zero point index
0 to X100 Y100 and Z20. Index
$DefineZeropoint 0 = G54, 1 = G55 etc. $DefineZeropoint[$][#][#][#] [#]
Index 0 can also be called with
G54.1 P0. After this command
has been used,
all G54-G59 and G54.1 calls will
refer to the defined zero point
and not to the
ones in the zero point registry.
Special command to set titles of
tutorials. You can read more
$TutorialLine about these hereCreating $TutorialLine[$]
customized tutorials in
CNCSimulator Pro.
Special command to set titles of
tutorials. You can read more
about these hereCreating
$Title $Title
customized tutorials in
CNCSimulator Pro.

By default, the simulator will


erase everything to the right
$PreventEraseOnCutOff when the material has been cut $PreventEraseOnCutOff
through to simulate the part
falling down. This command
prevents that from happening.
When your program wants to
use the embedded simulator
tools, you call these tools with
"ET". Using this command you
$ReadTasEmbeddedTool can skip the E and only use T $ReadTasEmbeddedTool
followed by the tool index
number. For example 'T25'. The
simulator will treat all T values
as embedded tools.
This activates an alternative
$UseAltG92method G92 mode so that it makes the $UseAltG92method
current tool position have the
coordinates you specify.
Used by SimCam and by "Make
$DefineLatheTool program portable" to create in-
lathe tools program.

Note that these commands work even when they are inside parentheses. It is normally good to
keep them in parentheses to avoid confusion with other software or CNC controllers.

6.4. Other codes


Code Explanation Format Example
Absolute or incremental X axis value used in canned X#
X X2.43
cycles and codes like G00-G03.
Absolute or incremental Y axis value used in canned Y#
Y Y1.16
cycles and codes like G00-G03.
Absolute or incremental Z axis value used in canned Z#
Z Z-3.2
cycles and codes like G00-G03.
Represents the center in X in G02 and G03 circle/ arc I# G02 X34 Z106.867
I
commands. I4.8 K0
Represents the center in Y in G02 and G03 circle/arc J#
J G02 Y0 J-20
commands.
Represents the center in Z in G02 and G03 circle/arc K# G02 X34 Z106.867
K
commands. I4.8 K0
Selects a tool from the user defined tool registry in the
T Inventory Browser (F2). M06 is used to execute the actual T# T1 M06
tool change.
Selects a tool from the fixed embedded tool registry. M06 ET#
ET ET9 M06
is used to execute the actual tool change.
Selects a predefined milling tool. The tool must first have
DT been defined using the $DefineMillTool command. M06 is DT# DT3 M06
used to execute the actual tool change.
S Defines spindle rotation in revolutions per minute (RPM). S# S2000
F Defines feed rate in millimeter or inches per minute. F# F240
M98 P1001 L1
Used in M98 to define a sub program number. Also used P#
P G73 Z-20 R2 Q5
as dwell time in canned cycles.
P100 F350
L Used in M98 to define the number of repetitions. L# M98 P1001 L1
(Drilling sub
Sub program number. Should be the first line of every sub O#
O program 1)
program.
O1000
Reference or starting plane in canned cycles. R is also
R used in G02/G03 to give the radius when I/J/K are not R# G81 Z-20 R2
used.
Q# G73 Z-20 R2 Q5 P0
Q Peck size in canned cycles.
F350

SA, AS, Start Angle, Angle Step and Radius used in G12 Circular SA# AS# G12 X0 Y0 Z-20 R2
RA# Q5 SA0 AS36 RA30
RA drilling cycle.
H10
G12 X0 Y0 Z-20 R2
Used for number of holes in the G12 Circular drilling
H Q5 SA0 AS36 RA30
cycle.
H10
Used for comments. G and M codes inside parenthesis
( and ) will not be executed. $-commands will be executed even (text) Drilling sub
program 1)
when inside the parenthesis.
G01 Z-20 F440
Used for commenting out one or several lines. G, M and $- /G03 I23.2 J0
/
commands will not be executed. /G03 X15 I19
G00 Z7
N Block number. N# N500

You might also like