Hey! This thing is still a Work in Progress. Files, instructions, and other stuff might change!

Filament Width Sensor - Prototype 2

by flipper, published

Filament Width Sensor - Prototype 2 by flipper May 15, 2013


Use This Project

Give a Shout Out

If you print this Thing and display it in public proudly give attribution by printing and displaying this tag.

Print Thing Tag


Thing Statistics

33003Views 5025Downloads Found in 3D Printing


Note: Prototype 3 is available: http://www.thingiverse.com/thing:454584

Also, I am selling a limited number of pc board kits for prototype 3 at:

This is a prototype for an optical sensor that measures the width of plastic filament in real time as it goes into a 3D printing extruder. It is prototype #2. The idea is that with a real-time width measurement the 3D printer could compensate the extruded flow for changes in filament width. This version includes a custom designed pc board as well as a housing. A version of Marlin is modified to use the sensor data.
I added some pictures of the sensor in operation. The sensor outputs a voltage in milimeters (3v=3mm) that is shown on the voltmeter. There is also a video at http://www.youtube.com/watch?v=HHjDG1jdv5o.
I made some changes to Marlin to read the filament diameter real-time and compensate the extrusion rate. Picture added.
Latest update: I made some refinements to the case design to improve the screw holes and fit of the PC board (try 3.3.stl)
This prototype sensor is now part of the design of the latest Lyman extruder. I worked with Hugh on building the controller and incorporating the sensor into the system.
I added the files for the Seeedstudio order. I also updated the schematic to reduce the parts (MCU uses internal oscilator, no external xtal required). The Codewarrior Eclipse project is also included.
8/2/14 - I added a photo of a sample print showing control on and then shut off mid-print. The filament has a variation of at least .5mm roughly every inch. This is using some new Marlin code I am working on that implements a delay to the sensor readings to handle the transit delay caused by the filament sensor being a fixed distance upstream from the extruder nozzle.


