Two DC Motor control

I'm making a robot that needs to follow commands, go forward, right, left. I used Uno, two DC (quite cheap) motors and L298n Motor control. enA and enB connected to pin 5.6. I want to achieve as straight a movement as possible, because at the moment there is a vague movement to the left. How could I achieve a straighter movement?

@kemzansdinars
You use IN1,IN2,IN3 and IN4 to control the direction, starting and stopping of the motors
Use enA and enB to control the speed of the motors using PWM
If it's veering to the left then you need to slow down the speed of the motor on the right.

I have tried to change the input (written) values of enA and enB motors, however the robot still rotates to the left. That mean I need reduce the value of the left motor more?

@jim-p

No, not the left motor, the right motor
If it going to the LEFT, then slow down the RIGHT motor

Here's some code you can try.
Connect the L298 to he Arduino as explained in the code.

// L298 motor driver

// Connections for Motor A
int enA = 9; // connect enA to pin 9
int in1 = 8; // connect IN1 to pin 8
int in2 = 7; // connect IN2 to pin 7

// Connections for Motor B
int enB = 3; // connect enB to pin 3
int in3 = 5; // connect IN3 to pin 5
int in4 = 4; // connect IN4 to pin 4

void setup() 
{
  // Set all the motor control pins to outputs
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  
  // Turn he motors off
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  
  // Set the motor speeds to zero
  analogWrite(enA, 0);
  analogWrite(enB, 0);
}

// Main loop
void loop() 
{
  // Turn on motors A & B
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);

  // Set both motors to maximum speed
  // For PWM: 0=Stop and 255=full speed
  analogWrite(enA, 255);
  analogWrite(enB, 255);
  
  delay(5000); // Run for 5 seconds
  
  // Make motor B a little slower
  analogWrite(enB, 250); // Set the speed to 250
  
  delay(10000); // Run for 10 seconds

  // Turn off motors
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);

  while (true) delay(10); // wait here forever
  
}

I connected it like that, but the robot still travels to the same place, i.e. very obliquely

@kemzansdinars Are your wheels crooked then? Because if you already sped up the motor on the left then it should not be veering that way anymore. Also check for an asymmetric connection with the motor and the board, as one motor could be getting more power than the other.

Well as you said your motors are "quite cheap". One may be much stronger than the other.
You will have to experiment with the speeds untill you can get them both the same.
Try slowing down the right motor even more maybe to half speed (128)

i tried putting '100' on one motor but the motor didn't start to slow down. Maybe there is a problem with the transfer of power to the motor?

@kemzansdinars Check for an asymmetric connection with the motors and the board, as one motor could be getting more power than the other.

Being rather suspicious of what you have used for motors. Please confirm that everything about the two motors is identical. Worst case, can you exchange the left and right motors? Can you reverse the direction of your robot and still get the movement to the left?

when applying the same speed, I measured and both motors receive 5.83V. If you start the robot to go forward and then back, then the robot stops there even at the beginning.

@kemzansdinars Hmm... It sounds like you might've accidentally purchased two one-way motors. Try everything we've suggested and also try and reverse them individually to see if they're one-way or not.

Can you show a picure of the motors or a link to the webpage where you purchased them.
Also show a picture of your L298 driver

all the parts were given so I don't know where they were purchased from. I have managed to turn right and also left with the motors, only the forward and backward movement is not straight. attached motors and motor driver


Can you make the motor slow down at all?
Try PWM values of 10, 20, 30, 40, 50, etc

I tried one motor with analogWrite giving 0 so the motor should spin, however the motor spins.

OK
That means you have some wires connect wrong somewhere.
Check the connections.
Are you sure that enA is connected to pin 9 and enB connect to pin 3?

I checked again, yes it is.

If you made analogWrite(enA, 0); then motor A should not spin.
Maybe there is a broken wire somewhere or you made a bad solder connection somewhere or maybe something wrong with your L298 driver.
Disconnect the enA wire from the arduino and connect it to GND and run the program. The A motor should not spin when you do this