1.2.15. 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.

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

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

1.2.15.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 Jun 23, 2021.