Obstacle avoidance car esp32 based

#include <ESP32Servo.h>
Servo servo;  // create servo object to control a servo

#define trigger_pin 5
#define echo_pin 18
#define SERVO_PIN 12 //seeting servo pin 

// Setting Motor A
int enable1Pin = 14;
int motor1Pin1 = 27;
int motor1Pin2 = 26;

//Settin Motor B
int enable2Pin = 32;
int motor2Pin3 = 25;
int motor2Pin4 = 33;

// Setting PWM properties
const int frequency = 3000;
const int pwm_channel = 0;
const int resolution = 8;

long t;    //var time to calculate distance
int distance; //var to store diatnce


void setup() {
  servo.attach(SERVO_PIN);  // attaches the servo on pin 12 to the servo objectư
  pinMode(trigger_pin,OUTPUT); // attaches the trigger pin to 5 pin
  pinMode(echo_pin,INPUT); // attaches echo pin to 18 pin

   //modding motor A
  pinMode(enable1Pin, OUTPUT);
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT); 

  //modding Motor B
  pinMode(enable2Pin, OUTPUT);
  pinMode(motor2Pin3, OUTPUT);
  pinMode(motor2Pin4, OUTPUT);

   // configure LEDC PWM
  ledcAttachChannel(enable1Pin, frequency, resolution,pwm_channel);
  ledcAttachChannel(enable2Pin, frequency, resolution,pwm_channel);


}

void loop() {
  //setting hc-sro4
  digitalWrite(trigger_pin,LOW);
  delay(5);
  digitalWrite(trigger_pin,HIGH);
  delay(10);
  digitalWrite(trigger_pin,LOW);

  //getting distance from hc-sr04
  t = pulseIn(echo_pin, HIGH);
  distance = (t*0.034)/2;

  //default moving forward
  MoveForward();
  delay(200);

  //centering servo
  servo.write(90); 
  delay(500);


  if(distance<20){
    int distanceLeft = 0; //setting var for left and right
    int distanceRight = 0;
    delay(200);

    distanceLeft = ServoLookLeft(distance);
    delay(200);
    distanceRight = ServoLookRight(distance);
    delay(200);

     StopCar();
     delay(200);
     MoveBackward();
     delay(200);
     StopCar();
     delay(200);

     //calcauting shortest distance
     if(distanceRight <= distanceLeft){
           TurnLeft();
      }
     else{
             
          TurnRight();
        }
  

  }


}


//servo seek clear path

//servo turn left
int ServoLookLeft(int distance){
  int ditanceLeft = 0;
  delay(500);

  servo.write(180); //lefting servo and taking value
  delay(2000);
  

  return distance;
}

//servo turn right
int ServoLookRight(int distance){
  int ditanceRight = 0;
  delay(500);

  servo.write(0); //lefting servo and taking value
  delay(2000);
  

  return distance;
}



int SeekClearPath(){
  int ditanceRight = 0;
  int ditanceLeft = 0;
  delay(500);


  servo.write(0); //righting servo
  delay(2000);
  ditanceRight=distance;

  servo.write(180); //lefting servo
  delay(2000);
  ditanceLeft = distance;
  //lcd.clear();


  //centering  servo back
  servo.write(90); 
  delay(1000);

}


//car movement functions

//move forward
void MoveForward(){
  
  
  //Set speed motor A and B
  analogWrite(enable1Pin,255);
  analogWrite(enable2Pin,255);
  
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH);
  digitalWrite(motor2Pin3, HIGH);
  digitalWrite(motor2Pin4, LOW);
  //lcd.clear();
  
}

//move backward
void MoveBackward(){
  analogWrite(enable1Pin,255);
  analogWrite(enable2Pin,255);
  
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin3, HIGH);
  digitalWrite(motor2Pin4, LOW);
  //lcd.clear();
  delay(700);

}

//turn car left
void TurnLeft(){

   //Set speed motor A and B
  analogWrite(enable1Pin,255);
  analogWrite(enable2Pin,255);
  
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH);
  digitalWrite(motor2Pin3, LOW);
  digitalWrite(motor2Pin4, LOW);
  //lcd.clear();
  delay(850);

   //Stop motor for half second
  analogWrite(enable1Pin,0);
  analogWrite(enable2Pin,0);

  digitalWrite(motor1Pin1,LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin3,LOW);
  digitalWrite(motor2Pin4, LOW);
  delay(500);

}


