——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

CNC Knowledge Center

CNC Programming: G-Code, M-Code, FANUC, Siemens & HEIDENHAIN

CNC programming is the language used to control computer numerical control machines. A CNC program tells the machine how to move the axes, control the spindle, select tools, apply offsets, activate machining cycles, and perform other machine functions.

The three CNC control systems covered in this guide are FANUC, Siemens SINUMERIK, and HEIDENHAIN. Although many basic concepts are similar, programming syntax and machine functions can differ between controls and machine manufacturers.

This guide introduces the most important concepts and commonly used codes for beginners and experienced CNC users.

Important: Always check the programming manual for the exact CNC control and machine before running a program. Machine-specific M-codes, options, offsets, tool-management functions, cycles, and safety functions can vary.


1. The Basic Structure of a CNC Program

A CNC program is normally made up of blocks, sometimes called program lines.

A simple block might look like:

N10 G01 X50 Y20 F200

Where:

  • N10 = block/sequence number
  • G01 = linear interpolation
  • X50 = X-axis position
  • Y20 = Y-axis position
  • F200 = feed rate

Another example:

N20 S2000 M03
  • S2000 = spindle speed
  • M03 = spindle clockwise

The exact syntax depends on the control.


2. G-Code

G-codes generally control geometry, movement, coordinate systems, machining modes, and cycles.

The following are some of the most important codes used in CNC milling and machining.

G-CodeFunctionExample
G00Rapid positioningG00 X50 Y20
G01Linear interpolationG01 X100 F200
G02Clockwise circular interpolationG02 X50 Y50 I25 J0
G03Counterclockwise circular interpolationG03 X50 Y50 I25 J0
G17Select XY planeG17
G18Select XZ planeG18
G19Select YZ planeG19
G20Inch programmingG20
G21Metric programmingG21
G40Cancel cutter compensationG40
G41Cutter compensation leftG41
G42Cutter compensation rightG42
G43Tool length compensationG43 H01 Z50
G49Cancel tool length compensationG49
G54Work coordinate systemG54
G55Work coordinate systemG55
G56Work coordinate systemG56
G57Work coordinate systemG57
G58Work coordinate systemG58
G59Work coordinate systemG59
G80Cancel canned cycleG80
G81Drilling cycleG81 Z-10 R2 F100
G82Drilling/counterboring cycleG82 Z-10 R2 P500 F100
G83Peck drilling cycleG83 Z-30 R2 Q5 F100
G84Tapping cycleG84 Z-15 R2 F...
G90Absolute programmingG90
G91Incremental programmingG91
G94Feed per minuteG94
G95Feed per revolutionG95

Note: The exact syntax and available cycles can differ between controllers and machine configurations.


3. M-Code

M-codes generally control machine functions such as the spindle, coolant, tool changes, program stops, and program end.

Common examples include:

M-CodeFunctionExample
M00Program stopM00
M01Optional stopM01
M02Program endM02
M03Spindle clockwiseM03 S2000
M04Spindle counterclockwiseM04 S2000
M05Spindle stopM05
M06Tool changeT01 M06
M08Coolant ONM08
M09Coolant OFFM09
M30Program end and resetM30

Important

M-codes are especially machine-dependent.

For example, an M-code used for a particular clamp, chuck, probing function, pallet changer, or auxiliary device may be defined by the machine builder rather than by FANUC, Siemens, or HEIDENHAIN itself.

Never assume that an M-code has the same function on every CNC machine.


4. Coordinates and Programming Modes

Understanding coordinate systems is essential for CNC programming.

Absolute Programming – G90

With absolute programming, positions are normally specified relative to the active work coordinate system.

Example:

G90G01 X100 Y50

The machine moves to the programmed position.

Incremental Programming – G91

With incremental programming, the programmed movement is relative to the current position.

Example:

G91G01 X20 Y10

The machine moves 20 units in X and 10 units in Y from its current position.


5. Work Coordinate Systems

Work offsets allow the programmer to define the position of the workpiece relative to the machine coordinate system.

Common examples include:

G54G55G56G57G58G59

