H codes in CNC programming are tool length offset codes that specify compensation values for differences in tool lengths. They work with G codes (e.g., G43, G44) to adjust the spindle’s Z-axis position, ensuring consistent cutting depths regardless of tool size variations. Each H code (e.g., H1, H2) links to a stored offset value in the machine’s controller, critical for multi-tool operations and precision machining.
What is the H code in CNC?

1. Core Function: Tool Length Compensation

H codes primarily enable tool length compensation, a mechanism to account for variations in tool lengths without rewriting the entire program. When machining with multiple tools (e.g., a drill, end mill, and tap), each tool has a unique length—from the spindle nose to the cutting edge. Without compensation, these differences would result in incorrect Z-axis positions, leading to overcuts or undercuts.
H codes solve this by associating each tool with a premeasured offset value stored in the controller’s offset table. For example:

 

  • Tool 1 (a 10mm drill) has a length of 75mm from the spindle; its offset (H1) is set to 75.0mm.
  • Tool 2 (a 15mm end mill) has a length of 80mm; its offset (H2) is 80.0mm.
When the program calls G43 H1 Z50;, the controller adds H1’s 75.0mm offset to the Z-axis command, positioning the drill’s tip at the correct depth relative to the workpiece. This ensures both tools cut to the same Z=50mm coordinate despite their physical length differences.

2. Syntax and Programming Integration

H codes follow a specific syntax, tightly integrated with G codes to activate compensation:

 

  • FormatHxx, where xx is a two-digit number (01–99) representing the offset index. For example, H05 refers to the 5th entry in the tool length offset table.
  • Activation with G Codes: H codes rarely work alone; they pair with G43 (positive compensation) or G44 (negative compensation) to apply offsets:
    • G43 H01 Z100; applies the offset stored in H01 to the Z-axis, moving the tool to Z100 plus the offset.
    • G44 H02 Z50; subtracts the H02 offset from Z50, used for tools shorter than the reference length.
  • Cancellation: G49 cancels tool length compensation, resetting the Z-axis to raw coordinates: G49 Z20;
Notably, H codes are modal in some systems—once called, the offset remains active until replaced by another H code or canceled by G49. This reduces code redundancy in multi-tool programs.

3. Offset Measurement and Setup

Accurate H code values depend on precise tool length measurement, done through two common methods:
  • Manual Measurement: Using a tool height gauge, operators measure the distance from the spindle nose to the tool tip, then input this value into the controller’s offset table (e.g., H03 = 62.35mm). This is cost-effective for small shops but prone to human error.
  • Automatic Tool Setter: CNC machines with integrated laser or touch probes (e.g., Renishaw TS27R) measure tool lengths automatically. When a tool is loaded, the probe touches its tip, calculates the offset, and stores it in the assigned H code. This method achieves ±0.001mm accuracy, critical for aerospace or medical parts.
For example, in a production line machining aluminum brackets, an automatic setter measures a new end mill, assigns its length to H07 (78.12mm), and updates the offset table—ensuring the program uses the correct value without manual input.

4. Machine-Specific Variations

H code behavior varies slightly across CNC systems, requiring familiarity with manufacturer-specific rules:
  • Fanuc Controls: Fanuc uses H codes exclusively for tool length offsets, with offsets stored in parameters 1200–1299 (H01 = 1201, H02 = 1202, etc.). They require explicit G43/G44 activation; omitting these codes leaves H values unused.
  • Siemens Sinumerik: Siemens integrates H codes into “tool management” where each tool (T code) is linked to an H code automatically. For example, T1 M6; (tool change) may call H1 by default, simplifying programming.
  • Haas Machines: Haas allows H codes to reference either tool length or diameter offsets (via parameters), but this is rare. Most shops use H for length and D for diameter to avoid confusion.
These differences mean programmers must adjust code structure when switching machines—e.g., a Fanuc program using G43 H05 may need reformatting for Siemens’ automatic T-H linking.

5. Common Errors and Troubleshooting

Misusing H codes leads to dimensional errors or collisions; key issues include:
  • Unassigned H Codes: If a program calls G43 H10 Z50; but H10 has no stored value (default 0), the tool will cut 10mm too deep (assuming the actual tool length is 10mm). Solution: Always verify offset table entries before running a program.
  • Incorrect Offset Values: A typo (e.g., entering 65.0mm instead of 56.0mm for H04) causes the tool to overshoot. Fix: Re-measure the tool with a setter and update the offset.
  • Compensation in Wrong Axis: H codes apply only to Z-axis (mills) or X-axis (lathes for tool length). Accidentally using H for Y-axis movement triggers errors.
  • Forgotten G49: Leaving compensation active after tool changes can carry over offsets to new tools. Prevention: Include G49 in tool change sequences (e.g., T2 M6 G49;).
For example, in machining a 5mm-deep slot, an unassigned H03 (0mm offset) with a 70mm tool would position the tip at Z5mm instead of Z75mm, cutting 70mm too deep and damaging the workpiece or tool.

Recommended Reading