//turn car  right 
void TurnRight(){
   //turn right
  analogWrite(enable1Pin,255);
  analogWrite(enable2Pin,255);
  
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin3, LOW);
  digitalWrite(motor2Pin4, HIGH);
  //lcd.clear();
  delay(850);

   //Stop motor for half second
  analogWrite(enable1Pin,0);
  analogWrite(enable2Pin,0);

  digitalWrite(motor1Pin1,LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin3,LOW);
  digitalWrite(motor2Pin4, LOW);
  delay(500);
}

// stop car
void StopCar(){
  analogWrite(enable1Pin,0);
  analogWrite(enable2Pin,0);

  digitalWrite(motor1Pin1,LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin3,LOW);
  digitalWrite(motor2Pin4, LOW);
  delay(700);
}

type or paste code here

thats my attempt for obstacle avoidance car.
the thing is , the engine wont move.

when i try this-

// Motor A
int enable1Pin = 14;
int motor1Pin1 = 27;
int motor1Pin2 = 26;

//Motor B
int enable2Pin = 32;
int motor2Pin3 = 25;
int motor2Pin4 = 33;

// Setting PWM properties
const int frequency = 3000;
const int pwm_channel = 0;
const int resolution = 8;

void setup() {
//modding motor A
pinMode(enable1Pin, OUTPUT);
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);

//modding Motor B
pinMode(enable2Pin, OUTPUT);
pinMode(motor2Pin3, OUTPUT);
pinMode(motor2Pin4, OUTPUT);

// configure LEDC PWM
ledcAttachChannel(enable1Pin, frequency, resolution,pwm_channel);
ledcAttachChannel(enable2Pin, frequency, resolution,pwm_channel);

}

void loop() {
//forward

analogWrite(enable1Pin,255);
analogWrite(enable2Pin,255);

digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin3, HIGH);
digitalWrite(motor2Pin4, LOW);
delay(700);

//Stop motor
analogWrite(enable1Pin,0);
analogWrite(enable2Pin,0);

digitalWrite(motor1Pin1,LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin3,LOW);
digitalWrite(motor2Pin4, LOW);
delay(700);

}

i get a normal movement forward.
what am i missing?

You did so well using code tags for the first sketch that you posted then failed to use them for the second sketch, Please add them

// Motor A
int enable1Pin = 14;
int motor1Pin1 = 27;
int motor1Pin2 = 26;

//Motor B
int enable2Pin = 32;
int motor2Pin3 = 25;
int motor2Pin4 = 33;

// Setting PWM properties
const int frequency = 3000;
const int pwm_channel = 0;
const int resolution = 8;




void setup() {
  //modding motor A
  pinMode(enable1Pin, OUTPUT);
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT); 

  //modding Motor B
  pinMode(enable2Pin, OUTPUT);
  pinMode(motor2Pin3, OUTPUT);
  pinMode(motor2Pin4, OUTPUT);
  
  
  // configure LEDC PWM
  ledcAttachChannel(enable1Pin, frequency, resolution,pwm_channel);
  ledcAttachChannel(enable2Pin, frequency, resolution,pwm_channel);


}

void loop() {
  //forward movement

  analogWrite(enable1Pin,255);
  analogWrite(enable2Pin,255);
  
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH);
  digitalWrite(motor2Pin3, HIGH);
  digitalWrite(motor2Pin4, LOW);
  delay(700);

  //Stop motor
  analogWrite(enable1Pin,0);
  analogWrite(enable2Pin,0);

  digitalWrite(motor1Pin1,LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin3,LOW);
  digitalWrite(motor2Pin4, LOW);
  delay(700);



}

Your first and second sketch use different devices. The first sketch, you say the motors do not move. I will guess the added servo motor and SR04 are using enough power to brown-out your project. The second project moves, and does not use the other devices. Show your wiring diagram.

The formatting is not right in the first sketch... something is missing (a brace, a parenthesis, a semi-colon?)... and if your SR04 is not working, the function StopCar() will probably stop the motors.

  • add a debug print at the beginning of each function so that you can check the flow of your program ( I see you are not using debug prints which are very usefull for debugging ) just print "MoveForward" for example
  • add a function to measure distance, so that you can use everywhere ( at the end of the function print the distance too )
  • in the function ServoLookLeft/Right you are not measuring anything, you simply return the distance passed in, so in main loop distanceLeft/Right have always the same value

