CNC programming with G-Code


CNC programming with G-code – What is that actually?

The G-code, also called DIN-code, is a machine language with the help of which the programmer tells the CNC machine what it should do.

CNC stands for computerized numerical control and means that the machine is controlled by a computer.

The G-code is called like this because many commands start with a G followed by a number (e.g. G56).

In fact, however, there are various commands with all possible letters from A-Z of the alphabet. The sum of the G-code commands results in the machine program.

G-Code program

G-Code program – An example

Since the G-code is largely standardized, it can be executed on almost any CNC machine.

As soon as the machine program is loaded into the machine, the job runs almost fully automatically in the ideal case.

How is the CNC program structured?

The CNC program is composed of several so-called blocks.

A block corresponds to exactly one line in the machine program. The block in turn consists of one or more commands. In the English-speaking world a block is also known as a “strong> block”/strong>.

The command usually consists of the address letter and one or more parameters.

Each record begins with a unique counter number, which is incremented in steps of ten (N010, N020, …).

This gives you enough space to add up to 9 command lines between two existing G-Code commands.

CNC program with G-Code – Example programs

1. example: linear movement with rapid traverse and standard feed rate

In the following example program, the command (N010) is first given so that the position x=50, y=50 and z=10 is approached in rapid traverse (G00).

In the following command line (N020) the position X=100 and y=100 is approached at normal speed (G01), the travel distance is linearly interpolated.

%Example CNC program in G code with two blocks:
N010 G00 X50 Y50 Z10
N020 G01 X100 Y100

2. example program: traversing a pocket contour

In this example program we want to mill the contour of a pocket. This should be 20 mm long and 10 mm wide.

The arcs are to be tangential to the straight lines. For the arcs we use the command G02 (clockwise arc).

To edit and visualize G-Code you can download the free G-Code Viewer and Editor NC Corrector.

  1. Moving to home position X=0, Y=0, Z=0
    N10 G01 X0 Y0 Z0
  2. plunge of the tool by 10 mm
    N20 G01 Z-10
  3. Clockwise arc movement to target coordinate X0 Y10; The G-code command G02 requires the specification of parameters I, J, and K: The relative position of the circle center from the current point is 0 mm away in the X direction (–> I0) and 5 mm away in the Y direction (–>J5)
    N30 G02 X0 Y10 I0 J5
  4. Linear approach to coordinate X10 Y10
    N40 G01 X10 Y10
  5. clockwise arc movement to target coordinate X10 Y0;
    The position of the circle center, seen from coordinate X10 Y10, is X+0 and Y-5
    N50 G02 X10 Y0 I0 J-5
  6. Linear approach to coordinate X0 Y0
    N60 G01 X0 Y0
  7. start up with the tool
    N70 G01 Z0
Pocket contour with G02

Pocket contour run off with G02

%Program Pocket contour with G02
N10 G01 X0 Y0 Z0
N20 G01 Z-10
N30 G02 X0 Y10 I0 J5
N40 G01 X10 Y10
N50 G02 X10 Y0 I0 J-5
N60 G01 X0 Y0
N70 G01 Z0
%End program Pocket contour

Frequently used G-code commands

Below you will find a list of the most frequently used commands and functions.

The commands for the CNC programmer range from moving the axes, tool path compensation and storing the tool radius to tool change:

G-commands (path functions)

  • G00: Move to a position in rapid traverse at the feed rate of the axes
  • G01: Move to a position at normal speed (linear interpolation)
  • G02: Feed with circular arc, clockwise (circle interpolation)
  • G03: Feed with circular arc, counterclockwise
  • G04: Dwell time
  • G05: Spline Definition
  • G06: Spline Interpolation
  • G09: Accurate stop
  • G14: Polar coordinate system, absolute
  • G15: Polar coordinate system, relative
  • G17: Select X-Y plane
  • G18: Select Z-X plane
  • G19: Select Y-Z plane
  • G20: Select freely definable plane
  • G28: Move to HOME position
  • G33: Thread cutting with constant pitch
  • G34: Thread cutting with increasing pitch
  • G35: Thread cutting with decreasing pitch
  • G40: Cancel all tool path corrections
  • G41: Tool path compensation in feed direction left
  • G42: Tool path compensation in feed direction right
  • G43: Tool compensation positive
  • G44: Tool compensation negative
  • G53: Delete the zero offset
  • G54-G59: Zero point shifts 1-6
  • G80: Cycles Loeschen
  • G81-G89: Drilling cycles
  • G90: Absolute coordinates in the current coordinate system
  • G91: Relative coordinates in the current coordinate system
  • G92: Reference point shift – Set memory
  • G94: Feed rate in mm/min
  • G95: Feed in mm/rev
  • G96: Constant cutting speed (Please set G92 spindle speed limit)
  • G97: Constant spindle speed in rpm