In this version #2 I improved the case so that it guides the filament close to the sensor and seals out external light. The CAD work was done in Cubify Invent (.fun file). The PC board was done in Eagle. The system uses an 8-bit Freescale SG8. I had the boards made at Seeed Studio.
I have ported the code from the previous proof of concept. It now uses an approach to get sub-pixel edge detection accuracy. The imaging sensor has roughly 16 pixels per mm. With sub-pixel accuracy, this is improved by a factor of 2-10.
I added some new pictures and a video of the sensor attached to the 3D printer, measuring the filament width. I also added a graph that shows measurements of filament and metal rods of various diameters to see how well the sensor works, with the sub-pixel algorithm.
I made some updates to the Marlin firmware on the printer so that it reads the real-time filament diameter measurement using an A-D channel on the board (Printrboard) and then optionally compensates the extrusion rate, based on calculated cross-sectional area of the filament. (https://github.com/filipmu/Marlin/tree/Filament-Sensor)
This prototype can be used with a 3D printer to control the extruder flow based on filament thickness.
The next prototype would provide a means of measuring the complete cross-sectional area. The current thought is to rotate the sensor around the filament, thereby scanning the filament at multiple angles. These measurements could then be used to calculate a cross section.
Firmware update:
The code extends one M-code and supports a couple new M-codes:
M404 - N Enter the nominal filament width (3mm, 1.75mm ) or will display nominal filament width without parameters
M405 - Turn on Filament Sensor extrusion control. Optional D to set delay in centimeters between sensor and extruder
M406 - Turn off Filament Sensor extrusion control
M407 - Displays measured filament diameter
I attached a picture showing the compensation working. The nominal filament width is 3.00, while measured is 2.82mm based on the sensor. This results in a factor difference in area of 113%, so the extruder rate is increased by 113%.
I did an experiment where I sliced an object for 4mm filament. I told the printer it was using 4mm nominal filament. The real-time measurement the filament coming through was measured at 2.82mm. Compensation kicked in and the print was successful. Next I need to get some poor quality filament with lots of variation to see if this helps in printing.
Notes on making this: You need to paint the inside of the LED cone with flat black acrylic paint to reduce reflections inside the cone. Alternatively black filament might work as well.
I added some instructions on the sensor terminals and how to calibrate: Board Instructions v3.pdf

More from 3D Printing

view more

All Apps

Auto-magically prepare your 3D models for 3D printing. A cloud based 3D models Preparing and Healing solution for 3D Printing, MakePrintable provides features for model repairing, wall thickness...

App Info Launch App

Kiri:Moto is an integrated cloud-based slicer and tool-path generator for 3D Printing, CAM / CNC and Laser cutting. *** 3D printing mode provides model slicing and GCode output using built-in...

App Info Launch App
KiriMoto Thing App

With 3D Slash, you can edit 3d models like a stonecutter. A unique interface: as fun as a building game! The perfect tool for non-designers and children to create in 3D.

App Info Launch App

when you get an idea for a new invention and its already out there

when you get an idea for a new invention and its already out there

Comments deleted.

Have a few fully assembled and programmed boards left, please contact me if interested…

mge - in reply to dreamax

Do you still have pieces left? If so, do you ship to the Israel? I Would be intrested. Thanks!

Do you still have pieces left? If so, do you ship to the netherlands? I Would be intrested.

i uploaded the zip file to seedstudio and i was told there is no board file inside.

I just tried downloading it myself and I got the message that processing was successful. Are you sure you are providing Seeedstudio the zip file?

Also, there is a new prototype 3 available, which has better terminal connections. See http://www.thingiverse.com/thing:454584

Filament Width Sensor Prototype Version 3
by flipper

Got few extra pcb if someone is intrested...

where are you located.


Maybe you can include metric calibration option with a 3 mm wire or drill bit. I thought about long press the button and double blink of the led. The 1/16 inch bits are not so easy to obtain in the metric world...

The sensor array has a length of about 8mm. Have you thought about rotating the sensor by 45° to get a higher resolution?
Could you explain the sub-pixel edge detection? Maybe this can help somebody to port it to arduino...

Sep 6, 2014 - Modified Sep 6, 2014
flipper - in reply to dreamax

Yes, 45 deg or even more would have the shadow cover more pixels and might give more resolution. I have not really thought about what the drawbacks would be going with that approach. Might be more sensitive to the angle that the filament is going into the sensor, which could change by a small amount as the filament moves. It might allow a simpler approach just thresholding the pixels (non sub-pixel), but with an mcu needed to read the sensor anyways, why not use a subpixel approach.

The subpixel approach improves apon a more basic approach. The basic approach is to essentially count the number of 'dark' pixels in the array to estimate the shadow size (proportional to the filament diameter). This requires thresholding the pixel values to come up with a definition of 'dark'. Its a little more complicated, because you want to search for an edge - the transition between a 'illuminated' pixel and a 'dark' pixel. And then search for the other edge dark to illuminated.
The subpixel approach takes the basic approach (count of the dark pixels) and adds corrections for each edge. Since the filament edge shadow does not fall exactly on a pixel boundary, it requires looking at the 3 pixels on the edge (definitely dark, grey, definitely illuminated). The algorithm uses a quadratic fit of the 3 pixel values to estimate a more precise edge. This is done for both edges, yielding a subpixel correction amount for each edge (something between 0-1 pixel width). Then add up the pixels counted in the basic approach and the subpixel amounts from each edge and you get a better width approximation.

Here is a link to the paper I used for the math: http://dev.ipol.im/~morel/Dossier_MVA_2011_Cours_Transparents_Documents/2011_Cours1_Document1_1995-devernay--a-non-maxima-suppression-method-for-edge-detection-with-sub-pixel-accuracy.pdf

Pictures in the paper do a good job showing whats happening.

Thank you flipper! This is a very interesting document, so it seems that higher resolution is not really necessary.
I think it shouldn't be a problem to rotate the whole board (in a new housing) for tests. So I ordered boards and I'll try it when I built the sensor. If anybody is interested in board and parts please PM!

Comments deleted.

I should have mentioned sooner but I am nearly complete with a new design for the board. No functional changes, only improvement is screw terminals. Board has same outline dimensions. I also improved the case to make it more sturdy around the LED. I will put the info out here shortly.

Comments deleted.

Can I get a board, programmed with components and shipped to New Zealand?

anyone willing to guide me on how to program the boards? i have soldered them and downloaded the USBDM software.
dont know what to do next...

andre.mercado (hangout maybe?) or skype?

If all you want to do is load the firmware on the mcu you can use the software that came with the programmer board (USBDM board). If you install the drivers, it installs some flash programmer software, one called HCS08 Programmer. This software lets you load the compiled firmware 'hex' file (called FilamentSensorproto2.abs.s19 in the directory called FLASH in the zipped project) into the MCU. No need for the IDE in this case.

programmed! woohoo! thanks for the help flipper.

what to do next .... ;)

I posted some instructions in the 'Thing File' section on how to calibrate it: Board Instructions v3.pdf. If you look at the Lyman extruder v5 docs theres more info there too.

So how much do you want for the 2 boards? just boards no components needed...

Comments deleted.

So you got the boards now?

yep, im trying to figure out how to program one