Now put the car on something so that the wheels can move freely ( you have your pc connected to the car and don't whant any 'accident to happen' )
For example put an obstacle in front of the sensor, is the distance measured correctly ( repeat for several distances )?
Does the car turn/move correctly to avoid obstacle?
Then check/report the output of the serial debug ( in the arduino serial debug enable the show timestamp option, very useful )

P.S.
When describing something 'use one word more' ; - )

would esp32 wont be enough to power up sr04 and servo?

to show a pic of the project itself
or use an logic tool for wiring?
is so is there an easy one to use?

Are you using these pins ( I imagine ) to connect motor, servo and sr04?

#define trigger_pin 5
#define echo_pin 18
#define SERVO_PIN 12 //seeting servo pin 

// Setting Motor A
int enable1Pin = 14;
int motor1Pin1 = 27;
int motor1Pin2 = 26;

//Settin Motor B
int enable2Pin = 32;
int motor2Pin3 = 25;
int motor2Pin4 = 33;

The sr04 is not a problem, the servo has high peaks of current, so no better not to connect the servo to the esp32 +5v ( if supplied by usb )
If the motor controller has a good +5v output you could use that

Which servo, SG90 or a MG995?

You can use the serial monitor. There you can print the distance of the nearest object. It will show if the SONAR sensor is giving correct data.

took your advice
changed to code

#include <ESP32Servo.h>
Servo servo;  // create servo object to control a servo

#define trigger_pin 5
#define echo_pin 18
#define SERVO_PIN 12 //seeting servo pin 

// Setting Motor A
int enable1Pin = 14;
int motor1Pin1 = 27;
int motor1Pin2 = 26;

//Settin Motor B
int enable2Pin = 32;
int motor2Pin3 = 25;
int motor2Pin4 = 33;

// Setting PWM properties
const int frequency = 3000;
const int pwm_channel = 0;
const int resolution = 8;

long t;    //var time to calculate distance
int distance; //var to store diatnce


void setup() {
  servo.attach(SERVO_PIN);  // attaches the servo on pin 12 to the servo objectư
  pinMode(trigger_pin,OUTPUT); // attaches the trigger pin to 5 pin
  pinMode(echo_pin,INPUT); // attaches echo pin to 18 pin

   //modding motor A
  pinMode(enable1Pin, OUTPUT);
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT); 

  //modding Motor B
  pinMode(enable2Pin, OUTPUT);
  pinMode(motor2Pin3, OUTPUT);
  pinMode(motor2Pin4, OUTPUT);

   // configure LEDC PWM
  ledcAttachChannel(enable1Pin, frequency, resolution,pwm_channel);
  ledcAttachChannel(enable2Pin, frequency, resolution,pwm_channel);
  

  // initaite serial monitor 
  Serial.begin(9600);



}

void loop() {
  //setting hc-sro4
  digitalWrite(trigger_pin,LOW);
  delay(5);
  digitalWrite(trigger_pin,HIGH);
  delay(10);
  digitalWrite(trigger_pin,LOW);

  

  //getting distance from hc-sr04
 

  //default moving forward
  MoveForward();
  Serial.println("move forward");
  delay(200);

  //centering servo
  servo.write(90); 
  Serial.println("servo centering");
  delay(200);
  int CurrenntDistance;
  CurrenntDistance = DistanceMeasure(t, distance);
  Serial.println(CurrenntDistance);
  Serial.println("cm");



  if(distance<20){
    int CalculatedDistanceLeft = 0; //setting var for left and right distances
    int CalculatedDistanceRight = 0;
    delay(200);

     

    //car will stop
    StopCar();
    Serial.println( "car stopping");
    delay(200);
    
   
    //car backwards
    MoveBackward();
    Serial.println( "car backs uo");
    delay(200);

    //left distance calc
    CalculatedDistanceLeft = ServoLookLeft(t, distance);
    Serial.println( CalculatedDistanceLeft);
    Serial.println("cm");
    delay(200);
    
    //right  distance calc
    CalculatedDistanceRight = ServoLookRight(t, distance); //right distance calc
    Serial.println( CalculatedDistanceLeft);
    Serial.println("cm");
    delay(200);

     
     //calcauting shortest distance
     if(CalculatedDistanceRight <= CalculatedDistanceLeft){
           Serial.println("turning left");
           TurnLeft();
      }
     else{
          Serial.println("turning right");
          TurnRight();
        }
  

  }


}


//servo seek clear path funcs



