One DC motor is running alone

Hello everyone, I need your help!

I am trying to build an obstacle avoiding car, with 4 DC motors, motor shield L293D, Arduino, and most importantly the acid batter with 12V an 1.3Ah.

My first problem was, before changing the battery and putting a usual 9v battery, the motors were running very slow. So that is why I changed the battery to this one.

The second problem was, after changing the battery, one DC motor was just running and the other three were solid fix.(?!)

I thought the problem was in the code but here it is attached below, so can anyone tell me if the battery I am using is too much for the components? I am running the motor shield and the arduino uno together with the same power supply.

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

#define TRIG_PIN A0 
#define ECHO_PIN A1 
#define MAX_DISTANCE 200 
#define MAX_SPEED 190 // sets speed of DC  motors
#define MAX_SPEED_OFFSET 20

NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); 

AF_DCMotor motor1(1, MOTOR12_1KHZ); 
AF_DCMotor motor2(2, MOTOR12_1KHZ);
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);
Servo myservo;   

boolean goesForward=false;
int distance = 100;
int speedSet = 0;

void setup() {

  myservo.attach(10);  
  myservo.write(115); 
  delay(2000);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
}

void loop() {
 int distanceR = 0;
 int distanceL =  0;
 delay(40);
 
 if(distance<=20)
 {
  moveStop();
  delay(500);
  moveBackward();
  delay(500);
  moveStop();
  delay(500);
  distanceR = lookRight();
  delay(500);
  distanceL = lookLeft();
  delay(200);

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

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

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

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

void moveStop() {
  motor1.run(RELEASE); 
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
  } 
  
void moveForward() {

 if(!goesForward)
  {
    goesForward=true;
    delay(500);
    motor1.run(FORWARD);      
    motor2.run(FORWARD);
    motor3.run(FORWARD); 
    motor4.run(FORWARD);     
   for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=1) // slowly bring the speed up to avoid loading down the batteries too quickly
   {
     delay(500);
    motor1.setSpeed(speedSet);
    motor2.setSpeed(speedSet);
    motor3.setSpeed(speedSet);
    motor4.setSpeed(speedSet);
    delay(500);
   }
  }
}

void moveBackward() {
    goesForward=false;
    delay(500);
    motor1.run(BACKWARD);      
    motor2.run(BACKWARD);
    motor3.run(BACKWARD);
    motor4.run(BACKWARD);  
  for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=1) // slowly bring the speed up to avoid loading down the batteries too quickly
  {
    delay(500);
    motor1.setSpeed(speedSet);
    motor2.setSpeed(speedSet);
    motor3.setSpeed(speedSet);
    motor4.setSpeed(speedSet);
    delay(500);
  }
}  

void turnRight() {
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);     
  delay(500);
  motor1.run(FORWARD);      
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);      
} 
 
void turnLeft() {
  motor1.run(BACKWARD);     
  motor2.run(BACKWARD);  
  motor3.run(FORWARD);
  motor4.run(FORWARD);   
  delay(500);
  motor1.run(FORWARD);     
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
}  

I moved your topic to an appropriate forum category @razandakak.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

1 Like

Hi, @razandakak
Welcome to the forum.

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.

Also some images of your project?
So we can check your component layout.

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

Hey Tom!
I tried to do another thing, I powered the arduino alone by a 9V battery and the motor shield driver with another power supply of 12V, 1.3Ah
but the problem continues...

here are the components:
L293D Motor Driver shield
Arduino Uno
4 DC geared motors (2axis)
one 9v battery
one 12v battery

(the ultrasonic distance sensor part of my code is just an addition, I am struggling with one motor rotating only.)

excuse my handwriting, here is the circuit:

Try this AF motor example on one motor at a time to ensure they all work...

Hello,
I just tried this code on the four motors and they all worked perfectly (with a beeping sound, is it okay?)

so I assume, the problem is within my code?

Excellent, you have four good motors.

Now, try ONE motor on all for motor outputs on the shield and run YOUR sketch. This will verify all four motor outputs work.

I do not think a beep is programmed to occur.

I am wondering if the “beeping sound” is a continuous whine while the motor is running. If so, this is normal for motors run using PWM when the frequency is in the audible range.

Yes the sound occurs only when the motor is accelerating or decelerating, but when it stops (to delay), there is no sound.

I guess all outputs of the shield work, because I ran the code you suggested while all four motors are attached to it, I just changed the port number.

But when I run my code, just the M1 port works and run while the others are fixed.

You know (1) all motors work, and (2) all motor shield ports work.

Now, try to run two motors together. The way I would do this is put a false "distanceR" and "distanceL" just before this line to force "turnRight" and the next test to force 'turnLeft"

for example:

distanceR = 0;
distanceL = 1;
if(distanceR>=distanceL)

and then:

distanceR = 1;
distanceL = 0;
if(distanceR>=distanceL)

This is to test if your battery/wiring can make two motors turn (because turnRight and turnLeft use two motors)

Okay so here the thing I tried to simplify the code:

#include <AFMotor.h>

AF_DCMotor motor1(1, MOTOR12_1KHZ);
AF_DCMotor motor2(2, MOTOR12_1KHZ);
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);

int speedSet = 80;

void setup(){
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
}

void loop() {
    motor1.run(FORWARD);
    motor2.run(FORWARD);
    motor3.run(FORWARD); 
    motor4.run(FORWARD); 

    motor1.setSpeed(speedSet);
    motor2.setSpeed(speedSet);
    motor3.setSpeed(speedSet);
    motor4.setSpeed(speedSet);

    delay(500);
   }

THIS worked perfectly and all four motors are running simultaneously.

but this:

#include <AFMotor.h>

#define MAX_SPEED 190 

AF_DCMotor motor1(1, MOTOR12_1KHZ);
AF_DCMotor motor2(2, MOTOR12_1KHZ);
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);

int speedSet = 0;

void setup(){
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
}

void loop() {
  moveStop();
  delay(500);
  moveBackward();
  delay(500);
  moveStop();
  delay(500);
  moveForward();
}

void moveStop() {
  motor1.run(RELEASE); 
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
  } 
  
void moveForward() {

    motor1.run(FORWARD);      
    motor2.run(FORWARD);
    motor3.run(FORWARD); 
    motor4.run(FORWARD);     
   for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=1) // slowly bring the speed up to avoid loading down the batteries too quickly
   {
    motor1.setSpeed(speedSet);
    motor2.setSpeed(speedSet);
    motor3.setSpeed(speedSet);
    motor4.setSpeed(speedSet);
    delay(500);
   }
  }

void moveBackward() {
    motor1.run(BACKWARD);      
    motor2.run(BACKWARD);
    motor3.run(BACKWARD);
    motor4.run(BACKWARD);  
  for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=1) // slowly bring the speed up to avoid loading down the batteries too quickly
  {
    delay(500);
    motor1.setSpeed(speedSet);
    motor2.setSpeed(speedSet);
    motor3.setSpeed(speedSet);
    motor4.setSpeed(speedSet);
    delay(500);
  }
}  

only runs one motor which is the M1

Very good. Use your discovery to make it work.

It worked now, well, partially.
I removed the for function and the four motors run together, but the motor on port M1 is taking the most speed, that even "forward" command makes it spin in circles.

It sounds like M1 is getting more turns to drive than the others.

Make another sketch that only makes the four motors turn... flip the car over...

  • define the four motors
  • set the speed of the four motors
  • run the four motors forward
  • run the four motors backward
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.