M-commands (switching functions):

M instructions, also called M functions (Miscellaneous Functions) are used to switch machine functions on and off.

  • M00: Program stop (spindle, coolant, feed off)
  • M01: Optional stop
  • M02: Program end
  • M03: Spindle On: Clockwise (Right rotation)
  • M04: Spindle On: Counterclockwise (counterclockwise rotation)
  • M05: spindle stop
  • M06: Perform tool change (tool must have been calibrated beforehand)
  • M07: Coolant 2 On
  • M08: Coolant 1 On
  • M09: Coolant Off
  • M10: Clamping on
  • M11: Clamping off
  • M13: spindle on, clockwise rotation and coolant on
  • M14: spindle on, counterclockwise rotation and coolant on
  • M19: Spindle stop at certain angular position
  • M30: Program end, Back to the program start
  • M31: Locking off
  • M40 – M45: Gear stage selection
  • M60: workpiece change
  • M68: Clamping the workpiece
  • M69: Clamp workpiece

Example program for M-commands:

%Program M command
N10 S1120 M14 $
N20 G95 F0.20 $

In the program shown above, the spindle speed is set to 1120 rpm, the spindle is switched on and the feed rate is set to 0.20 mm/rev

Overview of all address commands:

According to the DIN 66025 / ISO 6983 standard, the following address letters are available:

A Absolute or incremental position around the X axis (turning around X)
B Absolute or incremental position around the Y-axis (turning around Y)
C Absolute or incremental position around Z axis (turning around Z)
D tool correction factor
E second feed
F feedback
G travel commands (G0)
H tool length correction
I Defines the arc center along the x axis in commands G02 and G03
J Defines the arc center along the y axis in commands G02 and G03
K Defines the arc center along the z axis in commands G02 and G03
L Fixed Cycle Loop Count
M Additional functions (Miscellaneous) (e.g. coolant on/off, motor spindle on/off… )
N record number
O program name
P Additional parameter for some G and M commands
Q Additional parameter for some G commands
R parameter, e.g. radius for circular path (Rradius)
S spindle speed
T tool selection
U increment X
V increment Y
W increment Z
X absolute or incremental position in the direction of the X-axis
Y absolute or incremental position in the direction of the Y-axis
Z absolute or incremental position in the direction of the Z-axis
$ end of record

Unfortunately, I cannot go into all commands in detail at this point. Here I would like to refer to the manuals of the NC software providers, which document the individual commands in great detail and extensively. You can also find a very extensive list in the RepRap Wiki.


NC corrector

For easy editing and visualization of G-Code I find the G-Code Viewer and Editor NC Corrector great. NC Corrector is easy to get started with CNC programming and to learn the basic functions.

If you want to visualize in addition to the toolpaths also the machine and more complex things you can also have a look at PALmill, which you can download for free under the following link:

So that you don’t have to program everything by hand, it is obvious to use a ready-made software, which generates the G-code for you as far as possible independently from the 3D model:

Free CAM software for generating G-Code

To get from the 3D design to the finished product, you have to go through the following steps:

  1. CAD: Construction of a part in 3D CAD software and export of the 3D data
  2. CAM: Creating the G-code from the 3D data with the help of the CAM software
  3. CNC control: import of the G-code and running of the program.
Image with process flow for the G-Code

CAD CAM process flow

Not every CNC programmer has a professional machine at work or at home with a professional control system from Heidenhain, which does everything in no time at all.

For this reason I have put together a short list where you can download suitable CAD CAM programs for free:

CNC control software

To run the G-code in your machine, you need appropriate CNC control software for running the G-code.

Especially Mach 3 for the PC and GRBL for Arduino should be mentioned here.

Please leave me a short comment with your opinion about CNC controls.

Leave a Reply

Your email address will not be published. Required fields are marked *