Why is the motors & ultrasonic sensor on my L293D circuit not working?

Hello,
I am trying to make an obstacle avoiding robot using various youtube tutorials. I've connected the Arduino UNO to Motor Driver L293D Shield. The circuit shows :-

  1. Ultrasonic :-
    Trig - A0
    Echo - A1
    gnd - gnd
    vcc - 5v

  2. Servo motor to the servo pins

When I start it with only battery, just the light on the shield turns on. So i tried powering it with both pc & battery. The servo is working fine, but it is not stopping. Also the motors are not moving at all as well.

Can someone please help?

Code?
Schematic?

Which battery?

You have not read many of the posts on this forum. You have a lousy antique part that loses about 3V between the battery and the motor sometimes more. You are probably following many of the bad suggestions that suggest you use a 9V battery, I believe they may be posted by the battery manufacturers to generate more sales. Now connect the purple wire to the blue one but be sure thye orange does not touch it. Since we cannot see what you can post an annotated schematic showing exactly how you have wired it, showing all connections. Also post links to technical information on your hardware items. The device also needs code so post that following forum guidelines.

2 Likes

Sorry to hear this. Too many Youtubes and Instructables have bad information, so how can you make a robot? I would post a comment to the videos that their information is incorrect... or something like that.

The users here will help you build and program the obstacle avoiding robot. All you need to do is provide the information they ask from you. Add to that list of information:

  • write the parts you are using.
#include <AFMotor.h>
#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<=15)
 {
  moveStop();
  delay(100);
  moveBackward();
  delay(300);
  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(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;
    motor1.run(FORWARD);      
    motor2.run(FORWARD);
    motor3.run(FORWARD); 
    motor4.run(FORWARD);     
   for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // 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(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) // 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(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);
}  

This is a code. Can you please explain whats Schematic?

HiWatt 9v battery

I'm using Arduino UNO
Motor Driver Shield L293D
Ultrasonic sensor (HC SR04)
Jumper wires

I've bought the items from a Nearby Electronics shop & can you please explain a Schematic?

Motors? Servos?

A schematic is a hand-drawn picture with only horizontal and vertical lines that shows how all components are connected electrically together.

A schematic does NOT show the real ways how the physical wires go from left to right etc.
A schematic shows the principle in a way that makes it easy to analyse what is connected to what by DIS-regarding the physical distribution of the components.

Take a look here

best regards Stefan

Put it back in the smoke alarm, and get yourself six AAs as a minimum.

2 Likes

If this is a one up thing fine however if you want to pursue electronics for fun or work you need to do some studying. For Arduino I suggest you get the Arduino Cookbook and read it from cover to cover, it will take a few days but the knowledge gained will be worth it. When your eyes get tired and you have some time watch some tutorials some are good some are krap, use your judgement and over time it will get excellent at determining what is what. If you are really serious get a CAD (Computer Aided Design) program such as KiCad, it is a full schematic capture program that is supported and free. By the time you get through this you will be able to draw professional looking schematics and understand them.

Here's the schematic (thanks for the info btw) :-

How do i connect six batteries??

I wouldnt mind reading book for it & Im sure that im interested in it. But right now I am doing it for a science exhibition which is over by a week. So i've to complete it asap. But when I'll be done, I'll start reading arduino cookbook to gain some knowledge about it. Since i'm interested in these things.

Thanks.

https://www.amazon.com/Battery-Holder-Enclosure-Connector-Cable/dp/B01N2INSBR

Tower Pro (Micro servo 9g)

And i dont know the dc motor brand name but i can provide the image...

oh ok understood...Thanks for the link!!

You connect six cells, pretty much as the AAAA cells are connected in your Hi-Watt 9V; in series.