3D Printing : Filament runout Sensor


Now that you have the perfect enclosure, you are looking at improving it ! One add-on you may think at is a filament runout sensor. There is nothing more frustrating than a failed print because your spool is empty. On the other end, sometimes, you are reluctant to start printing with a almost exhaust spool. Then, I have a fix for you !

Let's do it!

On AliExpress you can find what you are looking for. This is actually the only purchase you have to do  and it is less than USD 2 (1.5 EUR).
Many blog posts and YouTube videos are modifying the Ender 3 board by soldering an extra plug and modifying the firmware. I'm not into this trip at all. Why ? Because I'm not an electronic nor soldering specialist and I wanted something easy to do without software modification on the printer itself.

The idea is to use the cheap sensor (actually a stop switch/micro-switch) plug on a Raspberry Pi running OctoPrint and then grab the status with an OctoPrint plugin - Easy!


- Micro switch found on AliExpress here;
- 3D Printed micro-switch arm from Thingiverse;
- 2 x M3 screws and nuts;
- 1 x Raspberry Pi running OctoPrint (OctoPi)

The supporting arm is attached to my extruder filament guide. I have modified an existing one, easy and neat. The micro-switch is attached on top and the cable is secured to avoid any bad surprises. Be sure to align the filament coming from the spool and the filament guide :  do not bend the filament line, there are positioning holes to ease the process. Otherwise you will add too much friction and filament doesn't like that. I have tested some retraction and this is fine, it works. If you like it, you can use it, I have posted it on thingiverse.


Next, connect the micro-switch to the Raspberry GPIO pins. In my case, I use PIN 4 & 6 for power and ground respectively and the signal PIN is 18. Warning, the pin out numbering that I'm using is the pin out numbering of the board. There are 2 ways to identify the GPIO pins. I prefer to use the board numbering since this is easy to debug by just looking at the board itself.

When the sensor is powered AND when a filament is placed inside the hole, there is a green LED that confirms that the filament is present (and the micro-switch is powered on). 

To test the sensor, I have written a small python code (gpio.py) : 

import RPi.GPIO as GPIO

import time

# Tell we use physical PIN numbering convention


# configure pin 18 as an input

GPIO.setup(18, GPIO.IN)

while True:

input_value = GPIO.input(18) 

     if input_value == 1 :

print("Filament present")

elif input_value == 0 :

print("Filament ABSENT!!")


You may run the script with the following command : 

python gpio.py

If a filament is inside the sensor (switch closed) it will display Filament present if the filament is removed it will display Filament ABSENT!! continuously every 0.5 sec until you kill the script (CTRL+C).

Once you have confirmed that everything works, you are ready for the next step.

OctoPrint plugin

On the OctoPrint side, we need to install a plugin to manage the end of filament event triggered by the sensor (micro-switch). I've chosen Filament Sensors Revolutions. I've tried many (the famous reloaded as well) but none worked for me excepted this one.

Easy to install using the Plugin Manager, the setup is also very easy. This plugin supports 2 sensors, in my case, I only have one and not using the Jam sensor. Therefore, the PIN is set to -1 (it means disabled).

The GCODE is used to pause the print are the following : 

Pause : 

M117 Print Paused
G1 Z10 F1000

Resume : 

M117 Print Resumed
G1 Z-10 F1000

You will configure those GCODE in the GCODE Scripts section of the OctoPrint configuration screen : 

To test the plugin, you need to start a print to enable it and check the logs located in /home/pi/.octoprint/logs/octoprint.log

Here is sample : 

2020-08-02 23:37:48,252 - octoprint.plugins.filamentrevolutions - INFO - Filament Sensors Revolutions
2020-08-02 23:37:48,253 - octoprint.plugins.filamentrevolutions - INFO - Pins not configured, won't work unless configured!
2020-08-02 23:39:13,932 - octoprint.plugins.filamentrevolutions - INFO - Using Board Mode
2020-08-02 23:39:13,934 - octoprint.plugins.filamentrevolutions - INFO - Filament Runout Sensor
2020-08-02 23:39:13,937 - octoprint.plugins.filamentrevolutions - INFO - Jam Sensor Pin not configured
2020-08-02 23:39:38,882 - octoprint.plugins.filamentrevolutions - INFO - PrintStarted: Enabling filament runout sensor.
2020-08-02 23:42:04,248 - octoprint.plugins.filamentrevolutions - INFO - Out of filament!
2020-08-02 23:42:04,250 - octoprint.plugins.filamentrevolutions - INFO - Pausing print.
2020-08-02 23:42:24,084 - octoprint.plugins.filamentrevolutions - INFO - Filament detected!
2020-08-02 23:42:35,245 - octoprint.plugins.filamentrevolutions - INFO - PrintResumed: Enabling filament runout sensor.

When I cut the filament while printing, as soon as the micro-switch detects the filament is not present anymore, we clearly see the printer going into pause. Then, I replace the filament, make sure we have a little extrusion, then I push resume on OctoPrint and the nozzle is going back to it original position, and resume printing.

This is really a must ! Ideally, I should find a way to send a notification so I can go to the printer and act asap. Next step ;)

Hope this helps !


  1. I've had great success with this one. It plugs directly into your Pi without any modification. https://www.amazon.com/dp/B09ZZBR28V?ref=myi_title_dp


Post a Comment

Thank you for your message, it has been sent to the moderator for review...

What's hot ?

RClone : Mount Google Drive on any File System

Wallbox : Get The Most Of It (with API)