Customizing Slic3r Prusa Edition g-code

The default g-code that ships with Slic3r Prusa Edition works just fine, but I found myself wanting to tweak a few things to my liking. I have updated my startup g-code to incorporate several functions that I want to use on every print:

  • A 2-step “no ooze” nozzle warmup routine to expedite startup, but avoid dots and strings caused by molten filament dripping out during the mesh bed leveling and bed warmup processes.
  • Position the nozzle away from the PEI print surface during warmup and startup motion to avoid damaging the print surface.
  • PINDA warmup to ensure a consistent 35C PINA temperature before performing mesh bed leveling to ensure consistent results.
  • A modified prime line print routine to catch errant nozzle ooze, test extrusion and perform a final wipe action to avoid stringing between the prime line and start of the print.

Note

These notes are based on my experiences with the Prusa i3 Mk3 printer. If you are using a different printer, please verify the hardware details are same. These pages may be a bit rough as I revise them and add new material. Please check back regularly for updates.

I’ve incorporated these features into my own customized g-code settings for Slic3r Prusa Edition.

Caution

Please be sure that you’ve calibrated your printer, and particularly your Live-Z settings before using any of the following examples. These routines move the nozzle around close to the bed, and could cause damage if your printer is not adjusted properly.

Start G-Code

I’m not a fan of loud peizo beeps, much less music, but I have inserted several chirp sounds (M300 S100 P10) to indicate progress throughout the print.

Here’s a line-by-line breakdown:

Version control

The first lines are simple date and firmware version stamps. If you print generated gcode on a printer running an older firmware version, you’ll get a warning message to update. You can press the front knob to continue and ignore this warning, but updating to recent firmware is always recommended to take full advantage of enhanced features that Prusa is continually adding to the printer firmware and Slic3r Prusa Edition.

Listing 1 Slic3r Prusa Edition start g-code version control
1
2
; Last updated 20190405
M115 U3.5.1 ; tell printer latest fw version

Initial warmup temperatures

The next stanza sets preliminary warmup temperatures.

  • Line 4 sets the extruder (nozzle) temperature to 160C, a good “no ooze” temp that will soften most filament, but not hot enough to cause filament to trickle out of the nozzle. This will prevent dots of filament and stringing during mesh bed leveling.
  • Line 5 sets the bed temperature to the higher of the specified bed temperature or 65C. This is to ensure that the bed is hot enough to warm up the PINDA even on cold days. See the PINDA warmup notes for details.
Listing 2 Slic3r Prusa Edition start g-code initial warmup temps
3
4
5
; Set initial warmup temps
M104 S160 ; set extruder no-ooze temp
M140 S{max(first_layer_bed_temperature[0],65)}  ; set bed PINDA warmup temp

Initial homing

The next stanza prepares for initial homing. On startup, the printer has no way of knowing where the extruder is in 3D space, so homing is necessary for it to establish a known position. This is to ensure that any filament hanging from the nozzle is sufficiently soft to either break off or bend when the nozzle is lowered in the next line. This avoids creating dents in the PEI sheet at the lower-left corner.

  • Line 7 waits for the nozzle warmup temp specified in line 4, above.
  • Line 9 performs homing, moving the printer to the lowest (Z 0) left (X 0) front (Y 210) coordinates to establish the current position.
Listing 3 Slic3r Prusa Edition start g-code nozzle warmup
6
7
8
9
; Nozzle warmup before home to avoid driving hardened ooze into PEI bed
M109 ; wait for extruder no-ooze warmup temp before mesh bed leveling, cool hot PINDA
M300 S40 P10 ; chirp
G28 W ; home all without mesh bed level

PINDA warmup

The next stanza waits for the PINDA probe to reach a consistent temperature before automatic mesh bed leveling. For more info on the PINDA warmup procedure, refer to the about PINDA warmup notes.

  • Line 12 raises the nozzle 3mm to avoid possibly damaging the PEI sheet during nozzle movement in the steps that follow.
  • Line 14 moves the nozzle to the center (X 125) rear (Y 210) to allow access to the bed for final inspection and cleaning while waiting for the PINDA warmup in the following steps.
  • Line 15 lowers the nozzle and PINDA probe close to the bed (Z 0.10mm) to aid in warming up the PINDA probe.
  • Line 16 issues to command to wait for the PINDA probe to reach at least 35C, an all-around good temperature for mesh bed leveling. Note that this can take a long time (5-10 minutes) on the first print on a cold day. For subsequent prints, it should warm up much more quickly.
  • Line 17 raises the nozzle 3mm (Z3) for movement as a precaution before rapid moves close to the print surface.
Listing 4 Slic3r Prusa Edition start g-code PINDA warmup
10
11
12
13
14
15
16
17
; PINDA warmup routine
M300 S40 P10 ; chirp
G0 Z3; Raise nozzle before move
; Present bed for final cleaning
G0 X125 Y210 F10200; Move nozzle to PINDA warming position
G0 Z0.10 F10200; Lower nozzle to PINDA warming position
M860 S35 ; wait for PINDA temp to stabilize
G0 Z3; Raise nozzle before move

