2.2.16. Other custom G-Code for the Prusa i3 Mk3

You can also embed custom g-code at before and after every layer change, on tool (nozzle) changes and between sequentially-printed objects.

2.2.16.1. Automating temperature towers with g-code

Here’s an example of using before layer change G-code for changing temperatures on a temperature tower. This model consists of 10mm segments which are printed starting at 235C and decreasing by 5C at each 10mm layer. Note that layer_z is the PrusaSlicer placeholder for the current nozzle height in mm, not the layer number.

Listing 2.17 PrusaSlicer temperature test tower g-code
 1;BEFORE_LAYER_CHANGE
 2; layer_z is height of current layer in mm
 3G92 E0.0
 4;[layer_z]
 5{if layer_z <= 10}M104 S235
 6{elsif layer_z <= 20}M104 S230
 7{elsif layer_z <= 30}M104 S225
 8{elsif layer_z <= 40}M104 S220
 9{elsif layer_z <= 50}M104 S215
10{elsif layer_z <= 60}M104 S210
11{elsif layer_z <= 70}M104 S205
12{elsif layer_z <= 80}M104 S200
13{elsif layer_z <= 90}M104 S195
14{elsif layer_z <= 100}M104 S190
15{elsif layer_z <= 110}M104 S185
16{endif}

When inserted into PrusaSlicer under Printer Settings->Custom G-code->Before layer change G-code, this code compares the current height to the height at which each segment begins (multiples of 10mm at the start of each layer. The first range matched will terminate the tests, so we start testing for the lowest tiers first (10mm) and working our way up to the highest (110mm), settings extruder temps along the way (M104 Sxxx).

2.2.16.2. Automating linear advance test towers with g-code

Here’s another example of using before layer change G-code for inserting Linear Advance “K-value” settings on a test tower. This model consists of a 1mm high base with 10mm segments with stacked 10mm test segments. Each segment is printed with a K value that increments starting from -0.02 (M900 K-0.02) and increasing by 0.02 up to 0.30 (M900 K0.30). Note that layer_z is the PrusaSlicer placeholder for the current nozzle height in mm, not layer number.

Listing 2.18 PrusaSlicer Linear Advance test tower g-code
 1;BEFORE_LAYER_CHANGE
 2; layer_z is height of current layer in mm
 3; first match wins
 4G92 E0.0
 5;[layer_z]
 6{if layer_z <= 1}
 7M900 K0
 8{elsif layer_z <= 11}
 9M900 K-0.02
10{elsif layer_z <= 21}
11M900 K0
12{elsif layer_z <= 31}
13M900 K0.02
14{elsif layer_z <= 41}
15M900 K0.04
16{elsif layer_z <= 51}
17M900 K0.06
18{elsif layer_z <= 61}
19M900 K0.08
20{elsif layer_z <= 71}
21M900 K0.10
22{elsif layer_z <= 81}
23M900 K0.12
24{elsif layer_z <= 91}
25M900 K0.14
26{elsif layer_z <= 101}
27M900 K0.16
28{elsif layer_z <= 111}
29M900 K0.18
30{elsif layer_z <= 121}
31M900 K0.20
32{elsif layer_z <= 131}
33M900 K0.24
34{elsif layer_z <= 141}
35M900 K0.26
36{elsif layer_z <= 151}
37M900 K0.28
38{elsif layer_z <= 161}
39M900 K0.30
40{endif}

When inserted into PrusaSlicer under Printer Settings->Custom G-code->Before layer change G-code, this code compares the current height to the height at which each segment begins (multiples of 10mm at the start of each layer. The first range matched will terminate the tests, so we start testing for the lowest tiers first (10mm) and working our way up to the highest (110mm), settings linear advance along the way (M900 Kxxx).

2.2.16.3. Changing print speeds at higher layers

It can be useful to reduce print speeds on taller prints. Inserting something as simple as:

M220 S{if layer_z < 20}100{else}90{endif} ; M220 adjusts feed rates to 90% at 20mm height

into PrusaSlicer under Printer Settings->Custom G-Code->Before layer change g-code can be done to automate this process.

Contact and feedback

You can find me on the Prusa support forums or Reddit where I lurk in many of the 3D printing-related subreddits. I occasionally drop into the Official Prusa 3D discord server where I can be reached as bobstro (bobstro#9830). You can email me directly at projects@ttlexceeded.com.

Last edited on Jun 23, 2021. Last build on Oct 22, 2021.