Go Down

### Topic: Controlling DC motor (transistor problems solved *Use MOSFETs* (Read 7193 times)previous topic - next topic

#### polishdude20

##### Mar 09, 2011, 03:25 amLast Edit: Mar 10, 2011, 05:40 am by polishdude20 Reason: 1
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.

#### retrolefty

#1
##### 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

#### polishdude20

#2
##### 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);        }`

#### retrolefty

#3
##### 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

#### polishdude20

#4
##### 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.

#### polishdude20

#5
##### 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.

#6
##### 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.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### polishdude20

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

#8
##### Mar 09, 2011, 07:41 am
If the motor runs that way, yes.

I am waiting ...
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### polishdude20

#9
##### Mar 09, 2011, 07:45 am

If the motor runs that way, yes.

I am waiting ...

thanks for being patient.

#10
##### 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.

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### polishdude20

#11
##### Mar 09, 2011, 08:01 amLast Edit: Mar 09, 2011, 08:06 am by polishdude20 Reason: 1

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!

#12
##### 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 ...
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### jackrae

#13
##### 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

#### polishdude20

#14
##### 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?

Go Up