# Optical Filament Diameter Sensor Doodles

It should be possible to sense the filament diameter with a cheap webcam and some optics:

The general idea:

Given that LinuxCNC runs on a bone-stock PC, you can plug in a stock USB webcam and capture pictures (I have done this already). Because LinuxCNC isolates the motion control in a hard real time process, you can run heavy metal image manipulation code in userland (think ImageMagick) without affecting the motors.

So you can put a macro lens in front of a webcam (like that macro lens holder) and mount it just above the extruder with suitable lighting to give a high-contrast view of the filament. Set it so the filament diameter maps to about 1/4 of the width of the image, for reasons explained below.

For a crappy camera with 640×480 resolution, this gives you 160 pixel / 1.75 mm filament = 91 pixel/mm → about 0.01 mm resolution = 0.6%. Use a better camera, get better resolution: 1280 pixel = 0.3% resolution.

That gives you roughly 1% or 0.5% resolution in area. This is pretty close to the holy grail for DIY filament diameter measurement.

Add two first-surface mirrors / prisms aligned at right angles, so that the camera sees three views of the filament: straight on, plus two views at right angles, adjacent to the main view. Set the optics so they’re all about 1/4 of the image width, to produce an image with three parts filament and one part high-contrast background separating them. This is the ideal, reality will be messier.

Figure 1 shows an obvious arrangement, the mirrors in Figure 2 give more equal distances.

You could align the mirrors to provide three views at mutual 120° angles, which would equalize the distances and give you three identical angles for roundness computation, should that matter.

Diameter measurement process:

• Extract one (*) scan line across the image.
• Convert to binary pixels: 1 = filament, 0 = background, perhaps with ImageMagick auto thresholding.
• Add pixel values across the line, divide by 3, multiply by mm/pixel → average filament diameter.
• Done!

Adding binary pixels is easy: it’s just the histogram, which ImageMagick does in one step. Dump data to a file / pipe, process it with Python. It all feeds into a LinuxCNC HAL component, which may constrain the language to C / Python / something else.

(*) You can get vertical averaging over a known filament length, essentially for free. Extract three (or more) scan lines, process as above, divide by 3 (or more), and you get a nicely averaged average.

Win: the image is insensitive to position / motion / vibration within reasonable limits, because you’re doing the counting on pixel values, not filament position. The camera can mount near, but not on, the extruder, so you can measure the filament just above the drive motor without cooking the optics or vibrating the camera to death.

Win: it’s non-contacting, so there’s not much to get dirty

Win: you get multiple simultaneous diameter measurements around one slice of the filament

You could mount the camera + optics at one end of the printer’s axis (on the M2, the X axis). Drive the extruder to a known X position, take a picture of the straight-on view, drive to another position, take a picture of the mirrored views, and you have two pictures in perfect focus. Combine & process as above.

You can do that every now and again, because any reasonable filament won’t vary that much over a few tens of millimeters. Maybe you do it once per layer, as part of the Z step process?

You could generalize this to a filament QC instrument that isn’t on the printer itself: stream the filament from spool to spool while measuring it every 10 mm, report the statistics. That measurement could run without stopping, because you don’t reposition the filament between measurements: it’s all fixed-focus against a known background. You could have decent roller guides for the filament to ensure it’s in a known position.

Heck, that instrument could produce a huge calibration file that gives diameter / roundness vs. position along the entire length of the filament. Use it to accept/reject incoming plastic supplies or, even better, feed the data into the printer along with the spool to calibrate the extrusion on the fly without fancy optics or measurements.

Dan wonders if this might be patented. I’m sure it is: I’m nowhere near as bright as the average engineering bear at a company that’s been spending Real Money for three decades. My working assumption: all the knowledge is out there, behind a barrier I can’t see through or reach around: there’s no point in looking for it beyond a casual Google search on the obvious terms that, so far, hasn’t produced anything similar.

Memo to Self: Might even be marketable, right up until they crush me like a bug…

## 13 thoughts on “Optical Filament Diameter Sensor Doodles”

1. Red County Pete says:

The method might be patentable, though using video to measure stuff is common practice. (With the loose patent rules nowadays, lord knows what’s been patented.)

