programming two motors using IRF540N transistors and pots

Hello, I have been trying to program two motors to work independently of eachother using two transistors and two pots. Every time I do so, the motors stop working suddenly and it also acts differently depending on how I have the arduino powered. I have looked up a circuit online here:

https://itp.nyu.edu/physcomp/labs/motors-and-transistors/using-a-transistor-to-control-high-current-loads-with-an-arduino/

This is what I am using and according to this, the external supply should power the arduino which it does not. The code I am using is also on this page at the bottom. I added my own code to add in the second motor. Here it is:

const int transistorPin = 9; // connected to the base of the transistor
const int transistorPin1 = 10;
void setup() {
// set the transistor pin as output:
pinMode(transistorPin, OUTPUT);
pinMode(transistorPin1, OUTPUT);
}

void loop() {
// read the potentiometer:
int sensorValue = analogRead(A0);
int sensorValue1 = analogRead(A1);
// map the sensor value to a range from 0 - 255:
int outputValue = map(sensorValue, 0, 1023, 0, 255);
int outputValue1 = map(sensorValue1, 0, 1023, 0, 255);
// use that to control the transistor:
analogWrite(transistorPin, outputValue);
analogWrite(transistorPin1, outputValue1);
}

If anybody can help me, I'd greatly appreciate it. The arduino always acts all funky when I decide to change a single thing. Every time. I guess it doesn't help that I'm no good at coding haha. But if I can get this to work, I'd be very happy. I have built a KJ66 turbine and I am currently trying to test it, the motors are a fuel pump and a starter for the turbine. I finally found the transistor that gives me the power I need now I just need to be able to power the two motors. Of course I could use two different arduinos but that just seems ridiculous to me when I'm sure I should be able to do this using only one. I then plan to add in code for sensing temperature and rpm. I have code for that, I just need to figure out how to integrate it all together. I actually built a circuit board with an avr chip and arduino bootloader and have it just for the temp and rpm. But that's another story. .. Thanks for any help. . .

John

P.S.

Even with just the one motor code and setup, if I have it connected to an external battery, it will just stop working OR it will lose variable control, going from off to full power. I would love to know what causes this to happen. I don't think it has ever happened when I am powering the arduino via usb port on the computer. I am using a 2 cell 7.4V lipo in the vin pin and the arduino is an arduino uno v3.

Thanks again,

John

It would help us to help you if you post a schematic of your wiring showing all parts and power supplies.

What is the stall current and rated voltage of the motors?

The IRF540 MOSFET is not a logic level device so will not fully turn on with 5V to the gate. The FQP30N60L mentioned on that page is a much better choice.

off the top of my head, there's 2 things i can think of that will cause what your describing. If you have 2 sources of power, like one for the arduino and another for the motors. Be sure that there is a common ground between them both.... if your powering your arduino from the circuit thru the VIN, it's good to install a diode on the VIN, so when you connect thru USB, it won't backfeed power through rest the circuit.

another issue could be the motors are creating a power surge which will cause the arduino to glitch out, so make sure you have a flyback diode for each motor to eliminate that surge. That was an issue i've recently had. tho i was using a small transistor to switch a relay on which controlled the dc motor, and even tho the relays were isolated, there was still a surge until i put the diode between the ground and hot on the motor.

If that's all your code, then i don't really see an issue, and you probably have a wiring or component issue

Definitely the IRF540N is the wrong choice of MOSFET it requires a minimum of 10V gate drive to operate
(12V is the expected drive).

Only a logic-level MOSFET can be driven from just 5V logic directly without a 12V level-shifting circuit or
gate driver chip.

Gate driver chips do what the name suggests and might be useful to you.

Thank you for your reply. I didn't realize the transistor was wrong. It's now just turning on and will not even respond to the pot. I have a lot of tip transistors and some others but every one I've tried just didn't have the power the motor required. Not enough amps were being given. The transistors you guys have named, are they going to be powerful enough? This motor uses like 5+ amps I believe. it needs quite a bit of power in order to turn that rotor. The motor is a speed 300 I believe. Same as in the jetcat starter if any of you are familiar with that. Let me get some more information and I will post more specifics. As far as a schematic, the best I can do is draw one up, although it's the same as on the website I provided. No different. I'm wondering why the arduino is not powering up via the battery in that circuit. I never put outside voltage on while I have it plugged in via usb. Thank you for all of your responses and I will be back with more info.

John

I just messed with it a little more b/c I wanted to see something. . . if I reload the code onto the arduino, it starts acting normal again and will actually vary speed with the pot. Then after awhile, it'll start just going on and off with the pot, off and max speed! That would indicate that the code in the arduino is somehow changing? What else could explain that??

I have found the perfect transistor out of all of the transistors I have. It is a P40NF03L. So far no issues at all. I will keep testing, but I think you guys hit it right on the head. The problem was with the transistor the whole time. Thank you very much for all of your help. I just need to know the perfect transistor to use, you know the voltage and amperage I need, I just don't know everything about them and the datasheets. I can read them but you guys know more about things like what you told me about the transistor not being a logic device etc. I'm not sure this one is either as the voltage is +_ 16V. But since the other one was +_20V, I'm guessing this one is just hitting it right since it's a little lower. But I'd rather have an idea device. But this one is even working with the pump, I was having major issues with the fuel pump, it was running and whining an awful lot and would stop, then go, it was all over the place. This one runs the pump perfectly. I didn't even think it was the transistor, I thought the pump was faulty! Thanks again guys and if you could point me towards the perfect transistor I'd appreciate that as well.

John

P40NF03L equivalent anyone? By any chance?

You say you can read the datasheet and that's all you need to do. For a MOSFET the thing to look for is Rds(on) - the resistance when switched fully on. If that is stated for Vgs of 4.5V or lower then the MOSFET is good for being switched by a 5V signal from an Arduino. If it's only given for Vgs of 10V or more then look for a different MOSFET.

So the STP40NF03L is a decent choice for a logic level MOSFET (and one I sometimes use myself).

Steve

Thank you for your response. I checked mouser and a few others and nobody seems to have it. Mouser has it backordered. Do you know of any others off the top of your head that I wouldn’t have a hard time buying? That one looked really good... it’s too bad.

thanks

Digikey seems to have thousands of them
https://www.digikey.com/products/en?keywords=STP40NF03L
(check the link for extra characters at the start and end)

So I found a really good transistor at home an irf540. It’s perfect for the fuel pump motor. So I’m testing it and all of a sudden it’s doing the same thing again, it responds to the pot but only off and high speed only. And even resetting the arduino, nothing changes. Putting in any other transistor results in the same behavior. It’s almost like the code changed in the arduino itself or something. Bc if I reload the code, all goes back to normal. Any idea why or how this could happen?

Thank you crossroads, I will check out digikey for that. But as said above, I think I have a good one here at home but now I’ve encountered that same problem again where it starts acting bonkers for some reason. I hope you guys have some idea of what could be happening. It’s the same exact circuit and code from the site on my original post. Nothing different but the transistor.

What are you talking about? You started off with an IRF540 which we explained won't work because it is not a logic level MOSFET.

An IRF540 is STILL not a logic level device so why have you suddenly decided that it is perfect?

Steve

I’m sorry I meant another type of transistor, I’ll get the name for it. It is a logic level transistor, 2 to 5v

Tip31 or tip47 I believe. I hve so many transistors out I must’ve mixed them up, sorry

Those are bipolar transistors. What value is the base resistor? Can an Arduino output supply the required base current?

There is no base resistor

I didn’t realize it was a bipolar and not a mosfet! Wow, I am dumb today