Is this circuit okay? Arduino using MOSFET IRF540N as a switch for a motor

Here it is in a nutshell: A metal detector is hooked up to an Arduino Uno. When it detects metal, the Arduino sends power to an IRF540N MOSFET which activates a DC motor with it's own power supply for a second and then turns off.


I do not know if the IRF540N MOSFET is N-channel or P-channel (if that even matters).
My problem is, everything works fine but the motor doesn't turn off as desired even when the metal detector stops detecting soon after.
I do not know if I've connected something wrongly or if there is an error in my programming - I assume that the digital pin connected to the MOSFET must be set to low/no voltage when I want to turn the motor off but this doesn't work (I only just started learning and playing with transistors today).

Here's the code:

int metalDetector = 11;
int pumpCircuit = 12;

void setup()
{
pinMode(metalDetector, INPUT);
pinMode(pumpCircuit, OUTPUT);
Serial.begin(9600);
}

void loop()
{
// while the metal detector isn't detecting anything, it gives a HIGH voltage (TRUE) reading
if(digitalRead(metalDetector))
{
Serial.println("Nothing");
}
else
{
Serial.println("Metal detected");
digitalWrite(pumpCircuit, HIGH);
delay(1000); //leave the pump on for a second
digitalWrite(pumpCircuit, LOW);
}
delay(2000); //delay between reads
}

Motor datasheet:
http://www.mabuchi-motor.co.jp/cgi-bin/catalog/e_catalog.cgi?CAT_ID=rs_360sh
IRF540N MOSFET datasheet:

EDIT: Fixed reversed terminals on motor

EDIT: Fixed reversed terminals on motor

No that makes no odds at all.

The first problem is that you have not set pin 12 to be an output in the setup function. So that pin is an imput and you are just turning on and off the internal pull up resistor.

Next that mosfet is not a logic level one, it requires at least 10V to fully turn it on. You are probably lucky that you are trying to switch such a low current load that it appears to work.

Finally you would normally have a 100R resistor in series with the gate to protect the arduino pin from over current on switching and a 10K pull down resistor so that the motor is off when the arduino is not powered up or is in the act of booting.

Grumpy_Mike:

EDIT: Fixed reversed terminals on motor

No that makes no odds at all.

I don't understand, what do you mean?

The first problem is that you have not set pin 12 to be an output in the setup function. So that pin is an imput and you are just turning on and off the internal pull up resistor.

Thanks for that, fixed it. Forgot to type it in the post.

Next that mosfet is not a logic level one, it requires at least 10V to fully turn it on. You are probably lucky that you are trying to switch such a low current load that it appears to work.

Finally you would normally have a 100R resistor in series with the gate to protect the arduino pin from over current on switching and a 10K pull down resistor so that the motor is off when the arduino is not powered up or is in the act of booting.

Damn. Okay I also have a TIP31C http://vakits.com/sites/default/files/Tip31C.pdf, would that work?
I was under the impression that transistors like these act as an on/off switch that can be controlled by the Arduino.

I got it working :smiley:
I switched out the MOSFET with a TIP31C and it works like a charm - the motor automatically turns on and off as programmed!
Do i need any kind of resistors or something anywhere in the circuit with this transistor? Also If I wanted to use 6v, 4x AA batteries? Apologies for the newbish questions, I'm new to all of this.

I don't understand, what do you mean?

I mean that what way round the motor is wired only affects the motor's direction, nothing else.

Yes you need the resistors I mentioned in my first post to correctly compleate the design. However because you are using a bipolar transistor in place of a FET you need a base resistor to prevent damaging your arduino. A1K should do.

Grumpy_Mike:

I don't understand, what do you mean?

I mean that what way round the motor is wired only affects the motor's direction, nothing else.

Yes you need the resistors I mentioned in my first post to correctly compleate the design. However because you are using a bipolar transistor in place of a FET you need a base resistor to prevent damaging your arduino. A1K should do.

Like this then?


Since I'm no longer using the FET, do I still need the 10k pull down resistor and if so, where exactly does it need to be connected?

Like this then?

Yes :slight_smile:

Since I'm no longer using the FET, do I still need the 10k pull down resistor

It is not as important but it is a good idea. Connect it between the base of the transistor and ground (the emitter).

Alright so like this:


Pin 1 and 3 (base and emitter) connected via 10k resistor and this should work awesomely yes?
Thanks again for all your help man.

Just out of curiosity, did you do the math yourself on the need for these resistors or did you know this from experience?
I'm not questioning your advice, I'm just wondering if it would take at least an introductory electrical course at university (or more) for me to be able to know when to put these resistors and such in future designs.

Pin 1 and 3 (base and emitter) connected via 10k resistor and this should work awesomely yes?

Yes

did you do the math yourself on the need for these resistors or did you know this from experience?

The values are not too critical so while I have done the maths hundreds of times they always come out the same so you could say it is from experience.
The 10K is to ensure the transistor's base is not floating during power off of the arduino, anything from 10K to 100K will work just the same. The 1K is to ensure that the base current is limited to 4.2mA because a base voltage of 0.7V being driven from 5V will give 4.2V across the resistor giving 4.2mA base current. Then this base current is multiplied by the gain of the transistor to give the maximum load current. So suppose you had a gain of 100 then the maximum the load could be would be 420mA. As you did not give any figures for motor current I assumed that this would be sufficient. Keeping the current out of the arduino pin below 35mA is important for the health of the arduino, but apart from that it is all very loosely defined, as long as you are in the right ball park it will be fine.

  1. Using TIP31C, a 1k base resistor is rather high. I suggest you reduce it to 220 ohms, otherwise the TIP31C may overheat, especially when the motor is stalled.

  2. You need to add a flyback diode in parallel with the motor, otherwise your TIP31C may not last very long. 1N4001 will do.