——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
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.
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:
Another example:
N20 S2000 M03
The exact syntax depends on the control.
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-Code | Function | Example |
|---|---|---|
| G00 | Rapid positioning | G00 X50 Y20 |
| G01 | Linear interpolation | G01 X100 F200 |
| G02 | Clockwise circular interpolation | G02 X50 Y50 I25 J0 |
| G03 | Counterclockwise circular interpolation | G03 X50 Y50 I25 J0 |
| G17 | Select XY plane | G17 |
| G18 | Select XZ plane | G18 |
| G19 | Select YZ plane | G19 |
| G20 | Inch programming | G20 |
| G21 | Metric programming | G21 |
| G40 | Cancel cutter compensation | G40 |
| G41 | Cutter compensation left | G41 |
| G42 | Cutter compensation right | G42 |
| G43 | Tool length compensation | G43 H01 Z50 |
| G49 | Cancel tool length compensation | G49 |
| G54 | Work coordinate system | G54 |
| G55 | Work coordinate system | G55 |
| G56 | Work coordinate system | G56 |
| G57 | Work coordinate system | G57 |
| G58 | Work coordinate system | G58 |
| G59 | Work coordinate system | G59 |
| G80 | Cancel canned cycle | G80 |
| G81 | Drilling cycle | G81 Z-10 R2 F100 |
| G82 | Drilling/counterboring cycle | G82 Z-10 R2 P500 F100 |
| G83 | Peck drilling cycle | G83 Z-30 R2 Q5 F100 |
| G84 | Tapping cycle | G84 Z-15 R2 F... |
| G90 | Absolute programming | G90 |
| G91 | Incremental programming | G91 |
| G94 | Feed per minute | G94 |
| G95 | Feed per revolution | G95 |
Note: The exact syntax and available cycles can differ between controllers and machine configurations.
M-codes generally control machine functions such as the spindle, coolant, tool changes, program stops, and program end.
Common examples include:
| M-Code | Function | Example |
|---|---|---|
| M00 | Program stop | M00 |
| M01 | Optional stop | M01 |
| M02 | Program end | M02 |
| M03 | Spindle clockwise | M03 S2000 |
| M04 | Spindle counterclockwise | M04 S2000 |
| M05 | Spindle stop | M05 |
| M06 | Tool change | T01 M06 |
| M08 | Coolant ON | M08 |
| M09 | Coolant OFF | M09 |
| M30 | Program end and reset | M30 |
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.
Understanding coordinate systems is essential for CNC programming.
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.
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.
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.
On many milling controls, tool length compensation is commonly associated with G43.
Example:
G43 H01 Z50
Here:
Cancellation is commonly:
G49
The exact method can vary between control systems.
Cutter compensation allows the control to compensate for the radius of the cutting tool.
Common codes are:
G40G41G42
Cancel cutter compensation.
Cutter compensation to the left.
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.
Circular movements are commonly programmed with:
G02G03
G02 X50 Y50 I25 J0
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
Canned cycles simplify repetitive machining operations.
A basic drilling example is:
G81 Z-10 R2 F100
Typical meanings:
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.
Two of the most important CNC parameters are:
Example:
S2000
This commonly means a spindle speed of 2000 RPM.
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
A typical FANUC-style milling program may use:
T01 M06
Meaning:
However, tool-management procedures vary between machine builders and controls.
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%
This is an educational example only. It must be adapted and verified for the specific machine before use.
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.
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.
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.
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 callL = linear movementFMAX = rapid/traverse movementF = feed rateM3 = spindle clockwiseM5 = spindle stopExact 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.
| Feature | FANUC | Siemens SINUMERIK | HEIDENHAIN |
|---|---|---|---|
| Common programming | ISO/G-code | Siemens + ISO | Klartext + ISO/G-code |
| Milling | Excellent | Excellent | Excellent |
| Turning | Excellent | Excellent | Available on suitable controls |
| Conversational programming | MANUAL GUIDE i | ShopMill / ShopTurn | Klartext |
| G-code | Yes | Yes | Yes |
| Advanced programming | Macro/Custom functions | High-level programming | Advanced cycles/Klartext |
| 5-axis | Available | Strong | Strong |
| Simulation/training | CNC Guide | Siemens training/simulation tools | Programming Station |
| Main strength | Broad industrial adoption | Flexibility & advanced technology | User-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.
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.
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:
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.
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
Simulation is an important part of modern CNC programming.
It can help identify:
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.
For someone learning CNC programming, I recommend studying these subjects in this order:
CNC programs control powerful machines capable of causing serious injury and equipment damage.
Before running any program:
Never run an unverified CNC program directly on a machine.
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.
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:
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.