Arduino car With l293d is not turning

Hello. I am trying to create a arduino car using l293d after uploading the code ot arduino uno i take the car in my hand and test the function for going front, back, left and right. It was working correctly with no problem but when i place it on the ground and test the function for going front and back is working without any problem but when i test turning left or right it doesn't turn and i test it again by taking the car on hand. But no problem their but the problem comes when i place it of any surface. I changed the surface many times but its not working. Please Help!

I also checked the connection their is no problem and the wires of motor is connected to the given pins correctly

Also i use 9v, 18v and 24v for just l293d motor shield and gives extra 5v to arduino via a power bank.

(This is a second project of a bt car first project works fine with same components)

#include <AFMotor.h>
#include <Servo.h>
#include <SoftwareSerial.h> // TX RX software library for bluetooth


//define

#define leftir A5
#define rightir A4

//define


//

Servo myservo1;
Servo myservo2;
//#define trigPin1 5
//#define echoPin1 3
#define trigPin2 9
#define echoPin2 10

//


AF_DCMotor motor1(1);  // Motor 1 connected to M1 port of L293D
AF_DCMotor motor2(2);  // Motor 2 connected to M2 port of L293D
AF_DCMotor motor3(3);  // Motor 3 connected to M3 port of L293D
AF_DCMotor motor4(4);  // Motor 4 connected to M4 port of L293D

void setup() {


  //ir

  pinMode(leftir,INPUT);
  pinMode(rightir,INPUT);

  //ir


  //ultrasonic sensor

  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);


  //ultrasonic sensor
  


  //
  Serial.begin(9600);  // Initialize serial communication
  motor1.setSpeed(255); // Set motor speed (0-255)
  motor2.setSpeed(255);
  motor3.setSpeed(255);
  motor4.setSpeed(255);
}

void loop() {

  if (Serial.available() > 0) {
    char command = Serial.read();
    executeCommand(command);

  }


  //ultrasonc sensor

  int duration, distance;     // The Duration will be the input pulse width and distance will be the distance to the obstacle in centimeters
  

  //new ultra

  digitalWrite(trigPin2, HIGH); // The output pulse with 1ms width on trigPin
  delay(1);
  digitalWrite(trigPin2, LOW);
  duration = pulseIn(echoPin2, HIGH);  // Measures the pulse input in the echo pin
  distance = (duration/2) / 29.1;   // Distance is half the duration devided by 29.1 (from datasheet)
  // if the distance is less than 0.5 meter and more than 0 (0 or less means over range)
  if (distance <= 10 && distance >= 0) {

    stopMoving();
    Serial.println("EOAB");


  } else {
  // Nothing
  }
  // Waits 50 milliseconds
  delay(50);

  //new ultra

  
  //ultrasonic sensor

}

void executeCommand(char command) {
  switch (command) {
    case 'F':
      moveForward();
      break;
    case 'B':
      moveBackward();
      break;
    case 'L':
      turnLeft();
      break;
    case 'R':
      turnRight();
      break;
    case 'S':
      stopMoving();
      break;
    case 'Z':
      linefollowing();
      break;


  }
}



void moveForward() {
  motor1.setSpeed(255);
  motor2.setSpeed(255);
  motor3.setSpeed(255);
  motor4.setSpeed(255);
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
}

void moveBackward() {
  motor1.setSpeed(255);
  motor2.setSpeed(255);
  motor3.setSpeed(255);
  motor4.setSpeed(255);
  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);
}

void turnLeft() {
  // Increase speed for the forward motors, decrease for the backward ones
  motor1.setSpeed(255);
  motor2.setSpeed(255);
  motor3.setSpeed(150);  // Reduced speed
  motor4.setSpeed(150);  // Reduced speed
  
  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
}

void turnRight() {
  // Increase speed for the forward motors, decrease for the backward ones
  motor1.setSpeed(150);  // Reduced speed
  motor2.setSpeed(150);  // Reduced speed
  motor3.setSpeed(255);
  motor4.setSpeed(255);
  
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);
}


void stopMoving() {
  motor1.setSpeed(0);
  motor2.setSpeed(0);
  motor3.setSpeed(0);
  motor4.setSpeed(0);
}



  //line detection forward