For example:

G54G00 X0 Y0

The machine uses the active G54 work coordinate system.

On real machines, the actual offset values are normally set in the control’s offset/work-coordinate tables.


6. Tool Length Compensation

On many milling controls, tool length compensation is commonly associated with G43.

Example:

G43 H01 Z50

Here:

  • G43 = tool length compensation
  • H01 = tool length offset number
  • Z50 = commanded Z position

Cancellation is commonly:

G49

The exact method can vary between control systems.


7. Cutter Compensation

Cutter compensation allows the control to compensate for the radius of the cutting tool.

Common codes are:

G40G41G42

G40

Cancel cutter compensation.

G41

Cutter compensation to the left.

G42

Cutter compensation to the right.

Example:

G41 D01

The D01 offset number is commonly associated with the cutter-radius compensation table on FANUC-type controls.

Because compensation behavior and syntax can vary, always verify the control manual and machine setup.


8. Circular Interpolation

Circular movements are commonly programmed with:

G02G03

G02 – Clockwise

G02 X50 Y50 I25 J0

G03 – Counterclockwise

G03 X50 Y50 I25 J0

The meaning of I, J and K depends on the selected plane and programming method.

For this reason, the plane selection should also be understood:

G17 = XYG18 = XZG19 = YZ

9. Drilling Cycles

Canned cycles simplify repetitive machining operations.

A basic drilling example is:

G81 Z-10 R2 F100

Typical meanings:

  • G81 = drilling cycle
  • Z-10 = drilling depth
  • R2 = reference/retract plane
  • F100 = feed rate

Other common cycles include:

G81 – DrillingG82 – Drilling with dwellG83 – Peck drillingG84 – TappingG80 – Cancel cycle

Cycle syntax varies considerably between controls, so these should not be treated as universal production code.


10. Spindle Speed and Feed Rate

Two of the most important CNC parameters are:

S – Spindle Speed

Example:

S2000

This commonly means a spindle speed of 2000 RPM.

F – Feed Rate

Example:

F200

Depending on the active feed mode, this may represent feed per minute or another unit.

Common feed modes include:

G94 – Feed per minuteG95 – Feed per revolution

11. Tool Selection

A typical FANUC-style milling program may use:

T01 M06

Meaning:

  • T01 = select tool 1
  • M06 = tool change

However, tool-management procedures vary between machine builders and controls.


12. A Simple FANUC-Style Milling Example

Here is a simple educational example:

%O1001G21 G90 G54T01 M06S2000 M03G43 H01 Z50M08G00 X0 Y0G01 Z-2 F100G01 X50 F200G01 Y50G01 X0G01 Y0G00 Z50M09M05G49G00 X0 Y0M30%

What happens?

  1. Metric programming is selected.
  2. Absolute programming is selected.
  3. G54 is selected.
  4. Tool 1 is loaded.
  5. Spindle starts clockwise.
  6. Tool length compensation is activated.
  7. Coolant starts.
  8. The tool moves to the work area.
  9. The tool cuts a simple square path.
  10. The tool retracts.
  11. Coolant and spindle stop.
  12. The program ends.

This is an educational example only. It must be adapted and verified for the specific machine before use.


13. FANUC CNC Programming

FANUC controls are widely used in industrial CNC machines.

FANUC programming commonly uses ISO-style G-code programming, while MANUAL GUIDE i provides conversational programming and graphical assistance. FANUC also offers CNC Guide, which can simulate FANUC CNC controls on a PC for programming and G-code testing.

Important FANUC topics to learn

  • Program structure
  • G00 / G01
  • G02 / G03
  • G17 / G18 / G19
  • G40 / G41 / G42
  • G43 / G49
  • G54–G59
  • G80–G89 cycles
  • G90 / G91
  • Tool offsets
  • Work offsets
  • Spindle speed
  • Feed rate
  • Tool changes
  • Macro programming
  • Subprograms
  • Canned cycles
  • Probing
  • Custom M-codes
  • Machine-specific functions

14. Siemens SINUMERIK Programming

