<p>The Marlin firmware is ready for connecting such a filament sensor. You need to adjust the configuration (<a href="https://github.com/MarlinFirmware/Marlin/blob/bugfix-2.1.x/Marlin/Configuration_adv.h" rel="nofollow noreferrer">Configuration_adv.h</a>) to enable the <code>FILAMENT_WIDTH_SENSOR</code> definition (remove the <code>//</code> before <code>#define FILAMENT_WIDTH_SENSOR</code>):
<a href="https://i.sstatic.net/Z6DQp.png" rel="nofollow noreferrer" title="Snippet of Configuration_adv.h"><img src="https://i.sstatic.net/Z6DQp.png" alt="enter image description here" title="Snippet of Configuration_adv.h" /></a></p>
<p>The distance from measurement to melting chamber should and is taken into account.</p>
<blockquote>
<p>shouldn't the firmware know the distance between the nozzle and the filament width sensor to know when to compensate</p>
</blockquote>
<p>The firmware does do that. You can either set the distance inside the firmware compile-time (which is pretty safe as the distance doesn't change much unless you have a Bowden setup where you exchange the tube for a different length tube or use a tool changer/multiple tool heads with different tube lengths) or through G-code:</p>
<p><a href="https://marlinfw.org/docs/gcode/M405.html" rel="nofollow noreferrer"><code>M405 D&lt;cm&gt;</code></a> specifies that distance through G-code, otherwise the firmware set value is used:</p>
<p><a href="https://i.sstatic.net/2nxq7.png" rel="nofollow noreferrer" title="M405 G-code usage"><img src="https://i.sstatic.net/2nxq7.png" alt="enter image description here" title="M405 G-code usage" /></a></p>

CC BY-SA 4.0
2023-12-06T13:35:53.063Z
2023-12-06T13:43:56.537Z
2023-12-06T13:43:56.537Z
0scar
<p>I found this great DIY '<strong>filament diameter sensor</strong>' or '<strong>filament width sensor</strong>'. It is called <strong>In</strong>line <strong>Fi</strong>lament <strong>D</strong>iameter <strong>E</strong>stimator <strong>L</strong>owcost (InFiDEL), as shown below. The <a href="https://github.com/drspangle/infidel-sensor/" rel="nofollow noreferrer">GitHub project</a> states:</p>
<blockquote>
<p>A cheap, yet precise filament diameter sensor, intended to compensate for filament diameter deviations in real-time.</p>
</blockquote>
<p><a href="https://i.sstatic.net/VRSc6.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/VRSc6.png" alt="InFiDEL filament diameter/width sensor" /></a></p>
<p><sup>Model source: <a href="https://www.printables.com/model/57154-infiDANK" rel="nofollow noreferrer">Printables.com</a> or <a href="https://www.youmagine.com/designs/infidel-inline-filament-diameter-estimator-lowcost-10-24" rel="nofollow noreferrer">Youmagine.com</a></sup></p>
<p>It can essentially be used for flow control. In a <a href="https://youtu.be/RYgdLPe_T0c?si=wqA6Yi6hHtA5MKFk&amp;t=32" rel="nofollow noreferrer">YouTube video</a> the <a href="https://www.youtube.com/@MadeWithLayers" rel="nofollow noreferrer">maker (Thomas Sanladerer)</a> says:</p>
<blockquote>
<p>... the firmware can then compensate for any differences in filament diameter and filament cross-sectional area essentially by adjusting the extrusion multiplier ...</p>
</blockquote>
<p><a href="https://youtu.be/RYgdLPe_T0c?si=Wa5b6zswdaoeG61r&amp;t=443" rel="nofollow noreferrer">The video</a> also explains how three different sized drill bits are used to calibrate the measurement.</p>
<p>To my understanding, it uses a principle where a hall effect sensor measures magnetic strength that is translated into a voltage that reflects the diameter, such as 1.78 Volt for 1.78 mm. I plan to implement something like this, however, I don't know how Marlin implements this and I'd like to understand it first. The Marlin documentation does contain several G-codes for a filament width sensor, such as <a href="https://marlinfw.org/docs/gcode/M405.html" rel="nofollow noreferrer">M405</a>, <a href="https://marlinfw.org/docs/gcode/M406.html" rel="nofollow noreferrer">M406</a>, and <a href="https://marlinfw.org/docs/gcode/M407.html" rel="nofollow noreferrer">M407</a>, but I cannot find anything about how Marlin implements this. Also, shouldn't the firmware know the distance between the nozzle and the filament width sensor to know when to compensate? The distance seems to be not implemented by Marlin entirely.</p>
<p><em>Lastly, I think that this sensor could also serve as a filament runout sensor by noting that the diameter is simply close to 0 mm.</em></p>

CC BY-SA 4.0
2023-12-06T10:49:29.370Z
P.S. as you buy (bought recently 2 spools) premium quality filament from a Dutch OEM, you don't need one! 
0scar
<filament-sensor><sensors>
How does Marlin firmware use filament width sensor data for flow control?
Bob Ortiz
# How does Marlin firmware use filament width sensor data for flow control?

(Asked by: username_0 on 2023-12-06)

I found this great DIY '**filament diameter sensor**' or '**filament width sensor**'. It is called **In**line **Fi**lament **D**iameter **E**stimator **L**owcost (InFiDEL), as shown below. The [GitHub project](https://github.com/drspangle/infidel-sensor/) states:

> A cheap, yet precise filament diameter sensor, intended to compensate for filament diameter deviations in real-time.

[![InFiDEL filament diameter/width sensor](https://i.sstatic.net/VRSc6.png)](https://i.sstatic.net/VRSc6.png)

Model source: [Printables.com](https://www.printables.com/model/57154-infiDANK) or [Youmagine.com](https://www.youmagine.com/designs/infidel-inline-filament-diameter-estimator-lowcost-10-24)

It can essentially be used for flow control. In a [YouTube video](https://youtu.be/RYgdLPe_T0c?si=wqA6Yi6hHtA5MKFk&t=32) the [maker (Thomas Sanladerer)](https://www.youtube.com/@MadeWithLayers) says:

> ... the firmware can then compensate for any differences in filament diameter and filament cross-sectional area essentially by adjusting the extrusion multiplier ...

[The video](https://youtu.be/RYgdLPe_T0c?si=Wa5b6zswdaoeG61r&t=443) also explains how three different sized drill bits are used to calibrate the measurement.

To my understanding, it uses a principle where a hall effect sensor measures magnetic strength that is translated into a voltage that reflects the diameter, such as 1.78 Volt for 1.78 mm. I plan to implement something like this, however, I don't know how Marlin implements this and I'd like to understand it first. The Marlin documentation does contain several G-codes for a filament width sensor, such as [M405](https://marlinfw.org/docs/gcode/M405.html), [M406](https://marlinfw.org/docs/gcode/M406.html), and [M407](https://marlinfw.org/docs/gcode/M407.html), but I cannot find anything about how Marlin implements this. Also, shouldn't the firmware know the distance between the nozzle and the filament width sensor to know when to compensate? The distance seems to be not implemented by Marlin entirely.

*Lastly, I think that this sensor could also serve as a filament runout sensor by noting that the diameter is simply close to 0 mm.*

**Tags:** filament-sensor, sensors

> **Comments:**
> **username_1**: P.S. as you buy (bought recently 2 spools) premium quality filament from a Dutch OEM, you don't need one!   

---

## 1 answers

### Answer by username_1 on 2023-12-06. Score: 1 | Accepted answer

The Marlin firmware is ready for connecting such a filament sensor. You need to adjust the configuration ([Configuration\_adv.h](https://github.com/MarlinFirmware/Marlin/blob/bugfix-2.1.x/Marlin/Configuration_adv.h)) to enable the `FILAMENT_WIDTH_SENSOR` definition (remove the `//` before `#define FILAMENT_WIDTH_SENSOR`):
[![enter image description here](https://i.sstatic.net/Z6DQp.png "Snippet of Configuration_adv.h")](https://i.sstatic.net/Z6DQp.png "Snippet of Configuration_adv.h")

The distance from measurement to melting chamber should and is taken into account.

> shouldn't the firmware know the distance between the nozzle and the filament width sensor to know when to compensate

The firmware does do that. You can either set the distance inside the firmware compile-time (which is pretty safe as the distance doesn't change much unless you have a Bowden setup where you exchange the tube for a different length tube or use a tool changer/multiple tool heads with different tube lengths) or through G-code:

[`M405 D<cm>`](https://marlinfw.org/docs/gcode/M405.html) specifies that distance through G-code, otherwise the firmware set value is used:

[![enter image description here](https://i.sstatic.net/2nxq7.png "M405 G-code usage")](https://i.sstatic.net/2nxq7.png "M405 G-code usage")


---