void linefollowing() {
  int leftSensor = digitalRead(leftir);
  int rightSensor = digitalRead(rightir);

  if (leftSensor == LOW && rightSensor == LOW) {
    // Both sensors detect the line, move forward
    motor1.setSpeed(150);
    motor2.setSpeed(150);
    motor3.setSpeed(150);
    motor4.setSpeed(150);
    motor1.run(FORWARD);
    motor2.run(FORWARD);
    motor3.run(FORWARD);
    motor4.run(FORWARD);
  }
  else if (leftSensor == LOW && rightSensor == HIGH) {
    // Left sensor detects the line, turn left
    motor1.setSpeed(200);
    motor2.setSpeed(200);
    motor3.setSpeed(100);
    motor4.setSpeed(100);
    motor1.run(BACKWARD);
    motor2.run(BACKWARD);
    motor3.run(FORWARD);
    motor4.run(FORWARD);
  }
  else if (leftSensor == HIGH && rightSensor == LOW) {
    // Right sensor detects the line, turn right
    motor1.setSpeed(100);
    motor2.setSpeed(100);
    motor3.setSpeed(200);
    motor4.setSpeed(200);
    motor1.run(FORWARD);
    motor2.run(FORWARD);
    motor3.run(BACKWARD);
    motor4.run(BACKWARD);
  }
  else {
    // No line detected, stop
    stopMoving();
  }
}

//check
void switchtoleftline() {

  //linefollowing sub
  motor1.setSpeed(150);
  motor2.setSpeed(150);
  motor3.setSpeed(160);
  motor4.setSpeed(160);
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);

  delay(1000);

  if(digitalRead(leftir)==0 && digitalRead(rightir)==0) {
    linefollowing();
  }
}

It looks like a powering problem.
Please give us data for the motors and power supplies. Datasheet links would be nice.

I used 300 RPM BO Motor-Straight https://robu.in/product/300-rpm-bo-motor-straight/

MOTOR SPECIFICATION

  1. Low density: lightweight, low inertia.
  2. Capability to absorb shock and vibration as a result of elastic compliance.
  3. Ability to operate with minimum or no lubrication, due to inherent lubricity.
  4. The relatively low coefficient of friction.
  5. Operating Voltage(VDC): 3~12
  6. Shaft Length (mm): 8.5
  7. Shaft Diameter (mm): 5.5 (Double D-type)
  8. No Load Current: 40-180mA.
  9. Rated Speed(After Reduction): 300 RPM
  10. Rated Torque: 0.35 Kgcm

POWER SUPPLY

FOR L293D MOTOR SHILED

3 Hi-Watt 9v Battery Site Link

Hi,

Do you have a DMM? (Digital MultiMeter)

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

yes I tested and the result is 23v after using the battery after testing

What's the stall current of the motors?
Drop that battery type. They don't supply enough current.

So which type of battery i need to use

can you tell me and give the link to buy that

I don't build battery powered things like Yours but there are helpers knowing rechargable good types. Just wait a little.

Hi! Welcome to the forum!

I don't discount the tips that were given earlier, since 9V batteries are a poor choice for working with motors, although you are using an also unusual 3 battery setup (are they in series or parallel?)

Anyway, the part that called my attention was in your sketch.

If you are to work with a 2x motor tank kind of vehicle, it does make sense to run one motor forward and other backward. But in 4 wheel vehicles I believe your code is actually braking the car instead of turning it (this would explain it working on your hand and not on the floor). Maybe it would be better something like this:

void turnLeft() {
  // Increase speed for the forward motors, decrease for the backward ones
  motor1.setSpeed(255);
  motor2.setSpeed(255);
  motor3.setSpeed(150);  // Reduced speed
  motor4.setSpeed(150);  // Reduced speed
  
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
}

void turnRight() {
  // Increase speed for the forward motors, decrease for the backward ones
  motor1.setSpeed(150);  // Reduced speed
  motor2.setSpeed(150);  // Reduced speed
  motor3.setSpeed(255);
  motor4.setSpeed(255);
  
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
}

On the tanks I use a 2x 18650 batteries in series, Like this:

images

it will give you 7.4V

I edited the code and tried again but it still not work. (The battery are connected is series)

Is it better than before? The car runs slow or just stand still?

just stands still on erlier i set also the speed of both motors 255 but not working. In the case of now i think the it is trying to turn but it is not ables to do so

Ok, so I agree that is probably a power issue. Have you removed the jumper from the L293 shield?

image

Yes i removed it because i gave arduino another power supply for power

All right. So, my next bet (as already stated before) is that your 9V batteries set is already dead.

You can try looking for the 2x18650 option I gave or a 6xAA battery pack (both in series, giving respectively 7.4V or 9V).

Come back to tell us what happened. It might help other users :wink:

Currently i have no these battery and i need to do something quickly becuase thsi is a project for a competition oi dont have time to buy it and i checked the voltage in series of the current battery it is 23v on the time when i typing this

Voltage is not the problem. Current is....
New 9V batteries can give you ~500mA. This is definitely not enough to power 4 DC motors.

Can i use a Powerbank Instead of these battery. I already using the powerbank for powering arduino Powerbank

Yes, you can. By the link you provided I was not able to identify if it´s 5V. It has to be at least 5V.