//getting distance from hc-sr04
//sro4 calc distance
int DistanceMeasure(int time, int distance){
  //time to singanl get back
  t = pulseIn(echo_pin, HIGH);

  //calculation of distance
  distance = (t*0.034)/2;

  return distance;

}

//servo turn left
int ServoLookLeft(int time, int distance){
  int distanceLeft = 0;
  delay(500);

  servo.write(180); //lefting servo and taking value
  delay(2000);
  distanceLeft = DistanceMeasure(time,distance);
  

  return distanceLeft;
}

//servo turn right
int ServoLookRight(int time, int distance){
  int ditanceRight = 0;
  delay(500);

  servo.write(0); //lefting servo and taking value
  delay(2000);
  ditanceRight = DistanceMeasure(time,distance);
  

  return ditanceRight;
}



int SeekClearPath(){
  int ditanceRight = 0;
  int ditanceLeft = 0;
  delay(500);


  servo.write(0); //righting servo
  delay(2000);
  ditanceRight=distance;

  servo.write(180); //lefting servo
  delay(2000);
  ditanceLeft = distance;
  //lcd.clear();


  //centering  servo back
  servo.write(90); 
  delay(1000);

}


//car movement functions

//move forward
void MoveForward(){
  
  
  //Set speed motor A and B
  analogWrite(enable1Pin,255);
  analogWrite(enable2Pin,255);
  
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH);
  digitalWrite(motor2Pin3, HIGH);
  digitalWrite(motor2Pin4, LOW);
  //lcd.clear();
  
}

//move backward
void MoveBackward(){
  analogWrite(enable1Pin,255);
  analogWrite(enable2Pin,255);
  
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin3, HIGH);
  digitalWrite(motor2Pin4, LOW);
  //lcd.clear();
  delay(700);

}

//turn car left
void TurnLeft(){

   //Set speed motor A and B
  analogWrite(enable1Pin,255);
  analogWrite(enable2Pin,255);
  
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH);
  digitalWrite(motor2Pin3, LOW);
  digitalWrite(motor2Pin4, LOW);
  //lcd.clear();
  delay(850);

   //Stop motor for half second
  analogWrite(enable1Pin,0);
  analogWrite(enable2Pin,0);

  digitalWrite(motor1Pin1,LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin3,LOW);
  digitalWrite(motor2Pin4, LOW);
  delay(500);

}


//turn car  right 
void TurnRight(){
   //turn right
  analogWrite(enable1Pin,255);
  analogWrite(enable2Pin,255);
  
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin3, LOW);
  digitalWrite(motor2Pin4, HIGH);
  //lcd.clear();
  delay(850);

   //Stop motor for half second
  analogWrite(enable1Pin,0);
  analogWrite(enable2Pin,0);

  digitalWrite(motor1Pin1,LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin3,LOW);
  digitalWrite(motor2Pin4, LOW);
  delay(500);
}

// stop car
void StopCar(){
  analogWrite(enable1Pin,0);
  analogWrite(enable2Pin,0);

  digitalWrite(motor1Pin1,LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin3,LOW);
  digitalWrite(motor2Pin4, LOW);
  delay(700);
}

and in the serial monitor i am getting a permeant "0 cm "distance
example of the print

"cm

0

cm

turning left

move forward

servo centering

0

cm

car stopping

car backs uo

0

cm

0

cm

turning left"

SG90

...

shouldn't that be delayMicroseconds instead of milli's??

good luck.. ~q

will 9v battery to the esp matter?

I think it´s time for you to show your setup/wiring/schematics.
Have you seen post #17 issue?

Is it an ESP32 devkit V4?

what is that?

  • connect the antenna to the esp32 board ( never leave a transmitter without antenna )
  • move the 'pulse generation' inside the DistanceMeasure function ( you can also remove the 2 arguments you are not using ), as noted due delay should be delayMicroseconds
  //setting hc-sro4
  digitalWrite(trigger_pin,LOW);
  delayMicroseconds(5);
  digitalWrite(trigger_pin,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigger_pin,LOW);

  • move also the debug distance at the end of the function, before return
  Serial.print("distance ");
  Serial.println(distance);
  • in the loop you are using CurrentDistance and distance ( use only CurrentDistance )
  • on the arduino serial monitor enable the checkbox 'show timestamp'

like this?

motor A
enableA =Pin 14

ini1 = Pin 27

ini2 = pin 26

Motor B

enable B = 32

in3 =Pin 25

in4 = Pin 33