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

Hi guys, I'm trying to use the pwm signal from my arduino to control a motor with 12v, I'm following this diagram, with the exception I'm using an IRF540:

Is there anything wrong on the diagram above? The only way I was able to use the pwm signal was with this other diagram but this one doesn't use an optocoupler:

I'd like to know how can I work out the other diagram too.

Top one looks fine.
When Arduino output is high, LED will light, will turn on NPN to pull MOSFET gate low & turn off the motor. Otherwise MOSFET is on.

I see IRF530 shown as both N-channel and P-channel

Do you have a complete part #?

Not really, in the diagram it didn't specify that. What would you recommend me to check out if I'm connecting wrong the circuit?

The circuit is correct, if you don't have parts yet make sure you get an n-channel part.

CrossRoads:
I see IRF530 shown as both N-channel and P-channel
http://www.digikey.com/product-search/en?x=0&y=0&lang=en&site=us&KeyWords=irf530

Now that is a bizarre arrangement, bound to cause big trouble!

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?

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

It's not necessary - either the optocoupler is off, in which case the FET is turned on, or when the optocoupler is on, it is pulling the gate to ground. The resistor is only necessary when you are driving the gate directly from a microcontroller pin which may be neither "on" or "off" as the program is starting up.

How are resistors (R2 and R3 ) chosen for mosfets? How are they calculated?

tempo1218:
How are resistors (R2 and R3 ) chosen for mosfets? How are they calculated?

Sheer necromancy!

R2 and R3 relate to the fact that the gate of the FET is highly capacitive and its switching speed is limited by how fast you can charge and discharge the gate. R2 in particular needs to be fairly low - you can calculate just how fast you need it to switch and work on that and the gate capacitance to derive the value.

R3 is a curious matter - I am not quite sure what it does. The opto-coupler has a transfer factor, so the actual current it can draw is constrained, but if it is commutating rapidly, it may be preferable to have it saturate in order to pull the gate down, so that its power dissipation is minimised.