G Codes for CNC Machines Explained
G Codes for CNC Machines Explained
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.
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.
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)
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
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...
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.
or
G68
A(center
in X)
B(center
in Y)
R(angle)
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.
G76 [X#]
Generic drilling/boring/tapping canned [Y#][Z#] Alone
G76
cycle. See G74-G75 above. [R#] Milling machines
Milling and
Cancels any canned cycle. Please
G80 turning
G80 note that G00 – G03 also cancels G80
machines
canned cycles.
only
G81
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.
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
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
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.
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.
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