Obstacle avoiding car only has 1 wheel moving

#I'm making a obstacle avoiding robot but when I upload the code, only the right wheel moves. I've eliminated a battery problem or driver and motors. Is there anything wrong with the code?`

#include  <NewPing.h>        
#include  <Servo.h>      


const  int LeftMotorForward = 2;
const int LeftMotorBackward = 3;
const int RightMotorForward  = 4;
const int RightMotorBackward = 5;


#define trig_pin  A1
#define echo_pin A2 

#define maximum_distance  200
boolean goesForward = false;
int distance = 100;

NewPing sonar(trig_pin,  echo_pin, maximum_distance); 
Servo servo_motor; 


void setup(){

  pinMode(RightMotorForward, OUTPUT);
  pinMode(LeftMotorForward,  OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);
  pinMode(RightMotorBackward,  OUTPUT);
  
  servo_motor.attach(8); 

  servo_motor.write(115);
  delay(2000);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
}

void loop(){

  int distanceRight = 0;
  int  distanceLeft = 0;
  delay(50);

  if (distance <= 20){
    moveStop();
    delay(300);
    moveBackward();
    delay(400);
    moveStop();
    delay(300);
    distanceRight = lookRight();
    delay(300);
    distanceLeft  = lookLeft();
    delay(300);

    if (distance >= distanceLeft){
      turnRight();
      moveStop();
    }
    else{
      turnLeft();
      moveStop();
    }
  }
  else{
    moveForward(); 
  }
    distance = readPing();
}

int  lookRight(){  
  servo_motor.write(50);
  delay(500);
  int distance =  readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
}

int  lookLeft(){
  servo_motor.write(170);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
  delay(100);
}

int  readPing(){
  delay(70);
  int cm = sonar.ping_cm();
  if (cm==0){
    cm=250;
  }
  return cm;
}

void moveStop(){
  
  digitalWrite(RightMotorForward,  LOW);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward,  LOW);
  digitalWrite(LeftMotorBackward, LOW);
}

void moveForward(){

  if(!goesForward){

    goesForward=true;
    
    digitalWrite(LeftMotorForward,  HIGH);
    digitalWrite(RightMotorForward, HIGH);
  
    digitalWrite(LeftMotorBackward,  LOW);
    digitalWrite(RightMotorBackward, LOW); 
  }
}

void moveBackward(){

  goesForward=false;

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorBackward,  HIGH);
  
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorForward,  LOW);
  
}

void turnRight(){

  digitalWrite(LeftMotorForward,  HIGH);
  digitalWrite(RightMotorBackward, HIGH);
  
  digitalWrite(LeftMotorBackward,  LOW);
  digitalWrite(RightMotorForward, LOW);
  
  delay(500);
  
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward,  LOW);
 
  
  
}

void turnLeft(){

  digitalWrite(LeftMotorBackward,  HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorForward,  LOW);
  digitalWrite(RightMotorBackward, LOW);

  delay(500);
  
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward,  LOW);
}

Welcome to the forum

How are the motors and servo powered ?
Are they connected directly to pins on the Arduino and which Arduino board do you have ?

Please post a schematic of the project showing all of its components, how they are connected and how they are powered. A 'photo of a hand drawn circuit is good enough

  • I use 2 18650 batteries as the source but couldn't find an equivalent in tinkercad
  • Also my driver is the l298n not the one in the diagram

The code you posted in Post #1 compiles (not tested).

The code you posted in Post #1 and the code in TinkerCad are not the same.

Pls Don't check the code in TinkerCad, the code in Post #1 is my main one

Okay... Using your code in Post #1.

Your code from Post #1 works... so if your hardware is not working, that indicates problems in the hardware (power supply - not enough power?, wiring - not connected the right way?, connection - bad connections? hardware - mis-matched hardware?)

What happens if you exchange the connections driving one motor with the other ?
Does the problem occur with the same motor or teh other one ?

when I switch the wires then the problem happens with the other motor even though I already replace the driver once already

I already did the wiring 3 times over to check if I follow the diagram and the batteries are fully charged. I know the l298n eats up a lot of energy but 2 should be enough right?

That means you missed something... Every day, someone "checks" their wiring, and ultimately, did not. For this reason, please, show a good photograph of your wiring and hardware.

What is the battery output?

You will be determining this.

From which pins on which module do these wire originate?

  • Each battery has a 3.7v output so 7.4v in total
  • The module is a l298n, the side not moving is the Out A and Out B ports, which I connect In A and In B to pin 2 and 3 of the Uno

    -It's quite hard to see through an image since I had to connect 2 wires sometimes to make it long enough

My first guess would be the "magic tape" is not doing its job.
If you "wiggle" the wires, do things start and stop?

When you "move the wires" do you change both ends of each wire, or just one end of each wire?

If you change both ends of each wire, then the wires are bad.

If you change only one end of each wire, then which two wires connected to the L298N make the motor not move?

Your photo is hard to examine. Too far away, not clear when zoomed, and too dark.

I didn't change the wires related to the motor and the driver though, the only wires I changed were thr servo wires and the battery for it to be able to plug in the breadboard

Hi, @klaw2

Do you have a DMM? Digital MultiMeter?

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

A battery with bad wires will not let motors run well due to resistance in the bad part of the wire.

Hi, @klaw2

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

The diagram in Tinker is different to your images.

What motor driver are you using?

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

One thing I can advise you to do is change the jumper wires with solid core wires, or use a digital multimeter to check each and every jumper cable. I dont trust these jumper cables, they have been the source of trouble in many of my projects.

I would start simple.
Remove the servo and sonar altogether.
Take the wheels off the motors.
Disconnect the motors (bear with me).
Write a small sketch that just sets both motors forward.
Measure the GPIO voltages at the MCU pins and at the bridge inputs.
If the MCU voltages are incorrect, check the pins you are flipping high/low in the software to be the same on the MCU.
If the bridge doesn't have the correct input voltages, replace the wires.
If those bridge voltages are correct, measure the output of the bridge.
If no output, replace the bridge.
If there is voltage at the outputs, connect the left motor then the right motor.
If only one runs, disconnect both motors and re-connect the right motor then the left motor.
If the same motor doesn't run in both cases, replace the bridge. If it still doesn't work, replace the motor.
If only the first connected motor runs in both cases and the second doesn't then there's not enough power.