Siemens SINUMERIK supports several programming approaches, including G-code programming and high-level programming, as well as ShopMill and ShopTurn-oriented programming.

A very important point is that Siemens can also work with ISO dialects, and Siemens documentation shows that many common G-codes have corresponding functions in Siemens and ISO modes.

Common Siemens/ISO concepts

G00 G01 G02 G03 G17 G18 G19 G40 G41 G42 G54 G55 G90 G91 G94 G95

But Siemens programming should not simply be treated as “FANUC with a Siemens name.” There are important differences in syntax, cycles, variables, transformations, tool management, and machine functions.

Siemens topics to learn

  • SINUMERIK program structure
  • ISO programming
  • Siemens native programming
  • G-code programming
  • ShopMill
  • ShopTurn
  • Work offsets
  • Tool management
  • Canned cycles
  • Subprograms
  • Variables
  • Coordinate transformations
  • 5-axis programming
  • High-level programming
  • Machine-specific M-functions

15. HEIDENHAIN CNC Programming

HEIDENHAIN is different from FANUC and Siemens because its controls are strongly associated with Klartext conversational programming.

HEIDENHAIN states that its controls can be programmed using Klartext and also support G-code/ISO programming.

A simple HEIDENHAIN-style example can look like:

BEGIN PGM DEMO MM
BLK FORM 0.1 Z X-50 Y-50 Z-20
BLK FORM 0.2 X+50 Y+50 Z+0
TOOL CALL 1 Z S2000
L X+0 Y+0 Z+50 FMAX M3
L Z+0 FMAX
L Z-2 F100
L X+50 F200
L Y+50
L X+0L
Y+0L
Z+50 FMAX M5
END PGM DEMO MM

This illustrates the basic idea of HEIDENHAIN’s conversational style:

  • TOOL CALL = tool call
  • L = linear movement
  • FMAX = rapid/traverse movement
  • F = feed rate
  • M3 = spindle clockwise
  • M5 = spindle stop

Exact syntax depends on the TNC control and configuration.

HEIDENHAIN also provides programming stations based on the same software used by its controls, allowing programs to be created and tested away from the machine.


16. FANUC vs Siemens vs HEIDENHAIN

FeatureFANUCSiemens SINUMERIKHEIDENHAIN
Common programmingISO/G-codeSiemens + ISOKlartext + ISO/G-code
MillingExcellentExcellentExcellent
TurningExcellentExcellentAvailable on suitable controls
Conversational programmingMANUAL GUIDE iShopMill / ShopTurnKlartext
G-codeYesYesYes
Advanced programmingMacro/Custom functionsHigh-level programmingAdvanced cycles/Klartext
5-axisAvailableStrongStrong
Simulation/trainingCNC GuideSiemens training/simulation toolsProgramming Station
Main strengthBroad industrial adoptionFlexibility & advanced technologyUser-friendly conversational programming

Siemens specifically describes SINUMERIK as supporting turning, milling, grinding and additive manufacturing, with functions including dynamic 5-axis machining. HEIDENHAIN’s current TNC controls also support advanced milling and, on suitable models, turning and grinding.


17. Subprograms

Subprograms allow programmers to reuse sections of code.

A simplified FANUC-style example:

M98 P1000

This can call a subprogram such as:

O1000G01 X50G01 Y50M99

The exact call format and behavior depend on the CNC control.


18. Variables and Macro Programming

Advanced CNC programming can use variables, calculations, conditional statements and loops.

For example, FANUC Macro B supports variables and programmable logic.

This allows programmers to create more flexible programs rather than writing every movement individually.

Typical advanced concepts include:

  • Variables
  • Arithmetic
  • IF statements
  • WHILE loops
  • Subprograms
  • Parameters
  • Custom cycles
  • Probing routines
  • Automated measurement

Siemens and HEIDENHAIN have their own approaches to advanced programming, so macro code should never be copied directly from one controller to another without verification.


19. CNC Programming Workflow

A professional CNC programming workflow normally involves:

1. Technical drawing

2. Select machine

3. Select cutting tools

4. Select workholding

5. Establish work coordinate system