Few jump wires from chip to programmer and program... that I would do with something like 0.2mm^2 wire...

yeah , i found the pins at fry's and soldered them on. i cant for the life of me figure out how to use the USBDM board though. installed the software for it and now im at a "what next" moment

Aug 15, 2014 - Modified Aug 15, 2014
flipper - in reply to Pixeldesire

The board has a place to solder on a 6 pin header that is used for programming.Make sure pin 1 on the board matches pin 1 on the USBDM board when you plug it in.

I just had 10 of these etched for me by seed studios as my attempts to diy etch at home were not too successful. Assuming they prove viable, I will only need one and gladly let the others go at cost. I would also be glad to solder everything on and upload code for a small fee (if my first ones work).

How much for a complete board?

just put in an order for 9 boards to be done and parts coming from digikey/mouser...

How did yours come out? I am embarrassed to say that out of 6 attempts...1 actually worked. Apparently I need to either enhance my smd skills or develop a more delicate touch with the soldering iron. If anyone is interested, I have 5 untouched boards left for you to mangle or complete...

The soldering paste worked quite well and all the components soldered on nicely. I do have to wait till the programming board comes in so I can test it out.

I found I had about a 90% success rate with the boards from a simple 'connectivity trace' perspective, but only about 25% success in uploading the code to boards.

Could buy 2 from you if you are willing to sell just the boards...

yeah for sure, i have all the components for the build too. ill let you know when i get the boards in.

Could I buy one complete and working, can you let me know?

Once I get all the components in I'll let you know.

So did you get the boards?

I'm I little embarrassed to say but I had them made for the Lyman side of things :|
I'm building the Lyman extruder v5.
The board is slightly different, don't know if it would work for this separate module.
Sorry guys.

I think I have two board designs floating around. Either will work for either application. One does not have a crystal, because I found the internal osc. to work more reliably. The firmware code does not use the crystal. The big difference is the 3D printed case. The one on Hugh's site is for the filament extruder and the one here is for a 3D printer. Board dimensions are exactly the same so either will fit in either case.

both are interchangeable?

How much you want for the boards?

What coincidence.... I'm building too the Lyman extruder V5 with slight modifications ... so the board should be perfect

I am building a Lyman Extruder V5 too, so I'm still interested.

Comments deleted.

seeedstudio requires a Gerber file in either zip or rar format. I was wondering if you could possibly post these?

I attached the files needed for Seeedstudio order.

Are those 2 or 4 layer files?


Hello Flipper! So I am trying to build one of these, and am a little confused one some points.
1) Which pins should I connect to for output
2) Which pins need I connect to in order to program the microcontroller?
3)Is there a specif USB to pin connector to use or will any usb to serial debugger work?
4) Is there a specific app/workspace for programming the microcontroller?

