Pages: [1]   Go Down
Author Topic: Is this circuit okay? Arduino using MOSFET IRF540N as a switch for a motor  (Read 3846 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:


Quote
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:
http://www.irf.com/product-info/datasheets/data/irf540n.pdf

EDIT: Fixed reversed terminals on motor
« Last Edit: May 08, 2013, 11:06:08 pm by EngineerWannabe » Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 619
Posts: 33961
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
EDIT: Fixed reversed terminals on motor
No that makes no odds at all.
I don't understand, what do you mean?

Quote
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.

Quote
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I got it working smiley-grin
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.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 619
Posts: 33961
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
« Last Edit: May 09, 2013, 04:01:01 am by Grumpy_Mike » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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?
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 619
Posts: 33961
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Like this then?
Yes  smiley

Quote
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).
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: May 09, 2013, 07:56:24 am by EngineerWannabe » Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 619
Posts: 33961
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Quote
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.
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 224
Posts: 6613
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Pages: [1]   Go Up
Jump to: