Send PWM from GPIO when M106 command

I have an ender 3 and I broke the part cooling fan output. I don't want to buy a new board so I would like to use the raspberry with octoprint that I use to send the PWM to the fan.
To do so I would like to know how I could know when an M106 command is send to the printer to run a script that will do the PWM ?
I don't know if I'm understandable.
If you have any questions or ideas tell me.


I think the plugin you are looking for is the Fan Speed Mirror. This will allow you to send system commands when the M106 is seen in the GCODE. And the commands you are looking for as far as PWM from the PI is concerned are as follows:

gpio mode 1 pwm
gpio pwm-bal
gpio pwmr 255
gpio pwm 1 255

You'll want to hook the Fan's PWM signal up to pin BCM 18 (board 12) of the pi. The first 3 lines will configure the PWM for the pin and should be executed at boot. The last line is what will control the output of the fan. Anywhere from 0-255, which should be the same that is coming across in your GCODE. So, you should just have to pass the S value coming over off to the command.


With the Fan Speed Mirror plugin that would be the script:

gpio mode 1 pwm
gpio pwm-bal
gpio pwmr $1
gpio pwm 1 $1
exit $?
1 Like

Would you have to set up the PWM every time? I was thinking you could add the setup to the rc.local and then just use the last line via the plug in.

Thanks for your answers, i've installed the plugin Fan Speed Mirror and wrote the script b-morgan mentioned. I changed the line gpio pwm because i think it need to always be 255. I've configured the plugin to use the script. But when i send m106 command nothing is on the output of the raspberry.
I've tried running the script direclty in the terminal of putty and it's working.
So i don't know where i made a mistake do you have any ideas ?
EDIT : I forgot to add execution right to the file now it works fine. Now i need to find a way to make the change to voltage of the PWM to be 24V and I will be able to use the printer again.

You won't get 24V out of the PWM itself. You need a MOSFET and a separate 24V supply, with the PWM controlling the MOSFET. Honestly, it is going to be cheaper (unless you just bread board it) and less work to just buy a new board.

1 Like

Is it a 4 pin or 2 pin fan you are trying to control? Because PWM fans (fans with 4 pins) actually work on 3.3 v logic on the control wire as part of the standard. I am controlling a 4 pin PWM fan directly from the Pi and supplying its power separately for an exhaust fan for my printer chamber. Also, remeber to make sure you have a ground connection between the two. Otherwise you won't be able to control the fan either.

But if you're talking a little 2 wire fan, just use a 5 v one and power it from the 5v pin of the Pi. Then all you need is a transistor to control it. But trying to use a 24v one is going to be much more trouble than it's worth.