# Arduino Forum

## Using Arduino => Motors, Mechanics, Power and CNC => Topic started by: polishdude20 on Mar 09, 2011, 03:25 am

Title: Controlling DC motor (transistor problems solved *Use MOSFETs*
Post by: polishdude20 on Mar 09, 2011, 03:25 am
So I have 2 motors connecting with 2n3906 transistors. The bases go to their digital pins and the voltage going to the motors (isolated from the arduino) is 9v.

I am making a sort of light follower prototype and I got the arduino to turn the left and right motors on depending on how much light hits one LDR. If my desk lamp is on then right motor high, if its off then left motor high, and lastly if my finger puts a shadow over the LDR then both motors low.

The problem is that both motors spin at the same speed if I have my finger on it and if I put my desk lamp on the right motor spins faster than the left but the left still spins.
Title: Re: Transistors letting volage "leak" through?
Post by: retrolefty on Mar 09, 2011, 03:38 am
I think you really need to post your code and wiring drawing for anyone to have a chance at helping you. You are using series current limiting resistors for the base connections to your two transistors, right?

Lefty

Title: Re: Transistors letting volage "leak" through?
Post by: polishdude20 on Mar 09, 2011, 04:05 am

I think you really need to post your code and wiring drawing for anyone to have a chance at helping you. You are using series current limiting resistors for the base connections to your two transistors, right?

Lefty

yes yes, I am. Before I show you the code could I use MOSFETS in place of the transistors? If so, what type would I need N-type?
Lastly, what is the difference between using a MOSFET for this and a normal transistor?

Code: [Select]
`ok here's the code : int Lm = 5;int Rm = 3;int lval = 0;int rval = 0;int Ls = 4;int Rs = 5;void setup() {                  pinMode(Lm, OUTPUT);      pinMode(Rm, OUTPUT);   Serial.begin(9600); }void loop() {    lval = analogRead(Ls);rval = analogRead(Rs);if  (lval < 250 && rval > 250){  digitalWrite(Lm,HIGH);  digitalWrite(Rm,LOW);}  else if  (rval < 250 && lval > 250){  digitalWrite(Lm,LOW);  digitalWrite(Rm,HIGH);}            Serial.println(lval);        }`
Title: Re: Transistors letting volage "leak" through?
Post by: retrolefty on Mar 09, 2011, 05:34 am
yes yes, I am.

Good, otherwise unlimited current would harm both the output pin and the transistor.

Before I show you the code could I use MOSFETS in place of the transistors?

Yes, but there would be no special advantage as your motors seem to be small to be able to be switched with small 2N3906 transistors. Which is a PNP transistor and most arduino switched applications use a NPN transistor as a low side switch. I would have to see a drawing of your wiring to see if you are driving your motors correctly or not.

If so, what type would I need N-type?

Most switching applications used with arduino outputs using mosfets do use N type as a low side switch.

Lastly, what is the difference between using a MOSFET for this and a normal transistor

From a software point of view no difference, from a hardware point of view mosfets can
have less losses in higher current applications. With your small motors there is no big advantage.

Lefty
Title: Re: Transistors letting volage "leak" through?
Post by: polishdude20 on Mar 09, 2011, 06:42 am

yes yes, I am.

Good, otherwise unlimited current would harm both the output pin and the transistor.

Before I show you the code could I use MOSFETS in place of the transistors?

Yes, but there would be no special advantage as your motors seem to be small to be able to be switched with small 2N3906 transistors. Which is a PNP transistor and most arduino switched applications use a NPN transistor as a low side switch. I would have to see a drawing of your wiring to see if you are driving your motors correctly or not.

If so, what type would I need N-type?

Most switching applications used with arduino outputs using mosfets do use N type as a low side switch.

Lastly, what is the difference between using a MOSFET for this and a normal transistor

From a software point of view no difference, from a hardware point of view mosfets can
have less losses in higher current applications. With your small motors there is no big advantage.

Lefty

ok thanks for all that great info i can soak in and use. But, my transistors heat up ALOT after using the motors. Soon I'll post a diagram of wiring and stuff.
Title: Re: Transistors letting volage "leak" through?
Post by: polishdude20 on Mar 09, 2011, 07:11 am
Wow, well apparently the upload folder is "full". So instead of writing a detailed acount of my problems all over again after losing my post ill just write the basics.

3 problems:

1. Transistors get too hot when using 9 volt vin.
2. When using 5v pin the motors don't turn unless "given encouragement" by turning the shaft lighty, after that they spin on their own fine and at full speed.
3. I want to incorporate some coding that will let the motors spin at the same speed when the light levels are close to a certain number and vary the speeds depending on how much one light level deviates from the other. I really need help on this one more than anything. PWM?

I'm planning on making a shield for this and here's the  breadboard layout for you.

Title: Re: Transistors letting volage "leak" through?
Post by: CrossRoads on Mar 09, 2011, 07:20 am
Do you have a multimeter? Put it in mA mode, and install it in series between one of the red motor leads and the transistor, see how much current is being drawn.
And download a real schematic editor, at least a simple easy to use one like from expresspcb.com. Draw a real schematic, this fritzing nonsense does not help anyone, it is hardly better than taking a picture - can't even see if the transistor is wired correctly as the leads are not labelled.
Title: Re: Transistors letting volage "leak" through?
Post by: polishdude20 on Mar 09, 2011, 07:30 am

Do you have a multimeter? Put it in mA mode, and install it in series between one of the red motor leads and the transistor, see how much current is being drawn.
And download a real schematic editor, at least a simple easy to use one like from expresspcb.com. Draw a real schematic, this fritzing nonsense does not help anyone, it is hardly better than taking a picture - can't even see if the transistor is wired correctly as the leads are not labelled.

Ok we have a multimeter in class but I don't want to bring the whole assembly there so can I bring one DC motor and connect it to the wall wart Ive been using and just try that without the transistor?

And ok Ill show you a proper schematic  in 10 mins if your still here.
Title: Re: Transistors letting volage "leak" through?
Post by: CrossRoads on Mar 09, 2011, 07:41 am
If the motor runs that way, yes.

I am waiting ...
Title: Re: Transistors letting volage "leak" through?
Post by: polishdude20 on Mar 09, 2011, 07:45 am

If the motor runs that way, yes.

I am waiting ...

thanks for being patient.

Title: Re: Transistors letting volage "leak" through?
Post by: CrossRoads on Mar 09, 2011, 07:56 am
Dang! Nice job! The battery is 9V?
I bet you will see that too much current is going thru the PNP transistor.
Do you have access to Logic Level MOSFETs? They have very low turn-on resistance, like in milli-ohm range, so will not dissipate a lot of power.
Say it had Rds of .005 ohm and you had 400mA thru the motor : P=I^2 x R = 0.8mW
The PNP: P=IV, so 400mA X ~0.5V (from C to E) = 200mW.  Big difference.

Title: Re: Transistors letting volage "leak" through?
Post by: polishdude20 on Mar 09, 2011, 08:01 am

Dang! Nice job! The battery is 9V?
I bet you will see that too much current is going thru the PNP transistor.
Do you have access to Logic Level MOSFETs? They have very low turn-on resistance, like in milli-ohm range, so will not dissipate a lot of power.
Say it had Rds of .005 ohm and you had 400mA thru the motor : P=I^2 x R = 0.8mW
The PNP: P=IV, so 400mA X ~0.5V (from C to E) = 200mW.  Big difference.

thanks for the comment! Ok I can try and find some MOSFETS in my class tomorrow. But I still have the problem of the motors only turning on when I touch them quickly, I guess that won't be a problem once I lay the completed bot on the ground with wheels and everything right? Since your an expert here can you give me some tips on writing the code like I talked about in my 3rd problem?

edit: yes battery is 9 volts, well ive been using my 9v wall wart. most likely though Ill be using a 6 volt or 9volt battery for the motors. Tomorrow I'll check how much current the motor draws or even better CHECK THE RESISTANCE AND CALCULATE THE CURRENT FROM MY OWN INPUTTED VOLTAGE YAY!
Title: Re: Transistors letting volage "leak" through?
Post by: CrossRoads on Mar 09, 2011, 08:12 am
There you go :)