Mesh bed leveling and final warmup

Now that the PINDA is warmed up to a consistent temperature, we can perform mesh bed leveling.

  • Line 19 performs the actual leveling. With the latest firmware releases, this will do either 3X3 or 7X7 grid leveling as specified in the printer setup menu.
  • After leveling, the nozzle will be returned to the home position. Line 20 raises the nozzle 3mm (Z3) while we wait for the nozzle to come up to full print temperature. This both avoids possibly damaging the PEI sheet under the nozzle, and also provides a clean prime line in the steps that follow.
  • Line 21 sets the final bed temperature to the user-specified temperature specified in the slicer settings. This may be warmer or cooler than the 65C PINDA warmup temp.
  • Lines 23 and 24 set (M104) and wait for (M109) thefinal nozzle print temperatures specified in the slicer settings.
  • Line 25 waits for the bed temperature to settle before printing.
Listing 5 Slic3r Prusa Edition start g-code mesh bed leveling & final warmup
18
19
20
21
22
23
24
25
M300 S40 P10 ; chirp
G80 ; mesh bed leveling
G0 Z3; Raise nozzle to avoid denting bed while nozzle heats
M140 S[first_layer_bed_temperature] ; set bed final temp
; Final warmup routine
M104 S[first_layer_temperature] ; set extruder final temp
M109 S[first_layer_temperature] ; wait for extruder final temp
M190 S[first_layer_bed_temperature] ; wait for bed final temp

Prime line

Once the hotend comes up to temp, a custom prime line will be printed. I’ve made some tweaks to the bog-standard line.

  • Line 28 lowers the nozzle (Z-3) to initial printing position.
  • Line 29 specifies relative exturder positioning mode.
  • Line 30 disables Linear Advance (M900 K0) for printing the prime line. With LA enabled, I often get distracting extruder clicks and skips when printing the initial thick line.
  • Lines 32 moves to the initial prime line position (X=0, Y=-3).
  • Line 33 extrudes an intial “blort” of filament without moving to trap any ooze or string remaing on the nozzle.
  • Line 34 extrudes 6mm of filament (E6) while moving 20mm along the X axis (X20.0).
  • Line 35 extrudes 3.2mm of filament (E3.2) while moving another 40mm along the X axis (X60).
    • Line 36 extrudes 6mm of filament (E6) while moving another 40mm along the X axis (X100).
  • Line 37 retracts 0.8mm of filament (E-0.8) to reduce stringing.
  • Line 38 performs a wipe action back over the printed line along the X axis (X99.5) without extruding (E0).
  • Line 39 moves another 10mm along the X axis (X110.0) without extruding (E0) to complete the wiping action and eliminate any stringing.
  • Line 40 de-retracts (pushes) 0.6mm of filament (E0.6) to re-prime after the retraction in line 37.
Listing 6 Slic3r Prusa Edition start g-code prime line
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
; Prime line routine
M300 S25 P10 ; chirp
G0 Z0.15; Restore nozzle position
M83  ; extruder relative mode
M900 K0; Disable LA for prime line
G92 E0.0 ; reset extrusion distance
G1 Y-3.0 F1000.0 ; go outside print area
G1 E2 F1000 ; de-retract and push ooze
G1 X20.0 E6  F1000.0 ; fat 20mm intro line @ 0.30
G1 X60.0 E3.2  F1000.0 ; thin +40mm intro line @ 0.08
G1 X100.0 E6  F1000.0 ; fat +40mm intro line @ 0.15
G1 E-0.8 F2100; retract to avoid stringing
G1 X99.5 E0 F1000.0 ; -0.5mm wipe action to avoid string
G1 X110.0 E0 F1000.0 ; +10mm intro line @ 0.00
G1 E0.6 F1500; de-retract

Final print adjustments

Finally, we do any last minute print adjustments. I’ve commented out line 43, but it shows an example of extrusion rate adjustment. Adjust for your printer.

Listing 7 Slic3r Prusa Edition start g-code final print adjustments
41
42
43
G92 E0.0 ; reset extrusion distance
; Final print adjustments
; M92 E282.54 ; adjust extrusion ratio

At this point, the print will proceed normally.

Full startup g-code example

Here’s the full start g-code:

