Introduction: 3D Printer Filament Sensor
While looking for a design project for school, my group and I were made aware that our college's 3D printers were have several issues. The first issue was bed adhesion. The second, and topic of this guide, was filament breaking during long prints.
Looking on the internet to see if there was a readily available solution found scores of projects using the marlin firmware's filament detector, but this was just a simple switch that detected the presence of filament. We wanted to detect filament motion to see if the printer was extruding properly.
Step 1: Materials
What you will need for this project.
An ATtiny85 (code will be added for the Uno/Nano) (ATtiny85)
A 3d printer with Marlin firmware and an LCD with knob installed
soldering iron (this is the one I own)
crimping tool (link here)
perfboard or blank circuit board (link here)
wire (link here)
connectors/ headers (link here)
a rotary encoder (link here)
a 5v voltage regulator (link here) or power supply (link here)
a couple 10k resisters (kit link here)
Various 3d printed parts (stl files included)
Step 2: Flashing Your Micro-controller
Included are files for flashing this project to your ATtiny85 with the Arduino IDE. (Files for the Uno and Nano will be added soon!)
ATtiny85:
Open the project with the Arduino IDE
Add the TinyDebugSerial.h library found (here)
Connect your Tiny with you favorite programmer
Flash the Arduino
Congratulations your micro-controller is ready to be connected
Attachments
Step 3: Modifying Marlin
If you have a Lulzbot Taz 5 you can download the modified firmware HERE and flash your printer with it. There is an excellent guide on how to flash your 3D printer (here)
Open the version of Marlin that you have installed on your printer using the arduino IDE and save it under a new file name. Next we want to turn on the filament detection function by going into the configuration.h tab and uncommenting "#define FILAMENT_RUNOUT_SENSOR"
Next we want to configure what where the printer goes when it detects the error. Go to the Configuration_adv.h tab and look for this section of code.
// Add support for experimental filament exchange support M600; requires display
#if ENABLED(ULTIPANEL)
#define FILAMENTCHANGEENABLE
#if ENABLED(FILAMENTCHANGEENABLE)
#define FILAMENTCHANGE_XPOS 3
#define FILAMENTCHANGE_YPOS 3
#define FILAMENTCHANGE_ZADD 10
#define FILAMENTCHANGE_FIRSTRETRACT -2
#define FILAMENTCHANGE_FINALRETRACT -30 //
#define AUTO_FILAMENT_CHANGE //This extrude filament until you press the button on LCD #define AUTO_FILAMENT_CHANGE_LENGTH 30 //Extrusion length on automatic extrusion loop
#define AUTO_FILAMENT_CHANGE_FEEDRATE 300 //Extrusion feedrate (mm/min) on automatic extrusion loop
#endif
#endif
I have changed the XPOS and YPOS to move the extruder to the front of the printer to make it easier to service the printer. You can change any of the numbers to accommodate your specific printer.
We'll now go in under the stepper motor function and add a variable for outputting the motor control signal.
On the stepper_indirection.h tab search for "#define E_STEP_WRITE(v) {E0_STEP_WRITE(v)}" and change it to "#define E_STEP_WRITE(v) {E0_STEP_WRITE(v); E1_STEP_WRITE(v);}// !!! writes to E0 and E1"
Finally we'll add variables under program for the board that our printer uses. The printers I'm working on use the Rambo. Open the code for your printer's board and change "#define E1_STEP_PIN XX" to
"#define E1_STEP_PIN 23//(33 original) !!! changed for extruder step output" I saved the original pin in the comments so I could change it back later if I needed to.
Select your board under the tools option connect via USB and upload the firmware to your printer.
Attachments
Step 4: 3D Printed Parts
Here are the .stl files for the rotary encoder mount. This mount is designed for, and works wonderfully with the Taz5 printer. In order to hold the filament guide we u
Step 5: Installing the Rotary Encoder
For this step you will need the rotatory encoder, 3d printed parts, and something to affix the encoder mount to your printer. Make sure the position you mount the encoder doesn't cause the filament to bend to much as we want it to move though the contact wheels as easily as possible. I would recommend attaching the mount with a good double sided tape (link here) or if your printer uses aluminum extrusions you can mount it with T-nuts. The wheels are press fit to the encoder shaft and snap into the other side and the cover is screwed to the mount with m3 screws. Next the flex tube is pressed into the hole in the top of the cover. Finally the wire should be neatly routed so it doesn't interfere with the print bed (see below).
Step 6: Building the Board
First I would recommend building this on a breadboard and testing that the system works prior to permanently soldering anything.
Second the rotary encoder needs to be connected to the ATtiny85 with a pull-up network (shown in image above)
We used perfboard with header pins to connect everything. An 8 pin DIP socket was used to install the ATtiny85. If you decided to do it this way VERIFY YOUR PIN TO PIN CONNECTIONS WITH A MULTI-METER BEFORE CONNECTING ANYTHING! The rotary encoder wires were routed through existing connectors on the Taz5 that had empty pins.Cables were made for all the pin connections. Finally everything was connected and a test print was done to verify everything was working together.
Step 7: Final Thoughts
This project is a work in-progress. We will be adding files for the Arduino Uno and Nano in the weeks to come as well as some better diagrams of the circuit layout to replace the white board ones. With that have fun and keep making.

Participated in the
Microcontroller Contest
8 Comments
3 years ago
A more detailed technical description, both of the ATtiny and of the way to build the software then how to load it via the IDE into the control board would help the beginner who is not au fait with the ATMEL system.
Question 3 years ago on Step 7
In chapter 7 you you promised improved layout and connection diagrams for the Atmel 85. Could I ask when these might be available? Regards. ender3pro
Question 4 years ago on Step 3
Hello again,
Sorry but I have still problems connecting the extruder signal to the atty85.
I use MKS Base v1.4 board and I cannot find the PIN': E1_STEP_PIN 36.
Is this the stepper connector E1?
Thanks in advance.
Answer 4 years ago
No actually. E1_STEP_PIN is the output for the second extruder. This output can be sent to any pin. I changed this from pin 33 to pin 23 on my board because it only outputs at 5v, In my case an unused limit switch pin. If you use the E1 output on your board then you'll probably send 12-24V to the ATtiny and fry it. You'll have to send it to an unused pin on your board. It looks like you could do the same with one of your unused x, y, or z limits or use one of the EXP1 or EXP2 pins. You would then find the E1 pin assignment for your board and change it to one of those pins.
Good luck I had to try a few pins and use an arduino to measure outputs to find the right one. My board didn't match the pinout on the schematic I had.
Question 4 years ago on Step 6
Hello,
I am using an cr 10 Printer. Were can i get the extruder signal. I think this is needed.
Regards
Uwe
Answer 4 years ago
The signal from the stepper motor is sent to both extruder 0 and extruder 1. This is done using the stepper_indirection.h tab. Search for "#define E_STEP_WRITE(v) {E0_STEP_WRITE(v)}" and change it to "#define E_STEP_WRITE(v) {E0_STEP_WRITE(v); E1_STEP_WRITE(v);}// !!! writes to E0 and E1"
I then set the E1 signal to an unused pin. In my case it was one of the unused limit switch pins. So you'll need to just assign #define E1_STEP_PIN (default value) to a known pin on your board.
Good luck I had to hunt for one and the limit switches were easy to find on my rambo board.
Reply 4 years ago
Thank You very much for Your help. Attached a picture of my Hardware Solution. I Use an extruder.
Uwe
4 years ago
Cool project. Good luck in the Microcontroller contest