MightyVariableFan
Variable fan speed workaround for 3D printers based on the MightyBoard, like the FlashForge Creator Pro
by Alexander Thomas, aka Dr. Lex
Version 1.1

Screenshots of web UI on a smartwatch (view larger image) 🔎
What is it?
This adds full variable print cooling fan speed capability to 3D printers based on the MightyBoard design like the FlashForge Creator Pro, by means of a kind of modem connection between the printer and a Raspberry Pi.
To make this work, you need some minimal understanding of how to use Linux on a Raspberry Pi and at least basic electronics skills. Being able to solder might not be essential but will make it much easier than trying to find ready-made cables that will fit. While working on the internals of your printer, take the usual precautions for working on delicate electronics: avoid static discharges and short-circuits.
This has so far only been tested with PrusaSlicer (formerly Slic3r) and (albeit not thoroughly) Simplify3D. Quite likely the post-processing script that generates the beep sequences will at this moment only work with G-code generated by those slicers. Of course the main reason why I have published this on GitHub is to make it easy for anyone to modify the code and submit pull requests to make it work with other slicer programs.
I have been using this for a year without problems, hence I have created a 1.0 release. There are some minor problems left and some things I would like to improve upon, but I consider the 1.0 release (and newer) very usable. See the Current Issues section at the bottom for caveats.
This has only been tested with prints from the SD card, because I don't print through USB. I don't expect problems with USB prints though.
Motivation
The MightyBoard has one annoying limitation, being lack of PWM ability for the cooling fan. The fan is connected to the ‘EXTRA’ output on the board and this output is only a binary toggle, meaning the fan can only be either at full throttle or off. This makes it impossible to get optimal results with filaments that require a small or moderate amount of cooling, and there are many other reasons why one would want to vary the speed of the fan. Programs like Slic3r offer advanced fan speed control but it is worthless with only a binary toggle. What we need is a way to control the fan speed from within G-code, and preferably there should be a manual override as well.
It is possible to recompile the Sailfish firmware with a software PWM implementation on the EXTRA output, but it is only a static setting that cannot be changed mid-print and it still only reacts to on/off commands without a speed value. A simple manual hardware PWM controller is probably a better option because it will at least allow to change the PWM at any time, but it remains cumbersome. I have done this for a few months until I got tired of having to babysit every print. I wanted fully automatic fan control from within the X3G file itself.
Recently a pull request has been proposed for Sailfish to make the software PWM respond to speed arguments in the G-code, but this still has problems: the commands are still executed with horribly bad timing and there is no manual override (even if it would be added, manipulating it mid-print via the LCD menu is very kludgy). Also, GPX needs to be patched and recompiled to output X3G files with fan speed values included, and this will only work if you have somehow managed to have your slicer generate G-code that is suitable for Sailfish and includes variable fan speeds.
The ideal way to tackle this problem is to completely decouple fan speed handling from the firmware. The printer's job is to print, not to accurately control fans. The printer should send fan speed commands to a separate device that does the actual fan PWM in a smart way. I found no straightforward way to send commands from within a print to an external device however. Most infuriatingly, the MightyBoard has a second UART1 serial interface that was originally intended exactly to send commands to external devices, but this feature was dropped for reasons unknown. At best the UART1 port can now be used as an alternative to the regular USB serial port. The ideal solution would be to restore the ability to send arbitrary serial commands through UART1 but this would require extensive changes to the Sailfish firmware code, introducing new G-code commands that would need to be supported by GPX as well. This was the last thing I wanted to try, so I looked for alternative solutions first and I found one even if it isn't the most straightforward.
From experiments, the buzzer has proven to be a reliable one-way communication channel. Not only does it play tones with accurate timings, it also plays them exactly where they occur inside the print code, unlike the M126 and M127 commands that toggle the EXTRA output at a rather unpredictable time before the command is expected to be executed. I did some experiments and the concept of using the buzzer as a communication channel proved very viable. So in a nutshell, I have used the buzzer to set up a modem connection to the external fan controller.
How It Works
- A post-processing script called pwm_postprocessor.py on your local computer takes G-code with M106 fan speed commands as input, and outputs the same code with those commands replaced by M300 beep commands. These play very specific sequences of 3 high-pitched blips using 4 fixed frequencies. This allows to encode 64 levels, which is plenty for controlling a fan. This script also manipulates the speeds to optimize them (see ‘advantages’ below), and can shift the speed changes to obtain very accurate timings. For this to work, you need to ensure your slicer program generates G-code that contains fan speed commands.
- A Raspberry Pi mounted inside the printer runs a Python daemon called beepdetect.py that continuously listens to audio input from a simple USB sound card. A microphone attached to this card is placed directly above the printer's buzzer. The script uses an FFT to detect the specific frequencies of the blips.
- When beepdetect.py detects a sequence, it performs an HTTP call to a simple Python-based web server that also runs on the Pi, this is the pwm_server.py script based on CherryPy.
- The pwm_server reacts to incoming calls that request a speed change, by manipulating PWM output on a GPIO pin of the Pi. This is done through the RPi.GPIO Python module (which only implements software PWM, but this is good enough for controlling a fan at low frequency).
- The GPIO pin controls a simple MOSFET break-out board that switches the 24V of the main printer PSU. The fan is connected to this MOSFET's output.
Advantages
This approach, crazy as it may seem, has quite a few advantages:
- No irreversible modifications to your printer are needed. Although you may need to construct the cables for the microphone and GPIO, you do not need to solder anything on the printer itself. The only requirements are glueing in some 3D printed mounts for the Pi and MOSFET, re-wiring the fan, and mounting a power supply for the Pi. These can all fit inside the spacious insides of an FFCP.
- You do not need to recompile anything. If you would have attempted to do all this stuff inside a heavily hacked Sailfish build, next to that build itself you would also have needed to hack and rebuild the GPX program and possibly your slicer, because otherwise you would have no way of getting the fan speed arguments in an X3G file.
- Decoupling the PWM controller from the printer's firmware allows to add advanced features without having to modify the printer or recompiling firmware. For instance, the PWM server controller has a kickstart feature to allow the fan to start at very low speeds and to more quickly reach intermediate speeds.
- The post-processing script can optimize fan speeds and timings. It can ensure the fan will have spun up at the right time, and it can gradually ramp up fan speed between the first layers and higher ones. The latter is very important with most cooling duct designs because at lower layers the cooling may otherwise be excessive due to air being forced in between the platform and the extruders, as well as the material cooling faster due to heat being absorbed by the build platform.
- Timing of fan speed changes can be very accurate, down to the level of one second. This allows to cool only sharp overhangs or bridges without risk of compromising layer adhesion elsewhere in the print.
- If your Pi is connected to a WiFi network, the fan PWM server can be reached through any portable device like your smartphone, and you can monitor and manipulate fan speed at any time without having to fiddle with the printer's LCD menu.
- The PWM server offers temporary or permanent manual override of fan behavior, useful for experimenting or in case you notice you have misconfigured the cooling for a print that would be a pity to abort. A scale factor can be applied to all incoming requests or the fan can be forced to a fixed speed.
- Fan duty cycles as low as 1% can be used even from standstill, thanks to the kickstart.
- If you already have installed a Raspberry Pi in your printer to have a camera feed or control other parts of the printer, you only need to install a USB sound card with microphone, an extra MOSFET, and some wires. (See the PAQ below though.)
- The EXTRA output on your printer becomes available for something else, should you find a use for it.
- Due to the modular design, if someone finds a less crazy way of making the printer communicate with the Pi, only the beepdetect and pwm_postprocessor scripts need to be updated.
Disadvantages
- There is only one obvious disadvantage: the printer will be making some extra noises as if R2D2 is trying to subtly get your attention. These blips are very short though, and are played at frequencies the buzzer cannot play loud anyway. Thanks to these two design choices, the blips don't really stand out above the usual printer noises.
- A less likely problem is that if your printer is placed next to a pen with pigs that can squeal extremely loud at the exact same frequencies as the blips, detection might fail. Please don't combine 3D printing with pigs. More realistic sources of disturbances are nearby machines that make loud hissing noises, or a loud music system very near to the printer. In my setup however, I couldn't even trigger any false responses with the detector in debug mode without making unreasonably loud noises.
If you would suffer from one or both of these problems, they can be reduced or eliminated in several ways. The first way is to print a sealed adapter for attaching the microphone to the buzzer instead of a half-open one. This both attenuates the blips to a nearly inaudible level and reduces influence of external noises.
If you're not afraid of soldering on your printer's main board, you can omit the microphone and make a direct electrical connection between the buzzer contacts (marked ‘BUZZ BUZZ’) and the USB sound card. You should make the connection through a decoupling capacitor and possibly add a resistor divider to attenuate the signal if necessary. This eliminates the outside noise problem completely. Moreover, you can unsolder the buzzer as well if you want to mute it entirely. This ReadMe won't explain how to do all these things: I assume that if you are prepared to solder on your printer's main board, you also have sufficient knowledge how to make this kind of connection correctly.
PAQ (Possibly Asked Questions)
Is this really reliable? It can't be!
Yes it is. I have been using it for more than a year now and despite the few glitches mentioned in the ‘current issues’, it works incredibly well.
Can I run something else on the same Raspberry Pi?
It depends. If the other process consumes a lot of CPU or I/O load, then it risks interfering with the real-time operation of the beep detection. So far someone has reported trying to run this system on the same Pi as Octoprint, and this failed because the beep detector couldn't reach the required performance. There was no obvious cause (the octoprint process did not cause an excessive load), but it was a pre-made custom Octoprint Raspbian image which might have contained modifications that interfere with the USB sound card. In other words, your mileage may vary. Safest is to use a dedicated Raspberry Pi for the fan system, they are cheap anyway…
Installing
Step 1: gather the required hardware
You need:
- A Raspberry Pi that has sufficient oomph to run the beep detector, and also WiFi (not strictly essential, but recommended). I am not sure whether a Pi 2 suffices, but a 3B certainly does. These things are cheap anyway so if you already have an old Pi inside your printer, this might be a good time to upgrade it. You don't need a huge microSD card, 16GB is sufficient.
- A small 5V power supply for your Pi. Ideally, it should be small enough to fit inside your printer. I used a simple off-the shelf supply (this one to be exact) that I could tuck under a bundle of wires inside the printer. I connected it to the mains contacts of the printer's PSU so the Pi is toggled together with the rest of the printer. If you have a choice between multiple supplies that fit, pick the one with the most flexible cable and the most compact microUSB plug. This is where you'll have to be a bit creative with parts you can find in local stores. You could take apart a USB power supply to more easily connect it to the mains, or you could plug the supply into half an extension cord like I did. Whatever you do, do not create dubious and dangerous constructions that expose mains voltage.
- A USB sound card. It will be much easier if it is as small as possible. I recommend to buy one of those extremely cheap ‘3D sound’ sticks with yellow and green 3.5mm jack. These are rather crappy but very compact, well supported in Linux, and for the purpose of this application they are good enough. The plastic case of these cards is unnecessarily large; to make it much easier to fit inside your printer you can print this custom case if it fits your model of sound card (if not, modifying the 3D model should be easy).