[HELP] Set the speed of 2 DC motors with H-Bridge

First, thanks for your replies.

I've connected the en1Pin to the ENB and the en2Pin to the ENA, the INPUT pins are correctly connected.
And yes I try to use one H-Bridge. And without trying to regular the speed, it works well.

What H bridge are you using ? Can you provide a link to what you are using ?
If it has ENA and ENB inputs then it is almost certainly 2 H bridges, even if they are in 1 chip, possibly an L293 or similar.

Yes its an L293.

Did you build the circuit yourself or buy it ready made ?
If you built it yourself what is the circuit ?
If ready made is there a link to the product ?

I buyed it, here's a link:
http://my.safaribooksonline.com/getfile?item=ODMvZDlpY3B0L20vZ2VzOTg4Mzk5NjI1c2FzcjdhaWxjZW90dC9odHBvdG9tcmxjdW1zeW9yb2Vyc2VhZ2VwZTNlaWx5Z3JpYW55bXMzNjcwMTIuZ24ubGFtanBn
Evrything the same, except that I connected the ENB to Pin6 and added a second motor.

for this kind of h bridge i would like to use functions actually

void Motor(boolean Dir, int Speed)
{
  int Stop;
  if (Dir ==Stop)
  {
    digitalWrite(Pin1,LOW);
    digitalWrite(Pin2,LOW);
  }
  else
  {
    digitalWrite(Pin1,Dir);
    digitalWrite(Pin2,!Dir);
    analogWrite(EnPin,Speed);
  }
}

for dir you could specify HIGH and LOW,

void Motor(boolean Dir, int Speed)
{
  int Stop;
  if (Dir ==Stop)

Really bad idea. Please rethink.

your write AWOL, Its should be and int instead of a Boolean

But where can I set the speed there?

I am all in favour of using functions but unless the inline code works when the inputs to the H bridge are set correctly then a function is not going to help.

To make things even easier to understand I have written a small class to control a motor attached to an H bridge but that does depend on the basic code working, of course.

your write AWOL, Its should be and int instead of a Boolean

No, think again.
What is the value of "Stop"?

ok How about this

void Motor1(boolean Pin1, boolean Pin2, int Speed)
{
  digitalWrite(in1Pin,Pin1);
  digitalWrite(in2Pin,Pin2);
  analogWrite(en1Pin,Speed);
}
void Motor2(boolean Pin1, boolean Pin2, int Speed)
{
  digitalWrite(in3Pin,Pin1);
  digitalWrite(in4Pin,Pin2);
  analogWrite(en2Pin,Speed);
}

Mats2006:
I buyed it, here's a link:
http://my.safaribooksonline.com/getfile?item=ODMvZDlpY3B0L20vZ2VzOTg4Mzk5NjI1c2FzcjdhaWxjZW90dC9odHBvdG9tcmxjdW1zeW9yb2Vyc2VhZ2VwZTNlaWx5Z3JpYW55bXMzNjcwMTIuZ24ubGFtanBn
Evrything the same, except that I connected the ENB to Pin6 and added a second motor.

That link does not work for me.

Does each motor work as expected when it is the only one being controlled ?

A thought. Have you got anything using interrupts, such as a sensor using timer 2, attached to the Arduino ?
I had problems with motor control when I added a ping sensor to my buggy. (Arduino Uno) It stopped PWM working on pins 3 and 11 because its library uses timer 2 which is also used for PWM on those pins. I had to switch the enable pin of one motor from pin 3 to pin 10.

OK, the picture is in the attach.
Yes, you can control one motor, but then the other one does nothing.
And no, I don't use any sensor, just a button and a few LED's :slight_smile:

getfile.jpg

try moving the enable switch to another free PWM pin. that should give you the speed control you need

What do you mean 'PWM pin'?

What do you mean 'PWM pin'?

Thank you, I'll try tomorrow

Mats2006:
OK, the picture is in the attach.
Yes, you can control one motor, but then the other one does nothing.
And no, I don't use any sensor, just a button and a few LED's :slight_smile:

Looking at your code and judging by the names used for the Arduino pins you have not wired your circuit the same as the example circuit. That will not matter as long as you have wired the enable and inputs of the H bridge to the pins that the names in your code suggest. ie

Arduino pin H bridge
5 2 IN1 - motor A
4 7 IN2 - motor A
6 1 Enable motor A

3 10 IN3 - motor B
2 15 IN4 - motor B
7 9 Enable motor B

I suggest that you check this carefully just in case.