Code: [Select]
`if  (lval < 250 && rval > 250){  digitalWrite(Lm,HIGH);  digitalWrite(Rm,LOW);}  else if  (rval < 250 && lval > 250){  digitalWrite(Lm,LOW);  digitalWrite(Rm,HIGH);}`
what do you want to happen if neither condition is met? Say rval & lvan both < 250,or both >250?
You might want to add some delay to give the motors a chance to spin a little & move some before the next reading comes around.

off to bed for me ...
Title: Re: Transistors letting volage "leak" through?
Post by: jackrae on Mar 09, 2011, 09:32 am
If you really are using a battery of the type your illustration shows, then it isn't surprising your motors do not turn unless encouraged to do so.  This type of battery can supply very little current and to start a cheap DC motor requires a fair amount of current.  The current demand falls off rapidly as the motor accelerates.  This high input current will also be contributory to why the transistors are overheating.
Use a "stronger" (larger) battery if you wish to continue on 9 volts or else get an sla type 12volt battery.  These permit serious experimentation and are rechargeable.
jack
Title: Re: Transistors letting volage "leak" through?
Post by: polishdude20 on Mar 09, 2011, 03:59 pm

If you really are using a battery of the type your illustration shows, then it isn't surprising your motors do not turn unless encouraged to do so.  This type of battery can supply very little current and to start a cheap DC motor requires a fair amount of current.  The current demand falls off rapidly as the motor accelerates.  This high input current will also be contributory to why the transistors are overheating.
Use a "stronger" (larger) battery if you wish to continue on 9 volts or else get an sla type 12volt battery.  These permit serious experimentation and are rechargeable.
jack

well idk if you read what I said before but I wasn't using a 9 volt battery. that was there just to show I was using 9 volts. But I was using a 9 volt wall wart rated at 500mA which should be enough for a small dc motor that I'm using?
Title: Re: Transistors letting volage "leak" through?
Post by: polishdude20 on Mar 09, 2011, 04:03 pm