At HP, the III-V LED fab used a video setup to measure area of stuff processed. Unlike silicon, where you really want/need to have whole wafers (and it’s medium easy to keep from breaking them most of the way), the LED material would break with a harsh look. At least in the ’70s and ’80s, they were expert at processing tiny slivers of stuff because they had to.

Their system may have been analog–contrast was set high with a small light table, so you could total the white signal and apply the fudge factor to get the area. It ended up digital, as I recall–some kind of display counted out square inches(?). We tended to mix units a lot in semiconductors…

1. Ed says:

lord knows what’s been patented

Ahoy! Submarine patent off the starboard extruder!

At first, I thought you were trying to measure the diameter of the filament as extruded, which would be very useful knowledge, but a much harder problem to solve.

1. Ed says:

As nearly as I can tell, the extruded filament doesn’t have a diameter: ideally, it’s a squished rectangularish blob. I’m not even sure it has a width until it’s cooled off enough to touch…

3. c0derage says:

I was working on a similar idea about a year ago. Don’t have much time to work with the printer any more so it is doubtful I will ever get to it. Using a TSL1402R 256 pixel linear photo array and an led (or line laser to be fancy) you can cast a shadow of the filament onto the photo sensor. By adjusting the distance between the filament, light source, and sensor the shadow can be effectively scaled to the breadth of the photo sensor. My old notes say that if it was calibrated for a maximum reading of 2.5mm the resolution would be around 0.01mm per pixel. If greater resolution is required then two sensor can be placed adjacent to each other. It is pretty simple and cheap. The goal was to make it something an arduino based controller could interface with and not require intensive processing.
For multiple profiles you could just stack a few of these together at 120 degree angles as an example.

Here is an old rendering for the concept. s22.postimg(dot)org/vnsmsdlgx/filament_sensor.png (Wont allow me to post a link)

[Ed: here it is…]

If any one has the desire and ambition to pursue it, go nuts.

-Jason Vreeland

4. Brent Crosby says:

I thought of using a spring loaded ball chain, and a linear position sensor. Or three bearings, two spring loaded, with a position sensor.

In real life though . . . the 1.75mm filament I am using from MakerGear lately seems to be completely consistent, even across reels of different colors. So maybe it is a solution whose problem has gone away.

1. c0derage says:

Sounds interesting. I wonder how well a set of rollers, one fixed and the other spring loaded / cantilevered against a pressure sensor. Determining the width is just a matter of applying the springs k factor and mechanical advantage to the pressure transducers output. Absolute fixed alignment wouldn’t be as critical compared to an optical approach.

Regarding MakerGear’s filament. Wouldn’t be surprised if they have been sourcing it from Stratasys given the recent merger. Going to be interesting to see how that evolves.

1. Ed says:

k factor and mechanical advantage to the pressure transducer

Plenty of mechanical fiddling involved in getting that right, but it seems reasonable. Doing it for several cross-sections might be tricky, though.

Regarding MakerGear’s filament. Wouldn’t be surprised if they have been sourcing it from Stratasys given the recent merger

Makerbot got Borged. Makergear is still running under their own power.

All the good names have been used up, repeatedly…

1. c0derage says:

Doh! You are absolutely right. Been out of the game long enough to get the two confused. Good people at Maker Gear, didn’t mean to mix them up with the likes of MakerBot. Thanks for pointing it out Ed.

2. Ed says:

a solution whose problem has gone away.

I certainly hope that’s the case! The various spools around here differ by a bit, although not enough to make much difference (AFAICT, anyway).

Actually building a filament measurement station is a long way down the road, but now I can toss that scrap of paper… [grin]

5. Kamil says:

Hi Ed, been reading your blog for some time as part of my research prior to getting my first 3D printer. I ended up getting the MendelMax 2, but the M2 was a serious contender.

More to the point:
I came across this Filament Width Sensor on Thingiverse the other day and I reminded me of this project.
http://www.thingiverse.com/thing:89044

It’s very similar to c0drage’s solution.

1. Ed says:

Filament Width Sensor on Thingiverse

That’s a very nice gadget… the direct readout of 1 mm/V is great!

Version 2 and the volumetric tweaks in Marlin look like a winner.