Can't figure out how to use pwm with an optocoupler and a MOSFET

At the end my IRF540 didn't work, so I bought a IRF530 N Channel, the circuit is working now using the first diagram as I linked, thanks for the help.

Note: I added a 10k resistor to ground connected to the Gate, is it ok, or should I remove it?