I need to build a device that will move a nail hanging from a string. I will use an electromagnet like this at a distance 5-10cm from the hanging nail and I want to create a swinging motion to the nail.
I was thinking to use a mosfet like this to turn on and off the electromagnet. I see that electromagnets are connected with a flyback diode in parallel and that mosfet module seems to have a diode on board.
How quickly am I allowed to turn on and off the electromagnet with the mosfet to be protected from voltage spikes? (for example 2-3 times per second is fine?)
Will I be able to use PWM with the mosfet and have a variable intensity field instead of turning it on and off?
Should I use a solid state relay instead?
Your project is getting WAY ahead of itself.
The first step is to determine if holding your electromagnet 5-10cm from the nail ans then connecting the 12 volts to the electromagnet will move your nail the way you want.
If that work the way you want, then you must measure the current drawn by the magnet when it is connected to the 12 volts. That way you can know how much current a mosfet must handle.
Next select a mosfet that will handle the current and then some and be turned completely on by less than 5 volts.
Now you are ready to connect things to your Arduino and try to control the mosfet with a digital pin. Be sure the grounds are all connected.
The inductance of the electromagnet determines how fast you can turn the magnet on and off.
And yes, you need the diode across the electromagnet.