Skip to content

Instantly share code, notes, and snippets.

@Igloczek
Last active October 22, 2025 15:52
Show Gist options
  • Select an option

  • Save Igloczek/f74b027594f0e82d5454ecf580bf99cc to your computer and use it in GitHub Desktop.

Select an option

Save Igloczek/f74b027594f0e82d5454ecf580bf99cc to your computer and use it in GitHub Desktop.
Klipper macro for semi-automated `rotation_distance` calibration

Klipper Extruder Calibration Macros

Why?

Klipper and Mainsail lack built-in extruder calibration. These macros automate it.

How It Works

  1. Mark the filament at 120mm before running the macro.
  2. Run:
    EXTRUDER_CALIBRATION
    
    • Heats nozzle, moves to center, extrudes 100mm, then cools down.
    • Measure remaining filament.
  3. Enter measured value:
    SET_EXTRUDER_CALIBRATION MEASURED_LENGTH=<your_value>
    
    (Replace <your_value> with the actual measurement.)
  4. Update printer.cfg manually with the new rotation_distance shown.

Example Output

Starting calibration...
Extruding 100mm...
Measure remaining filament and enter:
SET_EXTRUDER_CALIBRATION MEASURED_LENGTH=<your_value>
Old rotation_distance: 7.50
New rotation_distance: 8.62
Update printer.cfg manually!

Notes

  • Macros can’t pause for input, so it’s split into two steps.
  • No auto-saving to printer.cfg, manual update required.
[gcode_macro EXTRUDER_CALIBRATION]
description: "Start extruder calibration"
gcode:
RESPOND MSG="Starting calibration, make sure you already marked the 120mm spot on the filament"
RESPOND MSG="Pre-heating nozle"
SET_HEATER_TEMPERATURE HEATER=extruder TARGET=200
RESPOND MSG="Moving to save position"
G28
G90
{% set bed_x = printer.configfile.settings.stepper_x.position_max|float %}
{% set bed_y = printer.configfile.settings.stepper_y.position_max|float %}
{% set center_x = bed_x / 2 %}
{% set center_y = bed_y / 2 %}
G1 X{center_x} Y{center_y} Z100 F6000
RESPOND MSG="Waiting for nozle to be hot enough"
TEMPERATURE_WAIT SENSOR=extruder MINIMUM=200
RESPOND MSG="Extruding 100mm"
G91
M106 S100
G1 E100 F100
M400
M106 S0
G90
RESPOND MSG="Extursion finished, cooling down"
TURN_OFF_HEATERS
RESPOND MSG="Measure remaining filament and enter: SET_EXTRUDER_CALIBRATION MEASURED_LENGTH=your_value_in_mm"
[gcode_macro SET_EXTRUDER_CALIBRATION]
description: "Calculate new rotation_distance"
gcode:
{% if params.MEASURED_LENGTH|float > 0 %}
{% set requested_extrude_distance = 100 %}
{% set initial_mark_distance = 120 %}
{% set subsequent_mark_distance = params.MEASURED_LENGTH|float %}
{% set actual_extrude_distance = initial_mark_distance - subsequent_mark_distance %}
{% set previous_rotation_distance = printer.configfile.settings.extruder.rotation_distance|float %}
{% set new_rotation_distance = previous_rotation_distance * actual_extrude_distance / requested_extrude_distance %}
RESPOND MSG="Old rotation_distance: {previous_rotation_distance}"
RESPOND MSG="New rotation_distance: {new_rotation_distance}"
RESPOND MSG="Update printer.cfg manually!"
{% else %}
RESPOND MSG="ERROR: No valid measurement provided!"
{% endif %}
@jayg37
Copy link

jayg37 commented Oct 22, 2025

Great Macro! I made one small tweak to my setup to ensure smooth operation. In between line 23 and 24 I added an M400. This ensures Klipper waits for the extrusion command to finish prior to moving onto other comments within the macro. I had issues towards the end of the extrusion the extruder would start clicking rather than extrude. I believe this was due to the hotend already starting the cool down commands prior to the extrusion command finishing. This was also indicated by the console outputs which indicated extrusion complete way before it was done pushing filament. This minor change seems to have stopped the extruder from slipping during the last portion of the extrusion command.
See below for modification.

RESPOND MSG="Extruding 100mm"
G91
M106 S100
G1 E100 F100
M400
M106 S0
G90

@Igloczek
Copy link
Author

@jayg37 thanks, updated the macro with the missing M400

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment