PWM-stimulated N-channel MOSFET unexpectedly always on
You are using your MOSFET backwards.  your circuit, re-drawn the right way up, with proper symbols, is this:







simulate this circuit &ndash; Schematic created using CircuitLab

Note: I have added the body diode specifically to highlight the problem.

An N-channel MOSFET should be used as a low side switch, not a high side switch.  That specific MOSFET, the gate has to be at least 1.2V higher than the source for it to switch on.  Since your source is 12V that means your PWM signal would have to reach as high as 13.2V to turn the MOSFET on.  Not that it'd make much difference, since the diode bypasses the MOSFET anyway.

So, either turn the MOSFET over and use it to switch the GND wire of whatever you're switching, or use a P-channel MOSFET to switch the 12V.

For low side switching the circuit would be:







simulate this circuit