Klipper Macros and How to Use Them

Klipper Macros and How to Use Them

Klipper is a 3D printer firmware that runs on a Raspberry Pi or other single-board computers. It's designed to be faster and more flexible than traditional 3D printer firmware, and it allows for a number of advanced features, such as using macros to automate repetitive tasks.

In this article, we'll take a look at what Klipper macros are, how to create them, and how to use them in your 3D printing workflow.

What are Klipper macros?

Klipper macros are essentially scripts that you can use to automate repetitive tasks or sequences of G-code commands. They allow you to save time and reduce the likelihood of errors by creating a single command that performs multiple actions.

For example, you could create a macro that homes your printer, heats up the extruder and bed, primes the nozzle, and starts printing all with a single command. This would be much faster and less prone to error than manually entering each command.

How to create a Klipper macro

Creating a Klipper macro is fairly simple. Here's an example of how to create a macro that homes your printer and then moves the nozzle to a specific position:

  1. Open your Klipper configuration file in a text editor.
  2. Add the following lines:
[gcode_macro HOME_AND_CENTER]
gcode: G28 ; home all axes G1 X100 Y100 Z50 F3000 ; move nozzle to center of bed

This creates a macro called HOME_AND_CENTER that homes all axes and then moves the nozzle to the center of the bed at a speed of 3000 mm/min.

  1. Save your configuration file and restart Klipper.

How to use a Klipper macro

Using a Klipper macro is just as easy as creating one. Here's how to use the macro we created above:

  1. Open your terminal program and connect to your printer using the command "sudo screen /dev/ttyUSB0 115200" (replace "/dev/ttyUSB0" with your printer's serial port).
  2. Type "HOME_AND_CENTER" (without quotes) and press enter.
  3. Watch as your printer homes all axes and then moves the nozzle to the center of the bed.

And that's it! You've just used a Klipper macro to automate a sequence of G-code commands.

In most cases you will be utilizing your macros with Fluidd, Mainsail or even directly from your printer using Klipperscreen. Additionally macros are run from the G-Code as well. SuperSlicer and PrusaSlicer are utilized. For SuperSlicer it is a simple matter of editing the Custom G-Code settings on the Printer Settings page. This is how I use my Start Print macro. In my case I pass data to Klipper in the form of extruder temp and bed temp.

START_PRINT EXTRUDER_TEMP=[first_layer_temperature] BED_TEMP=[first_layer_bed_temperature]

Macro Best Practices

My preference is to write all of my macros in a separate file. In this example I have created a file named okuru-macros.cfg. Feel free to copy the contents of my example file below, it has several important macros that I use all the time.

STEP 1: Create a file named okuru-macros.cfg and paste in the markup from the file below:

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ OKURU Macros ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #

# ~~ Calibration ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #

[gcode_macro LEVEL_GANTRY]
description: Level Gantry - Hazaa

gcode:
  Z_TILT_ADJUST

  

[gcode_macro CALIBRATE_Z]
description: Calibrate Z offset

gcode:
  PROBE_CALIBRATE



[gcode_macro CALIBRATE_E]
description: Calibrate Extruder

gcode:
  M117 Extruding 100mm
  G91                     # Set Relative Positioning
  G1 E100 F60              # Extrude 100mm

  M117 Done Extruding



# ~~ Control ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #

[gcode_macro DISABLE_STEPPERS]
description: Disable all stepper motors

gcode:
  M84

[gcode_macro STOP_PRINT]
description: Stop Printing
gcode:
  CANCEL_PRINT_BASE
  M117 Stopped - Heat/Motors On
  G91               # Set Relative Positioning
  G1 E-3 Z+10 F3000 # Move Z Up
  G90               # Set Absolute Positioning
  G1 X10 Y220 F2000 # Present Print
  


[gcode_macro CANCEL_OPERATION]
description: Cancel entire operation
gcode:
  CANCEL_PRINT
  M117 Cancelled - All Off
  G91               # Set Relative Positioning
  G1 E-3 Z+10 F3000 # Move Z Up
  G90               # Set Absolute Positioning
  G1 X10 Y220 F2000 # Present Print
  
  # Disable
  # M104 S0
  # M140 S0
  # M106 S0
  M84
  BED_MESH_CLEAR


# ~~ Start & End ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #

[gcode_macro START_PRINT]
gcode:
  {% set BED_TEMP = params.BED_TEMP|default(50)|float %}
  {% set EXTRUDER_TEMP = params.EXTRUDER_TEMP|default(190)|float %}


  M117 Warming up yo 
  M104 S{EXTRUDER_TEMP}         # Get Extruder started             
  M190 S{BED_TEMP}              # Heat Bed


  M117 Homing
  G90                           # Set Absolute Positioning
  SET_GCODE_OFFSET Z=0.0        # Reset the G-Code Z offset (adjust Z offset if needed)
  G28                           # Home the printer
  BED_MESH_PROFILE LOAD=default # Use the bed mesh
  G1 X15 Y15 Z15 F3000          # Move the nozzle near the bed 

  M117 Extruder Heating
  M109 S{EXTRUDER_TEMP}         # Heat Extruder
    
  PRIME_LINE
  
  M117 Printing...



[gcode_macro END_PRINT]
gcode:
  M117 Done Printing :)

  G91               # Set Relative Positioning
  G1 E-3 Z+10 F3000 # Move Z Up
  G90               # Set Absolute Positioning
  G1 X10 Y220 F2000 # Present Print

  # Disable
  M104 S0
  M140 S0
  M106 S0
  M84
  BED_MESH_CLEAR



# Prime Line
[gcode_macro PRIME_LINE]
gcode: 
  M117 Prime Time

  G92 E0                          # Reset Extruder
  G1 Z2.0 F3000                   # Move Z Axis up
  G1 X10 Y20 Z0.28 F5000.0        # Move to start position
  G1 X10 Y200.0 Z0.28 F1500.0 E15 # Draw the first line
  G1 X12 Y200.0 Z0.28 F5000.0     # Move to side a little
  G1 X12 Y25 Z0.28 F1500.0 E30    # Draw the second line
  G92 E0                          # Reset Extruder
  G1 Z2.0 F3000                   # Move Z Axis up

STEP 2: Open the printer config file and add the include statement below the statement that included Fluidd:

[include fluidd.cfg]
[include okuru-macros.cfg]

STEP 3: Restart Klipper

That's it. Your fresh new macros are ready to go.

Conclusion

Klipper macros are a powerful tool that can save you time and reduce the likelihood of errors in your 3D printing workflow. By creating macros to automate repetitive tasks, you can streamline your printing process and focus on the more creative aspects of 3D printing. With a little bit of practice, you'll be creating your own macros in no time!