There you go :)

Code: [Select]
`if  (lval < 250 && rval > 250){  digitalWrite(Lm,HIGH);  digitalWrite(Rm,LOW);}  else if  (rval < 250 && lval > 250){  digitalWrite(Lm,LOW);  digitalWrite(Rm,HIGH);}`
what do you want to happen if neither condition is met? Say rval & lvan both < 250,or both >250?
You might want to add some delay to give the motors a chance to spin a little & move some before the next reading comes around.

off to bed for me ...

ok well the code looks like you copied mine from my first post! I'm looking to have the motors spin at the same speed when both sensors are at about the same value. I don't want one to be either off or on but faster and slower.
Title: Re: Transistors letting volage "leak" through?
Post by: CrossRoads on Mar 09, 2011, 04:41 pm
500mA - maybe not.
Did you have a chance to measure the impedance of the motor, or how much current it draws?
Most motors have a stall current rating, this is the current from a dead stop of if the motor gets jammed and can't spin. Can bea couple of amps. And then need much less current once they actually get moving.
Title: Re: Transistors letting volage "leak" through?
Post by: jackrae on Mar 09, 2011, 05:48 pm
Quote " yes battery is 9 volts, well ive been using my 9v wall wart "

Contradictory and/or confusing statements and misleading pictures makes it kind of difficult to establish exactly what you meant.

Never the less the comment about high starting current and collapsing supply voltages still stands.

Measuring across the terminals of a DC motor to establish resistance tends to give confusing information as the brush to commutator resistance is liable to change dependent upon numerous variables including commutator stop position, cleanliness of commutator, brush pressure etc.  The stall current is also dependent upon start friction (stiction), shaft load etc

Measure the wallwart output when you are trying to run your motor but before you help them and no doubt you will see where the problem lies.

jack
Title: Re: Transistors letting volage "leak" through?
Post by: polishdude20 on Mar 10, 2011, 01:56 am
Okay here's what I have discovered!

The forced stopped current (when my hand is preventing it from spinning) is 1.8 - 2 A
The current when I slow the motor down with my hand (simulating ground and wheel pressure?) is at around 600-700mA
The peak current that happens when the motor first starts up is at 1.3A then when it gets to normal operating speed it does about
500mA.

Now I tried this with my teachers 9 volt power supply, my wall wart which is also 9v can only provide 500mA.

I'm planning to use a 6volt battery to power the motors which makes the current a bit lower then.

Lastly,

Good news! I took apart a UPS (i think that's what it's called) and found maybe 7 MOSFETS. I brought 2 with me and this is the part number :  "IRFZ44V"   would those be good?

Oh and I still need to know if I can change the motor speed with the arduino using PWM and the MOSFET?

*Edit*

I connected the MOSFETs and it works! They don't overheat and the motors work fine on the 5v that I give them from the 5v line, it's already USB 5v so the regulator doesnt have to work as hard so t doesn't heat up either.

So now my next endeavor is to use PWM and analog write to control the speed of the motors.
Title: Re: Transistors letting volage "leak" through?
Post by: CrossRoads on Mar 10, 2011, 02:27 am
http://www.irf.com/product-info/datasheets/data/irfz44v.pdf

Well, may be better. Hard to say as Rds is not spec'ed with Vgs at 5V, only for 10V.
Figures 1,2,3 would seem to indicate that Vgs of 5V would allow up to 11A.
Title: Re: Transistors letting volage "leak" through?
Post by: polishdude20 on Mar 10, 2011, 02:42 am

http://www.irf.com/product-info/datasheets/data/irfz44v.pdf

Well, may be better. Hard to say as Rds is not spec'ed with Vgs at 5V, only for 10V.
Figures 1,2,3 would seem to indicate that Vgs of 5V would allow up to 11A.

hey crossroads since our online can you tell me how I can subtract an analog value? Basically one of my sensors gives off about 98 out of 255 and the other shows 110 in the same lighting condition. I want them to be the same so the motor speed is the same when the LDR's are the same.

Here's my code so far. (yes it needs some cleaning

Code: [Select]
`/*  Blink  Turns on an LED on for one second, then off for one second, repeatedly.   This example code is in the public domain. */int Lm = 5;int Rm = 3;int lval = 0;int rval = 0;int Ls = 4;int Rs = 5;int lval2;int rval2;void setup() {                  pinMode(Lm, OUTPUT);       pinMode(Rm, OUTPUT);    Serial.begin(9600); }void loop() {    lval = analogRead(Ls);rval = analogRead(Rs); lval2 = map(lval, 0, 1023, 0, 255); rval2 = map(rval, 0, 1023, 0, 255); analogWrite(Rm,rval2);analogWrite(Lm,lval2);           Serial.println(rval2);        }`
Title: Re: Transistors letting volage "leak" through?
Post by: CrossRoads on Mar 10, 2011, 02:56 am
just simple math once you have read it in: