H bridge problem (DC motor)

Hello, with my arduino UNO, I want to control a DC motor. It’s pretty basic, I want it to reverse and go forward at certain points in time. I used this useful tutorial on how to do it with an H-Bridge: http://itp.nyu.edu/physcomp/Labs/DCMotorControl

For the most part, it kinda works. I have encountered a couple of problems.

First of all, my enablePin doesn’t influence anything. I tried to make 2 speeds, slow and fast. I saw online that you can do this with analogWrite and the enable pin. But with whatever value I put to my enable pin (0, 400, 500, 1000…), the motor always spins full speed.

Which brings me to me second problem, the reverse and forward doesn’t have the same speed. My reverse goes faster than my forward (with the same enablePin value). Which makes everything more complicated.

Do you have any idea why I have those 2 problems? Here’s my code if it helps understand my problem:

int motorPin1=5;
int motorPin2=4;
int enablePin=6;


void setup() {

  //setup everything with everything off
    pinMode(motorPin1, OUTPUT); 
    pinMode(motorPin2, OUTPUT); 
    pinMode(enablePin, OUTPUT);
    
    digitalWrite(motorPin1, LOW);  
    digitalWrite(motorPin2, LOW); 
    
    digitalWrite(enablePin, LOW);
  }
  
  
  void loop() {
  
  //go forward slowly
      digitalWrite(motorPin1, HIGH);  
      digitalWrite(motorPin2, LOW); 
      analogWrite(enablePin, 450);
      delay(3000);
  //go reverse fast
      digitalWrite(motorPin1, LOW);  
      digitalWrite(motorPin2, HIGH); 
      analogWrite(enablePin, 1000);
      delay(3000);
      
   //go reverse slowly
      digitalWrite(motorPin1, LOW);  
      digitalWrite(motorPin2, HIGH); 
      analogWrite(enablePin, 450);
      delay(3000);
      
   //go forward fast
      digitalWrite(motorPin1, HIGH);  
      digitalWrite(motorPin2, LOW); 
      analogWrite(enablePin, 1000);
      delay(3000);
      
    //STOP
      digitalWrite(motorPin1, LOW);  
      digitalWrite(motorPin2, LOW); 
      delay(3000);
      
      
   //repeat

}

I’ve not looked at the code but let’s get 1 possibility out of the way up front. Connect your motor to the supply/battery, + to + and - to - and note it’s speed. Then reverse the connections and see if the speed is the same.

EDIT : OK, looked at the code. You’re using out-of-bound values for the analogWrite(). The range of the speed control variable is from 0 (off) to 255 (full on). Rescale your inputs accordingly.

http://arduino.cc/en/Reference/AnalogWrite

I would recommend a short disabled period between any forward and reverse commands. Perhaps 100 msec or so, just to let the back EMF from the motor bleed off a little.

Thanks for the replay. I inverted and connections of my motors but I still have the same problem. I am using the same 5V from my arduino to power everything, I don’t have a specific battery for my motor, do you think it’s because of that? (I basicaly plug everything that need power to the 5V pin of my arduino)

Here in my corrected code with 100ms of bleed:

int motorPin1=5;
int motorPin2=4;
int enablePin=6;


void setup() {

  //setup everything with everything off
    pinMode(motorPin1, OUTPUT); 
    pinMode(motorPin2, OUTPUT); 
    pinMode(enablePin, OUTPUT);
    
    digitalWrite(motorPin1, LOW);  
    digitalWrite(motorPin2, LOW); 
    
    digitalWrite(enablePin, LOW);
  }
  
  
  void loop() {
  
  //go forward slowly
      digitalWrite(motorPin1, HIGH);  
      digitalWrite(motorPin2, LOW); 
      analogWrite(enablePin, 50);
      delay(3000);
      digitalWrite(motorPin1, LOW);  
      digitalWrite(motorPin2, LOW); 
      delay(100);
      
  //go reverse fast
      digitalWrite(motorPin1, LOW);  
      digitalWrite(motorPin2, HIGH); 
      analogWrite(enablePin, 255);
      delay(3000);
      digitalWrite(motorPin1, LOW);  
      digitalWrite(motorPin2, LOW); 
      delay(100);      
      
   //go reverse slowly
      digitalWrite(motorPin1, LOW);  
      digitalWrite(motorPin2, HIGH); 
      analogWrite(enablePin, 50);
      delay(3000);
      digitalWrite(motorPin1, LOW);  
      digitalWrite(motorPin2, LOW); 
      delay(100);
      
   //go forward fast
      digitalWrite(motorPin1, HIGH);  
      digitalWrite(motorPin2, LOW); 
      analogWrite(enablePin, 255);
      delay(3000);
      digitalWrite(motorPin1, LOW);  
      digitalWrite(motorPin2, LOW); 
      delay(100);
      
    //STOP
      digitalWrite(motorPin1, LOW);  
      digitalWrite(motorPin2, LOW); 
      delay(3000);
      
      
   //repeat

}

EDIT: I think I found the problem of the the uneven reverse and forward, I had a servo connected to the same breadboard on the same column and I think they were some interference. When I unplugged the servo, the forward and reverse were the same. What can I do to keep that servo connected without affecting the dc motor? A small capacitor?

Oh and the enablePin still doesn’t affect anything, everything is at full speed all the time

Thank You

jseb:
I am using the same 5V from my arduino to power everything, I don’t have a specific battery for my motor, do you think it’s because of that? (I basicaly plug everything that need power to the 5V pin of my arduino)

W/o knowing the details of your servo and motor all I can say is that's it's not a good idea. The peak current demanded by the motor and/or servo can overload the Arduino's regulator. The noise being put on the 5V supply by those devices isn't a good thing either.

Oh and the enablePin still doesn’t affect anything, everything is at full speed all the time

Does the motor stop for the 3 secs as commanded ?

Mee_n_Mac:

jseb:
I am using the same 5V from my arduino to power everything, I don’t have a specific battery for my motor, do you think it’s because of that? (I basicaly plug everything that need power to the 5V pin of my arduino)

W/o knowing the details of your servo and motor all I can say is that's it's not a good idea. The peak current demanded by the motor and/or servo can overload the Arduino's regulator. The noise being put on the 5V supply by those devices isn't a good thing either.

Oh and the enablePin still doesn’t affect anything, everything is at full speed all the time

Does the motor stop for the 3 secs as commanded ?

The servo and the dc motor won’t be running at the same time, it’s a basic servo motor like the Medium Servo of SFE.

OK here’s an update, I just think that the connections on my breadboard are not optimal because sometimes it works and sometimes it doesnt. I’ll still go work on it and see if I can identify the problem (ill try with another breadboard)

The motor does spins the 3 seconds each time, but I still get the uneven reverse forward sometimes. I’ll try connecting everything on a fresh board

Thanks for the help!