0
$\begingroup$

I am actually doing an obstacle avoiding car. So, I bought Arduino UNO and L293D motor driver and four motors, servo motor and ultrasonic sensor. After the connections I have kept 10v li-ion battery. My problem the servo and sensor and motors are not working. BUT, the LED is glowing on both UNO and motor driver. I don't understand what's the problem?

(WHILE CONNECTING THROUGH USB CABLE THE MOTOR IS WORKING GOOD.)

Here my code,

#include <AFMotor.h>  
#include <NewPing.h>
#include <Servo.h> 
 
#define TRIG_PIN A0       
#define ECHO_PIN A1   
#define MAX_DISTANCE 100   
#define MAX_SPEED 150    
#define MAX_SPEED_OFFSET 20            
 
 
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); 
 
AF_DCMotor motor1(1, MOTOR12_64KHZ);   
AF_DCMotor motor2(2, MOTOR12_64KHZ);  
AF_DCMotor motor3(3, MOTOR34_64KHZ);  
AF_DCMotor motor4(4, MOTOR34_64KHZ);    
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<=25)  
 {  
  moveStop();  
  delay(100);  
  moveBackward();  
  delay(200);  
  moveStop();  
  delay(200);  
  distanceR = lookRight();  
  delay(200);  
  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(100);  
  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;  
    motor1.run(FORWARD);        
    motor2.run(FORWARD);  
    motor3.run(FORWARD);   
    motor4.run(FORWARD);       
   for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2)   
   {  
    motor1.setSpeed(speedSet);  
    motor2.setSpeed(speedSet);  
    motor3.setSpeed(speedSet);  
    motor4.setSpeed(speedSet);  
    delay(5);  
   }  
  }  
}  
   
void moveBackward() {  
    goesForward=false;  
    motor1.run(BACKWARD);        
    motor2.run(BACKWARD);  
    motor3.run(BACKWARD);  
    motor4.run(BACKWARD);    
  for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2)   
  {  
    motor1.setSpeed(speedSet);  
    motor2.setSpeed(speedSet);  
    motor3.setSpeed(speedSet);  
    motor4.setSpeed(speedSet);  
    delay(5);  
  }  
}    
   
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);  
} 


  [1]: https://i.stack.imgur.com/9Sxy6.jpg
$\endgroup$
11
  • $\begingroup$ Welcome to Robotics, SAI MANI. Are you saying the robot works fine when connected by USB but doesn't work when it's under battery power? What troubleshooting steps have you taken? If it works one way but not the other then the software is probably not the issue. How are you connecting the battery? Is the battery charged? What voltage is it reading? $\endgroup$
    – Chuck
    Aug 3, 2022 at 15:36
  • $\begingroup$ Hi, my battery voltage is of 10-11v and i have connected it to the external power supply for l293d motor driver. $\endgroup$
    – SAI MANI
    Aug 4, 2022 at 3:45
  • $\begingroup$ Can you confirm that it works as intended when connected to USB? That aspect isn't clear. What other troubleshooting steps have you taken? Do you have pictures of how everything is connected? $\endgroup$
    – Chuck
    Aug 4, 2022 at 12:41
  • $\begingroup$ When connected to USB the servo motor and ultras sonic sensor works. Sometimes the motors also runs slowly because given voltage is only 5v. But, everything works good. $\endgroup$
    – SAI MANI
    Aug 4, 2022 at 12:55
  • $\begingroup$ My connections : im.ge/i/FYcSK0. And connections are checked and they are correct. I'm not understanding the problem. $\endgroup$
    – SAI MANI
    Aug 4, 2022 at 13:02

1 Answer 1

1
$\begingroup$

A skim through the code shows no obvious problems. The several likely problems with the electrical system suggest investigating there first.

Voltage

The 10 V you cite is an eyebrow-raiser. In my experience, the tiny, cheap electronics inside hobby servos will NOT operate reliably at more than 6 volts.

Also, if you are using the common TT motors, they are rated at 3 to 6 volts. They will be unhappy at much more than that. As with all motors, the degree of "unhappy" can range from erratic behavior to reduced life to catastrophic damage (= life reduced to zero).

Current

When your motor is working hard, it will draw a lot of current. You should compare that current against what your battery or USB can provide. You should expect trouble if you are anywhere near the limits.

When you draw current from a battery, the voltage it actually delivers will drop below what you would measure at open circuit. The more current you draw, the more it will drop, and even more so when the battery is nearly discharged. This is commonly attributed to "internal resistance", although the electrochemistry is a lot more complicated than that. You should measure the voltage at the motor when you think it should be working but is not.

A USB source has a voltage regulator which tries to keep the voltage at 5.0 volts. But a heavy motor load, even a transient one, can exceed the regulator's ability to maintain the proper voltage, causing things to misbehave. Often we try to power motors from a different source than powers the electronics.

Electrical noise

Brushed DC motors are notorious for spewing indiscriminate electrical noise that is more than capable of scrambling nearby electronics. Similarly, I would be leery of powering a PWM motor controller off the same supply as my electronics. Servos, with both buitin electronics and brushed DC motors, seem to survive their own noise, but they have builtin error correction and, typically, human controllers who expect erratic behavior.

Temporary solution

Just to get things working on the bench, I would suggest that you separate the potential issues. Get four AA batteries (4 x 1.5V = 6V) to power your motors and L293D. Use your USB to power the Arduino (getting the ground connections right!). Get the car wheels off the floor. Then test and debug the parts without them hiding each other's problems. With the car wheels off the floor, you can even test its behavior by approaching it with "obstacles" while it's still wired up in the operating room.

Good luck, you're on a good path. Autonomous behavior, even when simple, is the difference between a robot and an RC car.

$\endgroup$
6
  • $\begingroup$ I was thinking about the problem daily. Thank you so much for your help and co-operation. $\endgroup$
    – SAI MANI
    Aug 5, 2022 at 2:52
  • $\begingroup$ You're welcome. We're here to help. BTW, if you found the answer satisfactory, that green check is always appreciated. $\endgroup$
    – r-bryan
    Aug 6, 2022 at 5:25
  • $\begingroup$ I have tried this but still not coming. I have kept 2 full charged batteries (4.0*2)=8.0v. But still no change in that. $\endgroup$
    – SAI MANI
    Aug 13, 2022 at 8:57
  • $\begingroup$ Should I try it with a new one? $\endgroup$
    – SAI MANI
    Aug 13, 2022 at 8:59
  • $\begingroup$ I think the issue is with the battery. so, I'm thinking to connect with normal 9v battery. $\endgroup$
    – SAI MANI
    Aug 18, 2022 at 4:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.