controlling 2 motors with transistors

Ok.. so I have a little robot I built, and I have a 9v motor for each wheel,
connected via a transistor method as shown here:, This Is powered by a 9v battery.
This is the code:

int mtr1 = 6;  
int mtr2 = 5;

void setup() {
  pinMode(mtr1, OUTPUT);
  pinMode(mtr2, OUTPUT);

void loop() {
  digitalWrite(mtr1, HIGH);
  digitalWrite(mtr2, LOW);
  digitalWrite(mtr1, LOW);
  digitalWrite(mtr2, HIGH);

Pretty much I just wrote this to test it out, just switching the motors on back and forth, the problem is...
no matter what pin I assign each motor, only the first assigned motor (mtr1) works.
The other one doesn't do anything
( so if i assign mtr1 = 6 and mtr2 = 5, the motor attached to pin 6 will work and the other won't and vise versa.)
Help Please :slight_smile:

Have you tried two other pins ? maybee one of the two pins (5 and 6 ) you are using are bad ?

Are you using an external powersupply for the motors ? (you should)

Did you connect both transistors correct ?

Are you sure both motors actually work (i guess :-))

Well I have a 9v battery supply, the pins definately work, because if I use the code below,
the one connected to the pin works and then when i connect it to the other transistor with the other motor,
it works as well, just not in the same code together as in my first reply.
**int transistorPin = 6;    // connected to the base of the transistor for one of the motors[/b]

void setup() {
 pinMode(transistorPin, OUTPUT);

void loop() {
 digitalWrite(transistorPin, HIGH);
 digitalWrite(transistorPin, LOW);

Hi david, I wonder if it has something to do with the excess motor current or back EMF. Can you try it with LEDs instead of the motors. Or if you have multimeter, remove the motors and see if the pin voltages alternate every second. If everything works as expected without the motors then look closely at your motor drive circuit.

BTW, if the 9v battery is also powering the arduino through the regulator, check that the voltage isn't dropping below the regulator dropout threshold (probably 7v) when the motor switches on.

ok thanks I'll try that.

Based on the basic knowlege I now have on transistors, I made this circuit for controlling a motor to turn both ways, I'm sure this already exists, but could someone look at this one and tell me If it works or not and if so, what changes:

That's not going to work.

For one thing, the transistors on the right are the wrong polarity - you also need base resistors.

I found this page: BJT H-bridge Circuit Details

it shows an h-bridge, but with added opto isolaters which you don't need.

Hopefully you can pick out some useful info from it.