Listing 8 Slic3r Prusa Edition start 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
41
42
43
44
; Last updated 20190405
M115 U3.5.1 ; tell printer latest fw version
; Set initial warmup temps
M104 S160 ; set extruder no-ooze temp
M140 S{max(first_layer_bed_temperature[0],65)}  ; set bed PINDA warmup temp
; Nozzle warmup before home to avoid driving hardened ooze into PEI bed
M109 ; wait for extruder no-ooze warmup temp before mesh bed leveling, cool hot PINDA
M300 S40 P10 ; chirp
G28 W ; home all without mesh bed level
; PINDA warmup routine
M300 S40 P10 ; chirp
G0 Z3; Raise nozzle before move
; Present bed for final cleaning
G0 X125 Y210 F10200; Move nozzle to PINDA warming position
G0 Z0.10 F10200; Lower nozzle to PINDA warming position
M860 S35 ; wait for PINDA temp to stabilize
G0 Z3; Raise nozzle before move
M300 S40 P10 ; chirp
G80 ; mesh bed leveling
G0 Z3; Raise nozzle to avoid denting bed while nozzle heats
M140 S[first_layer_bed_temperature] ; set bed final temp
; Final warmup routine
M104 S[first_layer_temperature] ; set extruder final temp
M109 S[first_layer_temperature] ; wait for extruder final temp
M190 S[first_layer_bed_temperature] ; wait for bed final temp
; Prime line routine
M300 S25 P10 ; chirp
G0 Z0.15; Restore nozzle position
M83  ; extruder relative mode
M900 K0; Disable LA for prime line
G92 E0.0 ; reset extrusion distance
G1 Y-3.0 F1000.0 ; go outside print area
G1 E2 F1000 ; de-retract and push ooze
G1 X20.0 E6  F1000.0 ; fat 20mm intro line @ 0.30
G1 X60.0 E3.2  F1000.0 ; thin +40mm intro line @ 0.08
G1 X100.0 E6  F1000.0 ; fat +40mm intro line @ 0.15
G1 E-0.8 F2100; retract to avoid stringing
G1 X99.5 E0 F1000.0 ; -0.5mm wipe action to avoid string
G1 X110.0 E0 F1000.0 ; +10mm intro line @ 0.00
G1 E0.6 F1500; de-retract
G92 E0.0 ; reset extrusion distance
; Final print adjustments
; M92 E282.54 ; adjust extrusion ratio
;M221 S{if layer_height>=0.32}95{else}100{endif} ; compensate for large layer height

End G-Code

My end g-code is pretty standard. Several gcode settings will persist between prints if not reset, so I like to ensure everything is at defaults on completion.

  • Line 4 retracts 2mm of filament (E-2).
  • Line 5 raises the nozzle 60mm or up to the maximum print area of 210mm, whichever is lower.
  • Line 6 moves the extruder to the far left (X0) and forward (Y200) to present the print for easy removal.
  • Line 7 resets print speed over-rides made with the front knob.
  • Line 8 resets extruder factor adjustments made with the front knob.
  • Line 9 resets Linear Advance.
  • Lines 10-13 shut down the printer hardware.

Here’s my current end g-code:

Listing 9 Slic3r Prusa Edition end g-code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
; Last updated 20190404
G4 ; wait
G92 E0 ; prepare to retract
G1 E-2 F4800; retract quickly to avoid ooze
{if layer_z < max_print_height}G1 Z{z_offset+min(layer_z+60, max_print_height)}{endif} ; Move print head up
G0 X0 Y200 F10200; present bed
M220 S100 ; reset speed factor to 100%
M221 S100 ; reset extruder factor to 100%
M900 K0 ; reset linear acceleration
M104 S0 ; turn off temperature
M140 S0 ; turn off heatbed
M107 ; turn off fan
M84 ; disable motors
M300 S100 P10 ; chirp

Other custom G-Code

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

Here’s an example of using before layer change G-code for changing temperatures on gaaZolee’s temperature calibration tower. This model consists of segments atop a 1.6mm high base. Note that layer_z is the Slic3r Prusa Edition placeholder for the current nozzle height in mm, not layers.

At each layer change, this code compares the current height to the height each segment begins (multiples of 10mm starting a 1.6mm). The first match will terminate the tests, so we start testing for the highest tiers first (91.6mm) and working our way down to the lowest (1.6mm), settings extruder temps along the way (M104 Sxxx).

As written, this g-code depends on layers changing at exactly the specified heights. If you’re not using changes at exact multiples of your layer height, you will want to specify ranges using the <= and >= operators.

Listing 10 Slic3r Prusa Edition layer change g-code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
;BEFORE_LAYER_CHANGE
G92 E0.0
;layer_z is height of current layer in mm
;[layer_z]
{if layer_z == 91.6}M104 S220
{elsif layer_z == 81.6}M104 S225
{elsif layer_z == 71.6}M104 S230
{elsif layer_z == 61.6}M104 S235
{elsif layer_z == 51.6}M104 S240
{elsif layer_z == 41.6}M104 S245
{elsif layer_z == 31.6}M104 S250
{elsif layer_z == 21.6}M104 S255
{elsif layer_z == 11.6}M104 S260
{elsif layer_z == 1.6}M104 S265
{endif}

Slic3r Prusa Edition placeholders

Here are the most commonly used placeholders (slice-time variables) used by Slic3r Prusa Edition.

Table 1 Slic3rPE G-Code Placeholders
Value Placeholder
First Layer Temperature [first_layer_temperature]
First Layer Bed Temperature [first_layer_bed_temperature]

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.