6. Calculate cutting data

7. Create CNC program

8. Simulate/backplot

9. Verify tools, offsets and clearances

10. Transfer program to machine

11. Dry run / single block / controlled verification

12. Machine the part

13. Measure and inspect the finished part


20. CNC Simulation and Verification

Simulation is an important part of modern CNC programming.

It can help identify:

  • Programming errors
  • Incorrect toolpaths
  • Collisions
  • Incorrect tool movements
  • Wrong machining depths
  • Incorrect work offsets
  • Unexpected rapid movements

For FANUC programming, FANUC CNC Guide provides PC-based simulation of FANUC CNC controls, including G-code testing.

For HEIDENHAIN, the Programming Station uses the control software and provides graphical simulation; HEIDENHAIN also offers free demo versions for its programming stations.


21. The Most Important CNC Programming Topics

For someone learning CNC programming, I recommend studying these subjects in this order:

Beginner

  1. CNC coordinate systems
  2. Machine zero
  3. Work zero
  4. G90 / G91
  5. G00
  6. G01
  7. G02 / G03
  8. Feed rate
  9. Spindle speed
  10. Tool selection
  11. Tool offsets
  12. Work offsets

Intermediate

  1. G41 / G42
  2. G43 / G49
  3. G81–G89 cycles
  4. Subprograms
  5. Tool management
  6. Work coordinate systems
  7. Probing
  8. Program verification

Advanced

  1. Macro programming
  2. Variables
  3. Custom cycles
  4. Coordinate transformations
  5. 4-axis machining
  6. 5-axis machining
  7. CAD/CAM
  8. Postprocessors
  9. Digital twins
  10. Machine-specific programming

22. Safety First

CNC programs control powerful machines capable of causing serious injury and equipment damage.

Before running any program:

  • Verify the correct machine.
  • Verify the correct CNC control.
  • Verify the tool numbers.
  • Check tool length and radius offsets.
  • Check work offsets.
  • Check spindle speed and feed rate.
  • Check rapid movements.
  • Check Z-axis clearance.
  • Simulate the program where possible.
  • Use single-block and controlled verification when appropriate.
  • Keep clear of moving machinery.
  • Follow the machine manufacturer’s safety procedures.

Never run an unverified CNC program directly on a machine.


23. Official CNC Resources

For readers who want deeper technical information, Parazonn can link to the manufacturers’ official resources.

FANUC: FANUC provides CNC Guide for PC-based CNC simulation and MANUAL GUIDE i for conversational programming.

Siemens SINUMERIK: Siemens provides documentation and information covering SINUMERIK programming, including ISO programming and advanced CNC functions.

HEIDENHAIN: HEIDENHAIN provides TNC documentation, programming stations, manuals and its Klartext programming environment.


CNC Knowledge Center – Recommended Sections

På själva CNC Knowledge Center-sidan skulle jag inte lägga hela texten ovan som en enda jättelång sida. Jag skulle göra den till en huvudsida med länkar till undersidor:

⚙️ CNC Knowledge Center

1. CNC Programming Basics
Coordinate systems, G90/G91, G00/G01/G02/G03, feed and spindle speed.

2. G-Code Guide
G-code explained with short examples.

3. M-Code Guide
Machine functions and common M-codes.

4. FANUC Programming
FANUC G-code, M-code, offsets, cycles, macros and programming examples.

5. Siemens SINUMERIK Programming
Siemens programming, ISO programming, ShopMill, ShopTurn and advanced functions.

6. HEIDENHAIN Programming
Klartext, G-code, cycles, tool calls and programming examples.

7. CNC Cycles
Drilling, tapping, boring and other machining cycles.

8. CNC Macro Programming
Variables, calculations, loops and custom programming.

9. CNC Simulation
FANUC CNC Guide, HEIDENHAIN Programming Station and other simulation solutions.

10. CAD/CAM & Postprocessors
How CAD/CAM software generates CNC programs and why the correct postprocessor matters.

11. CNC Troubleshooting
Common programming and machining problems.

12. CNC Safety
Essential safety and program-verification practices.