Motor driver ln298n problem - Same speed no matter value

I'm working on a RC car. Everything works fine except one thing : I can't control the speed of the motors. No matter what value I send to the ENA pin, motors are spinning the same. This is my code :

//motor A
int dir1PinA = 7;
int dir2PinA = 6;
int speedPinA = 2;

//motor B
int dir1PinB = 5;
int dir2PinB = 4;
int speedPinB = 3;



void setup(){
Serial.begin(9600);
//I'm not setting the pin modes because if I do the motors wont spin at all.
//pinMode(dir1PinA,OUTPUT);
//pinMode(dir2PinA,OUTPUT);
//pinMode(speedPinA,OUTPUT);
//pinMode(dir1PinB,OUTPUT);
//pinMode(dir2PinB,OUTPUT);
//pinMode(speedPinB,OUTPUT);
}

void loop(){
  motor_a(1,30);
  motor_b(1,30);
  delay(1000);
  //motor_a(1,10);
  delay(1000);
  motor_a(2,80);
  motor_b(2,30);
  //motor_a(2,10);
  delay(1000);
}

void motor_a(int dir,  int speed_proccent){
  int speed_val = map(speed_proccent, 0,100,0,255);
  Serial.println(speed_val);
  if(dir == 1){
      digitalWrite(dir1PinA,HIGH);
      digitalWrite(dir2PinA,LOW);
      digitalWrite(speedPinA,speed_val);
  }else if(dir == 2){
      digitalWrite(dir1PinA,LOW);
      digitalWrite(dir2PinA,HIGH);
      digitalWrite(speedPinA,speed_val);
  }else if(dir == 0){
      digitalWrite(speedPinA,0);
  }else if(dir == 3){
      digitalWrite(dir1PinA,LOW);
      digitalWrite(dir2PinA,LOW);
      digitalWrite(speedPinA,speed_val);
  }
}

void motor_b(int dir,  int speed_proccent){
  int speed_val = map(speed_proccent, 0,100,0,255);
  if(dir == 1){
      digitalWrite(dir1PinB,HIGH);
      digitalWrite(dir2PinB,LOW);
      digitalWrite(speedPinB,speed_val);
  }else if(dir == 2){
      digitalWrite(dir1PinB,LOW);
      digitalWrite(dir2PinB,HIGH);
      digitalWrite(speedPinB,speed_val);
  }else if(dir == 0){
      digitalWrite(speedPinB,0);
  }else if(dir == 3){
      digitalWrite(dir1PinB,LOW);
      digitalWrite(dir2PinB,LOW);
      digitalWrite(speedPinB,speed_val);
  }
}
 digitalWrite(speedPinA,speed_val);[code]

Shouldn't that be analogWrite for PWM?

groundfungus: ``` digitalWrite(speedPinA,speed_val);[code]

Shouldn't that be analogWrite for PWM?

If I do that, the motors wont rotate anymore.

You might have to use a high value with analogWrite to get the motors started, like 250, 255, then back off on the value for a slower speed.

digitalWrite(speedPinA,speed_val);

If speed_val = 0 the motor will stop, any other speed_val is evaluated as a 1 an turns the motor on.

You will need to find the PWM value that will start the motor to run. No motor will start with a PWM value of 1% 0r 2%. 10% power very likely is not enough and 30% could be not enough power depending on your motor, driver and power supply. So, in your sketch change the indicated lines to analogWrite and increase the value that you sent to 100% or 90% and see if they run.

groundfungus: digitalWrite(speedPinA,speed_val);

If speed_val = 0 the motor will stop, any other speed_val is evaluated as a 1 an turns the motor on.

You will need to find the PWM value that will start the motor to run. No motor will start with a PWM value of 1% 0r 2%. 10% power very likely is not enough and 30% could be not enough power depending on your motor, driver and power supply. So, in your sketch change the indicated lines to analogWrite and increase the value that you sent to 100% or 90% and see if they run.

You are right. After 80% , the motor starts. But it is the same speed as before. Also, increasing the proccentage to 100% does no effect (At least I can't see any). Maybe the battery is just too weak?

Maybe the battery is just too weak?

Can't see your battery, but that is certainly possible. What is the rated voltage, running current and stall current of the motors? What are the specifications of your battery? Which motor drivers are you using?

groundfungus: Can't see your battery, but that is certainly possible. What is the rated voltage, running current and stall current of the motors? What are the specifications of your battery? Which motor drivers are you using?

My battery is 6V.

Motors specification : ( I have 4 of them on my car) -Operating voltage: 3V ~ 12V DC (recommended value is 6 ~ 8V) -Maximum torque: 800g.cm (3V) -Speed without load: 170 RPM (3V) -Reduction ratio: 1:48 -Load current: 70mA (250mA MAX) (3V pm)

Also, another interesting thing I discovered : If I use a 12v battery, the motors wont work at all. (They will just make a weird sound that I can barely hear)

Which motor drivers are you using?

groundfungus: Which motor drivers are you using?

OP's subject says a 298 but doesn't say if it's loose chips or a breakout.

6V supply thru a 298 is going to leave 4V for the motor at best, perhaps less at startup with stall current, due to the 298's design.

Can we see a circuit diagram: very odd about the 12V not working at all.

Grounds all connected?

OP's subject says a 298

Right m_r, my keen eye for the obvious is a little blind, today. Must need coffee.

The OP should be more specific when he mentions hardware. Like the battery, we can't see the power supply and so know nothing about it other than 12V. It could be AC for all we know. What is the 6V battery capacity (2000mAh, 50mAh)?

groundfungus:
Right m_r, my keen eye for the obvious is a little blind, today. Must need coffee.

The OP should be more specific when he mentions hardware. Like the battery, we can’t see the power supply and so know nothing about it other than 12V. It could be AC for all we know.
What is the 6V battery capacity (2000mAh, 50mAh)?

The 6v battery is actually composed of 4x batteries connected in series. Each battery is alkaline, AA type, and has 1.5V. Some other info from them : Mignon, LR6. This is all I can find on them.

The 12V battery is also alkaline. Some other info from it : 6AM6 ; MN1604, E-Block

About the motor driver, is a ln298n. This is the exact model I have :
http://www.ebay.com/itm/162047146296?_trksid=p2060353.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT
Not the best one at that price probbably.

The scheme is simple. Just 2 motors on the right , 2 on the left. The ones on the right enter OUT1 and OUT2, the ones on the left enter OUT3 and 4. And every other pin is connected as it should to the battery and to the arduino board.

Another interesting thing : if I power the arduino board from the motor drive, the motors will also stop working. So I have to power the arduino with something else.

I'm still looking for a solution. Can anyone help?

Hi, What model Arduino are you using. The pins you want to analogWrite to, are they PWM pins? I see you are trying to digitalWrite the speed value, digitalWrite can only be HIGH or LOW.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png? Including power supplies and motors.

The ebay link doesn't work..

Are these the batteries you are trying to power everything with? |500x410

Thanks.. Tom... :)

softt: You are right. After 80% , the motor starts. But it is the same speed as before. Also, increasing the proccentage to 100% does no effect (At least I can't see any).

Applying PWM to a motor only controls the speed if the motor is under a load. If there is no load or it is a light load then the motor simply coasts during the off period.

Have you wired the motor up so that when it is off it is applying flywheel breaking. Without that there is little effect from PWM motor speed control.