See my response to bitouns, he has the same questions. I like the freescale microcontrollers, but they are not for everyone. I have been using them before arduino came along, so I am all set up for them, and I like the in-circuit debugging. I am thinking of a few quick options for getting these in people's hands. One option is I can flash the controllers, and make the boards, but people would have to populate the surface-mount components, which is not impossible - see tutorials at sparkfun using an electric skillet, soldering paste, and tweezers (what I do).
Another option is to port the code to arduino/atmel (arduino micro or nano) and then use an off the shelf board with the Tsl1401 sensor (http://www.thanksbuyer.com/tsl1401-module-ccd-sensor-tsl1401cl-integrated-operational-camera-lens-for-smart-car-27966)http://www.thanksbuyer.com/tsl... . There is arduino code online to read these sensors. Any volunteers to try this? I think its probably easier (and more fun) than learning the Freescale HCS08 toolchain.
I am also looking at other options for making these, but these might be the best short-term options.

as far I know, arduinos don't have a DAC.. so another data transmission would be needed

It's not really a problem, use i2c if fell like like. Nano reads the damn thin 30fps no problem, displays image on crius 128x64 mono LCD nicely.

I could buy one set of those components + board no soldering needed... that I can do by myself...
And for the Tsl1401 sensor and arduino if we do that kind of modification could we use the same Atmega 2560 what we are using on the Hlyman filament extruder v5? We could just add the code to marlin firmware. The code shouldn't be big problem if you have base ready for the use...

The TSL401 is not happy with long wires in a noisy environment. Nano or even tiny, and is more common device. Other words 'Arduino 2560 with Marlin can nicely do the job, if wires where not needed to TSL401'.

I think it would be challenging to use the same 2560 with Marlin, since the stepper control uses interrupts at up to 5000khz rate. The TSL1401 exposure depends on accurate timing during the read. I think the interrupts will mess with that. I think a separate MCU is required. DAC is not required - current design uses a PWM output which arduinos have.

Hmm... I just looked few things up and wouldn't this kind of approach be easier... There is the code how the sensor needs the interrupts so if we synchronize it with the other interrupts it might work?
With direct arduino support... adjustable to different wavelengths so lightsource can be pretty much anything...

That sensor is a different sensor - its not a linear array of 128 pixels.

Right it's useless for this job it's even too small - sorry! Still working on "3D" filament
measurement with a Arduino nano for bowden feed. Nice and slow work!

why would you want to use an atmega 2560? it's not faster than the one found in an arduino pro mini but much bigger and more expensive.

Cause Atmega 2560 is pretty much basic on reprap as controller and most of repraps ain't even taking off the full potential off the controller. So rather then having xxx number of different controllers. It would be much simpler just to have one main controller where everything would be hooked up.
flipper one way could be that we use the original design but simplify it by making it little bit bigger with normal components (forget the surface components) and look for more common controller chip to it.

hi flipper, i was looking for a filament sensor for a delta printer. Something that could monitor the speed/flow as well as the width and adjust the extruder accordingly. Maybe control the extruder itself? Any ideas? Oh and, do you have a blog?

hi flipper
could you remplace the microcontroller code by a simple arduino uno code because it's very difficult to find and buy the mc9s08sg8ctg
it will be very nice ?

I've thought of trying to put together a version using a small arduino board and a board with the tsl1401 sensor like this.
It would be easy to interface the two and can use the image sensor directly - just take off the lens assembly.
Programming is going to take some work, since the communication timing with the image sensor is critical and so I would need to learn how Atmel interrupts work.

Hello flipper, how do you program the microcontroller? Thank you!

Hello flipper, where can I find this MA02-1 Led ?

I uploaded a BOM with Mouser part numbers for all the electronic parts.

Oh nice ! Thank you ! I still have an other problem : How did you flash
the 8-bit Freescale SG8 ? Do you know a tutorial or a biginner guide for
that kind of Microcontroler ?

Did you ever figure this out? I have built the board, but am having a hell of a time actually building in the debugger. I don't see the general.h file or the main_asm.h file and am unsure how to build them. It also indicates that an application file was not specified in launch configuration. Is there a good tutorial for how to upload the main.c to the microcontroller?

I recently posted the complete zipped project for the freescale IDE ('Freescale 8 bit code...). You should be able to open it in the dev tool. Its a little tricky to configure the debugger, especially with the programmer. If all you want to do is load the firmware on the mcu there is an easier way. You can use the software that came with the programmer board (USBDM board) in the link I provided below. If you install the drivers, it installs some flash programmer software, one called HCS08 Programmer. This software lets you load the compiled firmware 'hex' file (called FilamentSensorproto2.abs.s19 in the directory called FLASH in the zipped project) into the MCU. No need for the IDE in this case.

Here is a programmer board for the 8 bit freescale microcontrollers. http://www.evbplus.com/freescale_usbdm_osbdm/usbdm_osbdm_bdm_multilink.htmlhttp://www.evbplus.com/freesca...
You can download the dev tools for free from freescale: http://www.freescale.com/webapp/sps/site/overview.jsp?code=CW_SPECIALEDITIONShttp://www.freescale.com/webap... - look for the one for microcontrollers, eclipse version.

I would e interested in a board. [email protected] Thanks

Sorry, I don't have any more boards at the moment. I've been busy working on a related project (filament extruder controller) so have not had a chance to make any prototypes of the filament sensor.

got all the parts a few days ago via fedex... Now i just need the boards....
could you at some point tell how to mod an excisting marlin setup? i got mine perfect tuned for my mendel90 and would like to keep changes to an absolute minimum
also let me know if you get time to make boards for sale...

I wonder if you at some point will do kits? I have been looking at the filament extruder by H. Lyman and would like to build it. And know thinking at it i would like 2 boards so i can fit one to the printer too.

To those that order this have LOTS of patience... the freescale controller is on backorder until october

Mouser has the S9S08SG8E2VTG in stock which is the equivalent microcontroller and package (it has expanded automotive temperature range.) I'm working on an approach to make more of these.

nice, mouser agreed to replace the MCU... I wonder if the filament extruder will ever be a kit... could be damm nice if so... :-D

oki doki :-D will mail them and ask to replace the MCU

The microcontroller is also available from digikey as of 2 hours ago...

What's the state of this sensor? are you still using it? does it work reliably?

Yes, I am still using it and working with it. It is on my 3D printer and I use it all the time there. I have had only two issues with reliability. In one case some dust must have got on the image sensor and I got strange readings (0 mm when filament in the sensor). In another case a large piece of plastic (size of a BB) got into the path of the light and readings did not stay constant.

Thanks for that reply.
I really like this approach, the only "problem" I have with this one is that my printer has a bowden setup. So I have 0.5m tube between measuring and extruding. I have filament which is really inconsistent in diameter (1.6mm up to 1.7mm in a few cm filament).
Do you have an idea how to implement that length offset into your code?

That will take alot of memory too make such a ringbuffer depending on how accurate you want it too be. Every mm cm or 10cm, you will have too extrude 0.5m in your setup before you can count on it. But I'm in for it, but with an i2c extra memory.

an idea... release the bowden tube at the extruder end and inject this on in the path, of course some sorts of bracket are needed... at least its food for thought

B0rax - in reply to boelle

i don't know.. the extruder grape into the filament, wouldn't this make the sensor reading unusable?

Right just another box for the circuit board or make a new extruder with build in TSL40 that would be nice

I am also interested in the boards. If you still have them, you can contact me in my email [email protected] Thank you very much.

I'm interested in one of those boards as well. Contact me if you have one laying around. [email protected] Thanks in advance.

I am interested in buying this sensor.
could you please contact me with price& ship to France
[email protected]

Send me your mail address and I will send you some filament from the Lyman Filament Extruder V4.1 for testing.
[email protected]

I am also interested in buying a complete sensor or board.
Please contact me with price & ship to Poland.
My e-mail :[email protected]

Hey, geat Gadget! If you have a board left, email me the price, please! julius at schleibinger dot com. Thank you very much!

Does this have a delay? Like does it compensate for the distance between the sensor and the extruder?

How many boards do you have left, and how much do you want for one? Email me. Datasmasher at gmail dot com

Add me in buying a board too [email protected]

I am also interested in buying a board. Please contact me with price etc
[email protected]

Yes, I will be in touch by email.

I'm interested in buying a board if you have any available? Shoot me an email - [email protected]

Nice work! I have an industrial laser width/opacity sensor which do not have imaging pixels -- which makes it challenging to use with clear/translucent filament. Since you can actually image the filament, it should be far easier for you to get good accurate results. Looking forward to this being completed!

It will be worth a try to see if it works with translucent filament. It may not be able to detect the edge. A sensor like this might be useful to rate different filament vendors. You could scan a whole spool and trend the variation.

It's one of my longer term goals is to build an automated filament testing station for the filament we carry at http://toybuilderlabs.comtoybuilderlabs.com

Could this be a distance away from the extruder so in the event of the filament being too far out of spec i.e too fat to actually go through the extruder then the print could be automatically stopped?

Yes, that is possible, to stop extrusion either when the filament is no longer there or hits a certain threshold.

This is really cool, care to go into more detail about how the sensing works? What is that device you are using to detect the filament's shadow? Does it work with transparent filaments?

The posting for the first prototype gives a good description: http://www.thingiverse.com/thing:70775http://www.thingiverse.com/thi.... It uses a linear array sensor
TSL1401CL. Transparent filaments may be a challenge, but I have not tried if there is enough contrast to detect the edge.

Filament Width Sensor
by flipper

how does the average filament deformation look like? I thought things go elliptic as well?

Yes, this current design only measures the diameter along one direction, so would not detect an elliptic filament. One solution I plan to try is to scan the sensor by rotating it 180 deg while taking measurements. A simple RC servo could be used to control the position. Then the set of diameter measurements could be combined together to provide a better representation of the cross-sectional area. Scanning could occur every couple seconds, so would not be too taxing on a servo. I could use multiple sensors, but the servo approach seems simpler and would give better data.

Does the guide hold the filament against the sensor, or is there some wiggle room? It wouldn't take much movement toward or away from the sensor to change the shadow size by a few .01mm, would it?

There is some wiggle room, maybe .4 mm. The illuminating LED is about 45mm away, so a .4 mm movement causes roughly a 1% change in shadow size (45-.4)/45, so +.03mm deviation. One way to reduce this is to move the LED even further away, or have some sort of spring-loaded roller that holds the filament in place. In practice I have not seen much variation when I wiggle the filament in the sensor. I wonder if moving the filament away from the sensor also diffuses the shadow edges, thereby effectively compensating for an increase in shadow size.

Nice work! I've been thinking about pressure/force sensors, which however may be more complicated to mount...

Thanks, Its working now, including interfacing with Marlin code. I am going to run it a bit on my printer. Unfortunately I don't have any really bad filament (with lots of variation) to try it on.