# Run custom code between g-code commands?

I would like to be able to add custom commands/script to be executed during a print.

For example I would like to write some software to take a picture check the print hasn't moved off the bead between layers.

Does anyone know if any of the available software/firmware allows custom scripts or calling back to the computer before continuing printing?

I am happy to build/buy a new printer if anyone knows a control board that has this sort of feature.

• you can trigger by moving the head to a reserved place that activates a sensor. you can abort by intercepting a sensor like hot end temp that will abort the print, using a relay or mosfet or whatever Apr 17 '18 at 5:38
– 0scar
Apr 28 '18 at 12:09

A solution I use involves a 3d print server. I have defined shell scripts that address the GPIO ports of the Raspberry Pi that runs OctoPrint. OctoPrint is a 3d print server that can be accessed over your home network.

From the OctoPrint homepage:

OctoPrint is the snappy web interface for your 3D printer that allows you to control and monitor all aspects of your printer and print jobs, right from your browser.

This print server application allows for specification of custom Gcode commands (linked to system commands for instance; note this is a plug-in called "GCODE Systems Commands", see image below).

As an example, e.g. OCTO100 is scheduled to run fan_on.sh shell script. This script in its turn drives a relay to switch the annoying extruder cooling fan that is always on when the printer is powered. These codes can then be used throughout your sliced file to do stuff you want (e.g. by using the TweakAtZ plugin of Cura). E.g. my extruder fan will stay on several minutes before it is scheduled off after a successful print through OCTO110 which in itself runs the script fan_off.sh.

The scripts from the figure could be setup to schedule to do something, e.g. fan_on.sh controls a relay using port 22 of the GPIO of the Raspberry PI:

#!/bin/bash
gpio export 22 out
gpio -g write 22 0


So to disable the fan you would need fan_off.sh to be:

#!/bin/bash
gpio export 22 out
gpio -g write 22 1

• nice example of octoPi interation Apr 13 '18 at 14:48
• @professor79 thanks, this is just a sample, I also schedule the LED lights and can also switch the printer on and off. You can also add custom menu items, so all the scripts can also be activated through the GUI (menu) of OctoPrint
– 0scar
Apr 13 '18 at 14:54

One of the solutions could be adding a layer change script (simplifi3d has that out of the box) and then using marlin firmware you could set a value to digital pin that could triger external actions.

Layer Change G-Code: I personally haven't had to use this, but I'm sure that there are some excellent reasons/ideas to use for this. If you'd like for a G-Code script to be inserted in-between each layer, than you can simply place it in this tab. One interesting use of this, is for the FlashForge Dreamer, to have the lights blink in between each layer, however that can be a bit too much at times!

The syntax for the M42 command is: M42 S(value to be written to pin) P (pin number) e.g. To set digital pin 30 high, you would use M42 S1 P30

The MARLIN firmware will not enable you to change the status / write values to any of the pins in use for things such as the heaters, thermistors, end stops etc. The command will let you send values other than 0 and 1 to any pins which can output analogue values. (0-255)

I saw this 3 years ago, this is a closed loop control, I think is better that taking a photo then analyze it with a kind of optical recognition software which needs some of possible failures.

The video has also the link for the additional resources the 3d printer will need according his author.